Gestion des imports dans les profils
This commit is contained in:
@@ -6,3 +6,4 @@ exports.Tree = require('./tree');
|
||||
exports.Const = require('./const');
|
||||
exports.Promises = require('./promises');
|
||||
exports.App = require('./app');
|
||||
exports.Profile = require('./profile');
|
||||
|
52
src/util/profile.js
Normal file
52
src/util/profile.js
Normal file
@@ -0,0 +1,52 @@
|
||||
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);
|
||||
}
|
||||
|
||||
};
|
Reference in New Issue
Block a user