2019-05-07 13:12:54 +00:00
|
|
|
// Copyright IBM Corp. 2016,2019. All Rights Reserved.
|
2016-04-27 22:55:57 +00:00
|
|
|
// Node module: loopback-boot
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2017-05-08 21:48:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-17 19:19:25 +00:00
|
|
|
const util = require('util');
|
|
|
|
const utils = require('../utils');
|
|
|
|
const path = require('path');
|
|
|
|
const async = require('async');
|
|
|
|
const debug = require('debug')('loopback:boot:script');
|
|
|
|
const PluginBase = require('../plugin-base');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const g = require('../globalize');
|
2016-04-27 22:55:57 +00:00
|
|
|
|
|
|
|
module.exports = function(options) {
|
|
|
|
return new Script(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
function Script(options) {
|
|
|
|
PluginBase.call(this, options, 'bootScripts', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(Script, PluginBase);
|
|
|
|
|
|
|
|
Script.prototype.load = function(context) {
|
2019-11-17 19:19:25 +00:00
|
|
|
const options = this.options;
|
|
|
|
const appRootDir = options.rootDir;
|
2016-04-27 22:55:57 +00:00
|
|
|
// require directories
|
2019-11-17 19:19:25 +00:00
|
|
|
let bootDirs = options.bootDirs || []; // precedence
|
2016-04-27 22:55:57 +00:00
|
|
|
bootDirs = bootDirs.concat(path.join(appRootDir, 'boot'));
|
|
|
|
utils.resolveRelativePaths(bootDirs, appRootDir);
|
|
|
|
|
2019-11-17 19:19:25 +00:00
|
|
|
let bootScripts = options.bootScripts || [];
|
2016-04-27 22:55:57 +00:00
|
|
|
utils.resolveRelativePaths(bootScripts, appRootDir);
|
|
|
|
|
|
|
|
bootDirs.forEach(function(dir) {
|
2017-03-08 09:29:59 +00:00
|
|
|
bootScripts = bootScripts.concat(
|
2019-11-17 19:19:25 +00:00
|
|
|
utils.findScripts(dir, options.scriptExtensions),
|
2017-03-08 09:29:59 +00:00
|
|
|
);
|
2019-11-17 19:19:25 +00:00
|
|
|
const envdir = dir + '/' + options.env;
|
2017-03-08 09:29:59 +00:00
|
|
|
bootScripts = bootScripts.concat(
|
2019-11-17 19:19:25 +00:00
|
|
|
utils.findScripts(envdir, options.scriptExtensions),
|
2017-03-08 09:29:59 +00:00
|
|
|
);
|
2016-04-27 22:55:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// de-dedup boot scripts -ERS
|
|
|
|
// https://github.com/strongloop/loopback-boot/issues/64
|
|
|
|
bootScripts = _.uniq(bootScripts);
|
|
|
|
debug('Boot scripts: %j', bootScripts);
|
|
|
|
this.configure(context, bootScripts);
|
|
|
|
return bootScripts;
|
|
|
|
};
|
|
|
|
|
|
|
|
Script.prototype.start = function(context, done) {
|
2019-11-17 19:19:25 +00:00
|
|
|
const app = context.app;
|
|
|
|
const instructions = context.instructions[this.name];
|
2016-04-27 22:55:57 +00:00
|
|
|
runScripts(app, instructions, done);
|
|
|
|
};
|
|
|
|
|
|
|
|
function runScripts(app, list, callback) {
|
|
|
|
list = list || [];
|
2019-11-17 19:19:25 +00:00
|
|
|
const functions = [];
|
2016-04-27 22:55:57 +00:00
|
|
|
list.forEach(function(filepath) {
|
|
|
|
debug('Requiring script %s', filepath);
|
|
|
|
try {
|
2019-11-17 19:19:25 +00:00
|
|
|
let exports = require(filepath);
|
2018-06-17 16:14:52 +00:00
|
|
|
if (exports.__esModule) exports = exports.default;
|
2016-04-27 22:55:57 +00:00
|
|
|
if (typeof exports === 'function') {
|
|
|
|
debug('Exported function detected %s', filepath);
|
|
|
|
functions.push({
|
|
|
|
path: filepath,
|
|
|
|
func: exports,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
g.error('Failed loading boot script: %s\n%s', filepath, err.stack);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async.eachSeries(functions, function(f, done) {
|
|
|
|
debug('Running script %s', f.path);
|
2019-11-17 19:19:25 +00:00
|
|
|
let cb = function(err) {
|
2017-08-18 10:42:54 +00:00
|
|
|
debug('Async function %s %s', err ? 'failed' : 'finished', f.path);
|
|
|
|
done(err);
|
|
|
|
// Make sure done() isn't called twice, e.g. if a script returns a
|
|
|
|
// thenable object and also calls the passed callback.
|
|
|
|
cb = function() {};
|
|
|
|
};
|
|
|
|
try {
|
2019-11-17 19:19:25 +00:00
|
|
|
const result = f.func(app, cb);
|
2017-08-18 10:42:54 +00:00
|
|
|
if (result && typeof result.then === 'function') {
|
|
|
|
result.then(function() { cb(); }, cb);
|
|
|
|
} else if (f.func.length < 2) {
|
2016-04-27 22:55:57 +00:00
|
|
|
debug('Sync function finished %s', f.path);
|
2017-08-18 10:42:54 +00:00
|
|
|
done();
|
2016-04-27 22:55:57 +00:00
|
|
|
}
|
2017-08-18 10:42:54 +00:00
|
|
|
} catch (err) {
|
|
|
|
debug('Sync function failed %s', f.path, err);
|
|
|
|
done(err);
|
2016-04-27 22:55:57 +00:00
|
|
|
}
|
|
|
|
}, callback);
|
|
|
|
}
|