Use forever-monitor in development only

This commit is contained in:
Aurélien Georget 2016-08-23 11:03:31 +02:00
parent 012abadc41
commit 0974eb1e02
2 changed files with 40 additions and 22 deletions

View File

@ -13,6 +13,9 @@ const path = require('path');
const _ = require('lodash'); const _ = require('lodash');
const forever = require('forever-monitor'); const forever = require('forever-monitor');
// Local Strapi dependencies.
const isLocalStrapiValid = require('../lib/private/isLocalStrapiValid');
// Logger. // Logger.
const logger = require('strapi-utils').logger; const logger = require('strapi-utils').logger;
@ -25,42 +28,57 @@ const logger = require('strapi-utils').logger;
module.exports = function () { module.exports = function () {
try { try {
// Require server configurations
const server = require(path.resolve(process.cwd(), 'config', 'environments', 'development', 'server.json'));
const options = {};
// Set NODE_ENV // Set NODE_ENV
if (_.isEmpty(process.env.NODE_ENV)) { if (_.isEmpty(process.env.NODE_ENV)) {
process.env.NODE_ENV = 'development'; process.env.NODE_ENV = 'development';
} }
if (server.reload === true && process.env.NODE_ENV === 'development') { // Require server configurations
_.assign(options, { const server = require(path.resolve(process.cwd(), 'config', 'environments', 'development', 'server.json'));
if (process.env.NODE_ENV === 'development' && server.reload === true) {
const options = _.assign({}, {
silent: false, silent: false,
watch: true, watch: true,
watchDirectory: process.cwd(), watchDirectory: process.cwd(),
killTree: true // Kills the entire child process tree on `exit` killTree: true, // Kills the entire child process tree on `exit`,
spinSleepTime: 0
}); });
} else {
_.assign(options, { const child = new (forever.Monitor)('server.js', options);
silent: process.env.NODE_ENV === 'production',
watch: false // Run listeners
child.on('restart', function() {
console.log();
logger.info('Restarting due to changes...');
}); });
// Start child process
return child.start();
} }
const child = new (forever.Monitor)('server.js', options); // Use the app's local `strapi` in `node_modules` if it's existant and valid.
const localStrapiPath = path.resolve(process.cwd(), 'node_modules', 'strapi');
// Run listeners if (isLocalStrapiValid(localStrapiPath, process.cwd())) {
child.on('restart', function() { return require(localStrapiPath).start(afterwards);
console.log(); }
logger.info('Restarting due to changes...');
console.log();
});
// Start child process // Otherwise, if no workable local `strapi` module exists,
child.start(); // run the application using the currently running version
// of `strapi`. This is probably always the global install.
return require('../lib/')().start(afterwards);
function afterwards(err, strapi) {
if (err) {
logger.error(err.stack ? err.stack : err);
strapi ? strapi.stop() : process.exit(1);
}
}
} catch (e) { } catch (e) {
console.error(e); logger.error(e);
process.exit(0); process.exit(0);
} }
}; };