53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
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) {
|
|
Logger.info('Loading import "%s"', path.resolve(node.import));
|
|
p = exports.load(node.import)
|
|
.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);
|
|
}
|
|
|
|
};
|