2014-06-02 16:47:46 +00:00
|
|
|
var assert = require('assert');
|
2014-07-16 18:44:30 +00:00
|
|
|
var cloneDeep = require('lodash.clonedeep');
|
2014-06-02 16:47:46 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
2014-06-16 13:07:21 +00:00
|
|
|
var toposort = require('toposort');
|
2014-06-02 16:47:46 +00:00
|
|
|
var ConfigLoader = require('./config-loader');
|
|
|
|
var debug = require('debug')('loopback:boot:compiler');
|
2014-10-21 14:51:17 +00:00
|
|
|
var Module = require('module');
|
2014-06-02 16:47:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gather all bootstrap-related configuration data and compile it into
|
|
|
|
* a single object containing instruction for `boot.execute`.
|
|
|
|
*
|
|
|
|
* @options {String|Object} options Boot options; If String, this is
|
|
|
|
* the application root directory; if object, has the properties
|
|
|
|
* described in `bootLoopBackApp` options above.
|
|
|
|
* @return {Object}
|
|
|
|
*
|
|
|
|
* @header boot.compile(options)
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function compile(options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2014-11-13 14:54:59 +00:00
|
|
|
if (typeof options === 'string') {
|
2014-06-02 16:47:46 +00:00
|
|
|
options = { appRootDir: options };
|
|
|
|
}
|
|
|
|
|
|
|
|
var appRootDir = options.appRootDir = options.appRootDir || process.cwd();
|
|
|
|
var env = options.env || process.env.NODE_ENV || 'development';
|
|
|
|
|
2014-09-29 14:46:13 +00:00
|
|
|
var appConfigRootDir = options.appConfigRootDir || appRootDir;
|
|
|
|
var appConfig = options.config ||
|
|
|
|
ConfigLoader.loadAppConfig(appConfigRootDir, env);
|
2014-06-02 16:47:46 +00:00
|
|
|
assertIsValidConfig('app', appConfig);
|
|
|
|
|
|
|
|
var modelsRootDir = options.modelsRootDir || appRootDir;
|
|
|
|
var modelsConfig = options.models ||
|
|
|
|
ConfigLoader.loadModels(modelsRootDir, env);
|
2014-06-09 12:43:44 +00:00
|
|
|
assertIsValidModelConfig(modelsConfig);
|
2014-06-02 16:47:46 +00:00
|
|
|
|
|
|
|
var dsRootDir = options.dsRootDir || appRootDir;
|
|
|
|
var dataSourcesConfig = options.dataSources ||
|
|
|
|
ConfigLoader.loadDataSources(dsRootDir, env);
|
|
|
|
assertIsValidConfig('data source', dataSourcesConfig);
|
|
|
|
|
|
|
|
// require directories
|
2014-08-04 08:33:03 +00:00
|
|
|
var bootDirs = options.bootDirs || []; // precedence
|
|
|
|
bootDirs = bootDirs.concat(path.join(appRootDir, 'boot'));
|
2014-10-27 10:10:57 +00:00
|
|
|
|
2014-08-04 08:33:03 +00:00
|
|
|
var bootScripts = options.bootScripts || [];
|
|
|
|
bootDirs.forEach(function(dir) {
|
|
|
|
bootScripts = bootScripts.concat(findScripts(dir));
|
|
|
|
});
|
2014-06-02 16:47:46 +00:00
|
|
|
|
2014-06-16 08:39:59 +00:00
|
|
|
var modelsMeta = modelsConfig._meta || {};
|
|
|
|
delete modelsConfig._meta;
|
|
|
|
|
2014-08-04 08:33:03 +00:00
|
|
|
var modelSources = options.modelSources || modelsMeta.sources || ['./models'];
|
2014-06-13 11:14:43 +00:00
|
|
|
var modelInstructions = buildAllModelInstructions(
|
2014-06-16 08:39:59 +00:00
|
|
|
modelsRootDir, modelsConfig, modelSources);
|
2014-06-13 11:14:43 +00:00
|
|
|
|
2014-07-16 18:44:30 +00:00
|
|
|
// When executor passes the instruction to loopback methods,
|
|
|
|
// loopback modifies the data. Since we are loading the data using `require`,
|
|
|
|
// such change affects also code that calls `require` for the same file.
|
|
|
|
return cloneDeep({
|
2014-06-25 06:09:24 +00:00
|
|
|
config: appConfig,
|
2014-06-02 16:47:46 +00:00
|
|
|
dataSources: dataSourcesConfig,
|
2014-06-13 11:14:43 +00:00
|
|
|
models: modelInstructions,
|
2014-06-02 16:47:46 +00:00
|
|
|
files: {
|
|
|
|
boot: bootScripts
|
|
|
|
}
|
2014-07-16 18:44:30 +00:00
|
|
|
});
|
2014-06-02 16:47:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function assertIsValidConfig(name, config) {
|
2014-11-13 14:54:59 +00:00
|
|
|
if (config) {
|
2014-06-02 16:47:46 +00:00
|
|
|
assert(typeof config === 'object',
|
|
|
|
name + ' config must be a valid JSON object');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 12:43:44 +00:00
|
|
|
function assertIsValidModelConfig(config) {
|
|
|
|
assertIsValidConfig('model', config);
|
|
|
|
for (var name in config) {
|
|
|
|
var entry = config[name];
|
|
|
|
var options = entry.options || {};
|
|
|
|
var unsupported = entry.properties ||
|
|
|
|
entry.base || options.base ||
|
|
|
|
entry.plural || options.plural;
|
|
|
|
|
|
|
|
if (unsupported) {
|
|
|
|
throw new Error(
|
2014-07-15 09:05:42 +00:00
|
|
|
'The data in model-config.json is in the unsupported 1.x format.');
|
2014-06-09 12:43:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 16:47:46 +00:00
|
|
|
/**
|
|
|
|
* Find all javascript files (except for those prefixed with _)
|
|
|
|
* and all directories.
|
|
|
|
* @param {String} dir Full path of the directory to enumerate.
|
|
|
|
* @return {Array.<String>} A list of absolute paths to pass to `require()`.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
|
|
|
|
function findScripts(dir) {
|
|
|
|
assert(dir, 'cannot require directory contents without directory name');
|
|
|
|
|
|
|
|
var files = tryReadDir(dir);
|
|
|
|
|
|
|
|
// sort files in lowercase alpha for linux
|
|
|
|
files.sort(function(a, b) {
|
|
|
|
a = a.toLowerCase();
|
|
|
|
b = b.toLowerCase();
|
|
|
|
|
|
|
|
if (a < b) {
|
|
|
|
return -1;
|
|
|
|
} else if (b < a) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var results = [];
|
|
|
|
files.forEach(function(filename) {
|
|
|
|
// ignore index.js and files prefixed with underscore
|
|
|
|
if ((filename === 'index.js') || (filename[0] === '_')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filepath = path.resolve(path.join(dir, filename));
|
|
|
|
var ext = path.extname(filename);
|
|
|
|
var stats = fs.statSync(filepath);
|
|
|
|
|
|
|
|
// only require files supported by require.extensions (.txt .md etc.)
|
|
|
|
if (stats.isFile()) {
|
|
|
|
if (ext in require.extensions)
|
|
|
|
results.push(filepath);
|
|
|
|
else
|
|
|
|
debug('Skipping file %s - unknown extension', filepath);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
path.join(require.resolve(filepath));
|
2014-11-13 14:54:59 +00:00
|
|
|
} catch (err) {
|
2014-06-02 16:47:46 +00:00
|
|
|
debug('Skipping directory %s - %s', filepath, err.code || err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryReadDir() {
|
|
|
|
try {
|
|
|
|
return fs.readdirSync.apply(fs, arguments);
|
2014-11-13 14:54:59 +00:00
|
|
|
} catch (e) {
|
2014-06-02 16:47:46 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2014-06-13 11:14:43 +00:00
|
|
|
|
|
|
|
function buildAllModelInstructions(rootDir, modelsConfig, sources) {
|
|
|
|
var registry = findModelDefinitions(rootDir, sources);
|
|
|
|
|
|
|
|
var modelNamesToBuild = addAllBaseModels(registry, Object.keys(modelsConfig));
|
|
|
|
|
|
|
|
var instructions = modelNamesToBuild
|
|
|
|
.map(function createModelInstructions(name) {
|
|
|
|
var config = modelsConfig[name];
|
|
|
|
var definition = registry[name] || {};
|
|
|
|
|
|
|
|
debug('Using model "%s"\nConfiguration: %j\nDefinition %j',
|
|
|
|
name, config, definition.definition);
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
config: config,
|
|
|
|
definition: definition.definition,
|
|
|
|
sourceFile: definition.sourceFile
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return sortByInheritance(instructions);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addAllBaseModels(registry, modelNames) {
|
|
|
|
var result = [];
|
|
|
|
var visited = {};
|
|
|
|
|
|
|
|
while (modelNames.length) {
|
|
|
|
var name = modelNames.shift();
|
2014-06-16 13:07:21 +00:00
|
|
|
|
|
|
|
if (visited[name]) continue;
|
|
|
|
visited[name] = true;
|
2014-06-13 11:14:43 +00:00
|
|
|
result.push(name);
|
|
|
|
|
|
|
|
var definition = registry[name] && registry[name].definition;
|
|
|
|
if (!definition) continue;
|
|
|
|
|
2014-06-16 13:07:21 +00:00
|
|
|
var base = getBaseModelName(definition);
|
2014-06-13 11:14:43 +00:00
|
|
|
|
|
|
|
// ignore built-in models like User
|
|
|
|
if (!registry[base]) continue;
|
|
|
|
|
|
|
|
modelNames.push(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-16 13:07:21 +00:00
|
|
|
function getBaseModelName(modelDefinition) {
|
|
|
|
if (!modelDefinition)
|
|
|
|
return undefined;
|
|
|
|
|
|
|
|
return modelDefinition.base ||
|
|
|
|
modelDefinition.options && modelDefinition.options.base;
|
|
|
|
}
|
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
function sortByInheritance(instructions) {
|
2014-06-16 13:07:21 +00:00
|
|
|
// create edges Base name -> Model name
|
|
|
|
var edges = instructions
|
|
|
|
.map(function(inst) {
|
|
|
|
return [getBaseModelName(inst.definition), inst.name];
|
|
|
|
});
|
|
|
|
|
|
|
|
var sortedNames = toposort(edges);
|
|
|
|
|
|
|
|
var instructionsByModelName = {};
|
|
|
|
instructions.forEach(function(inst) {
|
|
|
|
instructionsByModelName[inst.name] = inst;
|
|
|
|
});
|
|
|
|
|
|
|
|
return sortedNames
|
|
|
|
// convert to instructions
|
|
|
|
.map(function(name) {
|
|
|
|
return instructionsByModelName[name];
|
|
|
|
})
|
|
|
|
// remove built-in models
|
|
|
|
.filter(function(inst) {
|
|
|
|
return !!inst;
|
|
|
|
});
|
2014-06-13 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function findModelDefinitions(rootDir, sources) {
|
|
|
|
var registry = {};
|
|
|
|
|
|
|
|
sources.forEach(function(src) {
|
2014-10-21 14:51:17 +00:00
|
|
|
var srcDir = resolveSourceDir(rootDir, src);
|
|
|
|
if (!srcDir) {
|
|
|
|
debug('Skipping unknown module source dir %j', src);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
var files = tryReadDir(srcDir);
|
2014-10-21 06:21:40 +00:00
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
files
|
|
|
|
.filter(function(f) {
|
|
|
|
return f[0] !== '_' && path.extname(f) === '.json';
|
|
|
|
})
|
|
|
|
.forEach(function(f) {
|
|
|
|
var fullPath = path.resolve(srcDir, f);
|
2014-10-21 06:21:40 +00:00
|
|
|
var entry = loadModelDefinition(rootDir, fullPath, files);
|
2014-06-13 11:14:43 +00:00
|
|
|
var modelName = entry.definition.name;
|
|
|
|
if (!modelName) {
|
|
|
|
debug('Skipping model definition without Model name: %s',
|
|
|
|
path.relative(srcDir, fullPath));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
registry[modelName] = entry;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return registry;
|
|
|
|
}
|
|
|
|
|
2014-10-21 14:51:17 +00:00
|
|
|
function resolveSourceDir(rootDir, sourceDir) {
|
|
|
|
var srcDir = path.resolve(rootDir, sourceDir);
|
|
|
|
if (fs.existsSync(srcDir))
|
|
|
|
return srcDir;
|
|
|
|
|
|
|
|
// Handle module-relative path, e.g. `loopback/common/models`
|
|
|
|
var start = sourceDir.substring(0, 2);
|
|
|
|
if (start !== './' && start !== '..') {
|
|
|
|
// Module.globalPaths is a list of globally configured paths like
|
|
|
|
// [ env.NODE_PATH values, $HOME/.node_modules, etc. ]
|
|
|
|
// Module._nodeModulePaths(rootDir) returns a list of paths like
|
|
|
|
// [ rootDir/node_modules, rootDir/../node_modules, etc. ]
|
|
|
|
var modulePaths = Module.globalPaths
|
|
|
|
.concat(Module._nodeModulePaths(rootDir));
|
|
|
|
|
|
|
|
srcDir = modulePaths
|
|
|
|
.map(function(candidateDir) {
|
|
|
|
return path.join(candidateDir, sourceDir);
|
|
|
|
})
|
|
|
|
.filter(function(candidate) {
|
|
|
|
return fs.existsSync(candidate);
|
|
|
|
})
|
|
|
|
[0];
|
|
|
|
if (srcDir)
|
|
|
|
return srcDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2014-10-21 06:21:40 +00:00
|
|
|
function loadModelDefinition(rootDir, jsonFile, allFiles) {
|
2014-06-13 11:14:43 +00:00
|
|
|
var definition = require(jsonFile);
|
2014-10-21 06:21:40 +00:00
|
|
|
var basename = path.basename(jsonFile, path.extname(jsonFile));
|
|
|
|
|
2014-10-27 10:10:57 +00:00
|
|
|
// find a matching file with a supported extension like `.js` or `.coffee`
|
2014-11-13 14:54:59 +00:00
|
|
|
var base;
|
|
|
|
var ext;
|
|
|
|
var validFileType;
|
2014-10-21 06:21:40 +00:00
|
|
|
var sourceFile = allFiles
|
|
|
|
.filter(function(f) {
|
|
|
|
ext = path.extname(f);
|
|
|
|
base = path.basename(f, ext);
|
|
|
|
validFileType = (ext !== '.node') && (ext !== '.json') &&
|
|
|
|
((typeof require.extensions[ext]) === 'function');
|
|
|
|
return validFileType && (base === basename);
|
|
|
|
})[0];
|
2014-06-13 11:14:43 +00:00
|
|
|
|
|
|
|
try {
|
2014-10-21 06:21:40 +00:00
|
|
|
sourceFile = path.join(path.dirname(jsonFile), sourceFile);
|
2014-06-13 11:14:43 +00:00
|
|
|
sourceFile = require.resolve(sourceFile);
|
|
|
|
} catch (err) {
|
|
|
|
debug('Model source code not found: %s - %s', sourceFile, err.code || err);
|
|
|
|
sourceFile = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('Found model "%s" - %s %s', definition.name,
|
|
|
|
path.relative(rootDir, jsonFile),
|
|
|
|
sourceFile ? path.relative(rootDir, sourceFile) : '(no source file)');
|
|
|
|
|
|
|
|
return {
|
|
|
|
definition: definition,
|
|
|
|
sourceFile: sourceFile
|
|
|
|
};
|
|
|
|
}
|