From 0974eb1e02b9efac9ec74aeb5e99e280b69e14c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Georget?= Date: Tue, 23 Aug 2016 11:03:31 +0200 Subject: [PATCH] Use forever-monitor in development only --- packages/strapi/bin/strapi-start.js | 60 +++++++++++++++--------- packages/strapi/lib/private/loadHooks.js | 2 +- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/packages/strapi/bin/strapi-start.js b/packages/strapi/bin/strapi-start.js index 110ba5fb49..8234eec130 100755 --- a/packages/strapi/bin/strapi-start.js +++ b/packages/strapi/bin/strapi-start.js @@ -13,6 +13,9 @@ const path = require('path'); const _ = require('lodash'); const forever = require('forever-monitor'); +// Local Strapi dependencies. +const isLocalStrapiValid = require('../lib/private/isLocalStrapiValid'); + // Logger. const logger = require('strapi-utils').logger; @@ -25,42 +28,57 @@ const logger = require('strapi-utils').logger; module.exports = function () { try { - // Require server configurations - const server = require(path.resolve(process.cwd(), 'config', 'environments', 'development', 'server.json')); - const options = {}; - // Set NODE_ENV if (_.isEmpty(process.env.NODE_ENV)) { process.env.NODE_ENV = 'development'; } - if (server.reload === true && process.env.NODE_ENV === 'development') { - _.assign(options, { + // Require server configurations + 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, watch: true, 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, { - silent: process.env.NODE_ENV === 'production', - watch: false + + const child = new (forever.Monitor)('server.js', options); + + // 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 - child.on('restart', function() { - console.log(); - logger.info('Restarting due to changes...'); - console.log(); - }); + if (isLocalStrapiValid(localStrapiPath, process.cwd())) { + return require(localStrapiPath).start(afterwards); + } - // Start child process - child.start(); + // Otherwise, if no workable local `strapi` module exists, + // 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) { - console.error(e); + logger.error(e); process.exit(0); } }; diff --git a/packages/strapi/lib/private/loadHooks.js b/packages/strapi/lib/private/loadHooks.js index 800614c38b..2dd544a08c 100644 --- a/packages/strapi/lib/private/loadHooks.js +++ b/packages/strapi/lib/private/loadHooks.js @@ -84,7 +84,7 @@ module.exports = function (strapi) { strapi.emit('hook:' + id + ':error'); return cb(err); } - + strapi.emit('hook:' + id + ':loaded'); // Defer to next tick to allow other stuff to happen.