var RecursiveIterator = require('recursive-iterator'); var System = require('./system'); var Logger = require('./logger'); var _ = require('lodash'); var path = require('path'); // Load a profile file with imports exports.load = function(profileUrl, withImports) { withImports = withImports || true; // Load root profile return System.loadJSON(profileUrl) .then(function(profile) { if(!withImports) { return profile; } var promises = []; // Search for imports var iterator = new RecursiveIterator(profile); for(var node, item = iterator.next(); !item.done; item = iterator.next()) { node = item.value.node; // For each import found, load the partial and "mount" it on the current item if(node.import) { var importPath = path.resolve(path.dirname(profileUrl), node.import) Logger.info('Loading import "%s"', importPath); p = exports.load(importPath) .then(_mountImport.bind(node)) ; promises.push(p); } } // When all imports are loaded, return the complete profile return Promise.all(promises) .then(function() { return profile; }) ; }) ; // Internal method, extend "this" (the mount point) with the partial profile function _mountImport(importProfile) { _.extend(this, importProfile); } };