'use strict';

const path = require('path');
const cluster = require('cluster');
const fs = require('fs-extra');
const { cyan } = require('chalk');
const chokidar = require('chokidar');
const execa = require('execa');

const { logger } = require('strapi-utils');
const strapi = require('../index');

/**
 * `$ strapi develop`
 *
 */
module.exports = async function({ build }) {
  const dir = process.cwd();

  if (build && !fs.existsSync(path.join(dir, 'build'))) {
    console.log(`> No ${cyan('build')} dir found. Starting build`);
    try {
      execa.shellSync('npm run -s build', {
        stdio: 'inherit',
      });
    } catch (err) {
      process.exit(1);
    }
  }

  try {
    const strapiInstance = strapi({ dir, autoReload: true });

    if (cluster.isMaster) {
      cluster.on('message', (worker, message) => {
        switch (message) {
          case 'reload':
            strapiInstance.log.info('The server is restarting\n');
            worker.send('isKilled');
            break;
          case 'kill':
            worker.kill();
            cluster.fork();
            break;
          case 'stop':
            worker.kill();
            process.exit(1);
            break;
          default:
            return;
        }
      });

      cluster.fork();
    }

    if (cluster.isWorker) {
      watchFileChanges({ dir, strapiInstance });

      process.on('message', message => {
        switch (message) {
          case 'isKilled':
            strapiInstance.server.destroy(() => {
              process.send('kill');
            });
            break;
          default:
          // Do nothing.
        }
      });

      return strapiInstance.start();
    }
  } catch (e) {
    logger.error(e);
    process.exit(1);
  }
};

/**
 * Init file watching to auto restart strapi app
 * @param {Object} options - Options object
 * @param {string} options.dir - This is the path where the app is located, the watcher will watch the files under this folder
 * @param {Strapi} options.strapi - Strapi instance
 */
function watchFileChanges({ dir, strapiInstance }) {
  const restart = () => {
    if (
      strapiInstance.reload.isWatching &&
      !strapiInstance.reload.isReloading
    ) {
      strapiInstance.reload.isReloading = true;
      strapiInstance.reload();
    }
  };

  const watcher = chokidar.watch(dir, {
    ignoreInitial: true,
    ignored: [
      /(^|[/\\])\../,
      /tmp/,
      '**/admin',
      '**/admin/**',
      '**/components',
      '**/components/**',
      '**/documentation',
      '**/documentation/**',
      '**/node_modules',
      '**/node_modules/**',
      '**/plugins.json',
      '**/index.html',
      '**/public',
      '**/public/**',
      '**/cypress',
      '**/cypress/**',
      '**/*.db*',
      '**/exports/**',
    ],
  });

  watcher
    .on('add', path => {
      strapiInstance.log.info(`File created: ${path}`);
      restart();
    })
    .on('change', path => {
      strapiInstance.log.info(`File changed: ${path}`);
      restart();
    })
    .on('unlink', path => {
      strapiInstance.log.info(`File deleted: ${path}`);
      restart();
    });
}

@@ -110,43 +110,8 @@
 ');%0A
-const %7B cyan %7D = require('chalk');%0A
 cons
@@ -428,76 +428,8 @@
 ) %7B%0A
-    console.log(%60%3E No $%7Bcyan('build')%7D dir found. Starting build%60);%0A
     

Remove unnecessary log