#!/usr/bin/env node

'use strict';

/**
 * Module dependencies
 */

// Node.js core.
const cp = require('child_process');
const path = require('path');

// Public dependencies
const _ = require('lodash');
const forever = require('forever-monitor');

// Local Strapi dependencies.
const isLocalStrapiValid = require('../lib/private/isLocalStrapiValid');

// Logger.
const logger = require('strapi-utils').logger;

/**
 * `$ strapi start`
 *
 * Expose method which starts the appropriate instance of Strapi
 * (fire up the application in our working directory).
 */

module.exports = function () {
  try {
    // Set NODE_ENV
    if (_.isEmpty(process.env.NODE_ENV)) {
      process.env.NODE_ENV = 'development';
    }

    // 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(),
        watchIgnoreDotFiles: true, // Whether to ignore file starting with a '.'
        watchIgnorePatterns: ['node_modules/**/*', 'public/**/*', '.git/**/*', '.idea'], // Ignore patterns to use when watching files.
        killTree: true, // Kills the entire child process tree on `exit`,
        spinSleepTime: 0,
        command: 'node --harmony-async-await'
      });

      const child = new (forever.Monitor)('server.js', options);

      // Run listeners
      child.on('watch:restart', (info) => {
        console.log();
        logger.verbose('Restarting due to ' + info.file + '... (' + info.stat.replace(child.cwd, '.') + ')');
        console.log();
      });

      child.on('exit:code', function(code) {
        console.log();
        process.exit(code);
      });

      // Start child process
      return child.start();
    }

    // Run app as a child_process
    // when harmony flag is not detected.
    if (!~process.execArgv.indexOf('--harmony')) {
      const opts = Object.create(process.env);
      opts.execArgv = ['--harmony-async-await'];

      return cp.fork(path.resolve(process.cwd(), 'server.js'), opts);
    }

    // Use the app's local `strapi` in `node_modules` if it's existant and valid.
    const localStrapiPath = path.resolve(process.cwd(), 'node_modules', 'strapi');

    if (isLocalStrapiValid(localStrapiPath, process.cwd())) {
      return require(localStrapiPath).start(afterwards);
    }

    // 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('strapi').start(afterwards);
  } catch (e) {
    logger.error(e);
    process.exit(0);
  }
};

function afterwards(err, strapi) {
  if (err) {
    logger.error(err.stack ? err.stack : err);

    strapi ? strapi.stop() : process.exit(1);
  }
}

@@ -1565,39 +1565,16 @@
 o) =%3E %7B%0A
-        console.log();%0A
         
@@ -1758,39 +1758,38 @@
 ) %7B%0A        
-console.log();%0A
+if (code) %7B%0A  
         proc
@@ -1800,24 +1800,34 @@
 exit(code);%0A
+        %7D%0A
       %7D);%0A%0A 

Fix restart when there is not exit code