Merge pull request #46 from wistityhq/feature/installation

Auto install adapters and template engines on restart
This commit is contained in:
Loïc Saint-Roch 2015-12-03 14:44:37 +01:00
commit e9e54b8667
4 changed files with 83 additions and 7 deletions

View File

@ -6,6 +6,7 @@
// Node.js core.
const path = require('path');
const spawn = require('child_process').spawn;
// Public node modules.
const _ = require('lodash');
@ -56,6 +57,52 @@ module.exports = function (strapi) {
}
cb();
},
/**
* Installation template engines
*/
installation: function () {
const done = _.after(_.size(strapi.config.views.map), function () {
strapi.emit('hook:views:installed');
});
_.forEach(strapi.config.views.map, function (engine) {
try {
require(path.resolve(strapi.config.appPath, 'node_modules', engine));
done();
} catch (err) {
if (strapi.config.environment === 'development') {
strapi.log.warn('Installing the `' + engine + '` template engine, please wait...');
console.log();
const process = spawn('npm', ['install', engine, '--save']);
process.on('error', function (error) {
strapi.log.error('The template engine `' + engine + '` has not been installed.');
strapi.log.error(error);
process.exit(1);
});
process.on('close', function (code) {
if (code !== 0) {
strapi.log.error('The template engine `' + engine + '` has not been installed.');
strapi.log.error('Code: ' + code);
process.exit(1);
}
strapi.log.info('`' + engine + '` successfully installed');
done();
});
} else {
strapi.log.error('The template engine `' + engine + '` is not installed.');
strapi.log.error('Execute `$ npm install ' + engine + ' --save` to install it.');
process.exit(1);
}
}
});
}
};

View File

@ -285,6 +285,7 @@ module.exports = function (strapi) {
process.exit(1);
}
strapi.log.info('`' + adapter + '` successfully installed');
done();
});
} else {

View File

@ -141,7 +141,7 @@ module.exports = function (strapi) {
// Prepare all other hooks.
prepare: function prepareHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router', 'waterline'), function (id, cb) {
prepareHook(id);
process.nextTick(cb);
}, cb);
@ -149,7 +149,7 @@ module.exports = function (strapi) {
// Apply the default config for all other hooks.
defaults: function defaultConfigHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router', 'waterline'), function (id, cb) {
const hook = hooks[id];
applyDefaults(hook);
process.nextTick(cb);
@ -158,7 +158,7 @@ module.exports = function (strapi) {
// Load all other hooks.
load: function loadOtherHooks(cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router', 'waterline'), function (id, cb) {
loadHook(id, cb);
}, cb);
},
@ -171,6 +171,16 @@ module.exports = function (strapi) {
prepareHook('router');
applyDefaults(hooks.router);
loadHook('router', cb);
},
// Load the waterline hook.
waterline: function loadWaterlineHook(cb) {
if (!hooks.waterline) {
return cb();
}
prepareHook('waterline');
applyDefaults(hooks.waterline);
loadHook('waterline', cb);
}
},

View File

@ -42,6 +42,7 @@ module.exports = cb => {
// Callback.
err => {
let count = 0;
// Just in case there is an error.
if (err) {
@ -57,12 +58,21 @@ module.exports = cb => {
// Run adapters installation
if (cluster.isMaster) {
self.hooks.waterline.installation();
++count;
if (_.isPlainObject(self.config.views) && !_.isBoolean(self.config.views)) {
self.hooks.views.installation();
++count;
}
}
// Install new adapters
strapi.after('hook:waterline:installed', () => {
self.log.warn('Application is restarting...');
console.log();
const installed = _.after(count, () => {
if (_.isPlainObject(strapi.config.reload) && !_.isEmpty(strapi.config.reload) && strapi.config.reload.workers > 0) {
self.log.warn('Application is restarting...');
console.log();
}
// Teardown Waterline adapters and
// reload the Waterline ORM.
@ -99,5 +109,13 @@ module.exports = cb => {
// Reloading the ORM.
self.hooks.waterline.reload();
});
self.after('hook:waterline:installed', () => {
installed();
});
self.after('hook:views:installed', () => {
installed();
});
});
};