2014-06-02 16:47:46 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
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');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 || {};
|
|
|
|
|
|
|
|
if(typeof options === 'string') {
|
|
|
|
options = { appRootDir: options };
|
|
|
|
}
|
|
|
|
|
|
|
|
var appRootDir = options.appRootDir = options.appRootDir || process.cwd();
|
|
|
|
var env = options.env || process.env.NODE_ENV || 'development';
|
|
|
|
|
2014-06-25 06:09:24 +00:00
|
|
|
var appConfig = options.config || ConfigLoader.loadAppConfig(appRootDir, 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
|
|
|
|
var bootScripts = findScripts(path.join(appRootDir, 'boot'));
|
|
|
|
|
2014-06-16 08:39:59 +00:00
|
|
|
var modelsMeta = modelsConfig._meta || {};
|
|
|
|
delete modelsConfig._meta;
|
|
|
|
|
|
|
|
var 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-06-02 16:47:46 +00:00
|
|
|
return {
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function assertIsValidConfig(name, config) {
|
|
|
|
if(config) {
|
|
|
|
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(
|
|
|
|
'The data in models.json is in the unsupported 1.x format.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
} catch(err) {
|
|
|
|
debug('Skipping directory %s - %s', filepath, err.code || err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryReadDir() {
|
|
|
|
try {
|
|
|
|
return fs.readdirSync.apply(fs, arguments);
|
|
|
|
} catch(e) {
|
|
|
|
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) {
|
|
|
|
var srcDir = path.resolve(rootDir, src);
|
|
|
|
var files = tryReadDir(srcDir);
|
|
|
|
files
|
|
|
|
.filter(function(f) {
|
|
|
|
return f[0] !== '_' && path.extname(f) === '.json';
|
|
|
|
})
|
|
|
|
.forEach(function(f) {
|
|
|
|
var fullPath = path.resolve(srcDir, f);
|
|
|
|
var entry = loadModelDefinition(rootDir, fullPath);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadModelDefinition(rootDir, jsonFile) {
|
|
|
|
var definition = require(jsonFile);
|
|
|
|
|
|
|
|
var sourceFile = path.join(
|
|
|
|
path.dirname(jsonFile),
|
|
|
|
path.basename(jsonFile, path.extname(jsonFile)));
|
|
|
|
|
|
|
|
try {
|
|
|
|
// resolve the file to `.js` or any other supported extension like `.coffee`
|
|
|
|
sourceFile = require.resolve(sourceFile);
|
|
|
|
} catch (err) {
|
|
|
|
debug('Model source code not found: %s - %s', sourceFile, err.code || err);
|
|
|
|
sourceFile = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sourceFile === jsonFile)
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|