'use strict';

var _os = require('os');

var _os2 = _interopRequireDefault(_os);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _util = require('../util');

var _util2 = _interopRequireDefault(_util);

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _nodemon = require('nodemon');

var _nodemon2 = _interopRequireDefault(_nodemon);

var _monitorctrlc = require('monitorctrlc');

var _child_process = require('child_process');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const getPath = path => {
  if (_os2.default.platform() === 'win32') {
    const shortPath = (0, _child_process.execSync)(`@echo off && for %I in ("${path}") do echo %~sI`);
    return shortPath.toString('utf8').replace(/(\n|\r)+$/, '');
  } else {
    return path;
  }
};

/**
 * Entry point of botpress
 *
 * It will do the following things:
 *
 * 1. Find botpress instance creator in `node_modules` folder in current project.
 * 2. Find the `botfile.js` which will be injected into the creator to create the instance.
 * 3. Start the botpress instance.
 */
module.exports = (projectPath, options) => {
  let Botpress = null;

  if (!projectPath || typeof projectPath !== 'string') {
    projectPath = '.';
  }

  projectPath = _path2.default.resolve(projectPath);

  try {
    const botpress = require(_path2.default.resolve(__dirname, '..'));
    Botpress = botpress.Botpress();
  } catch (err) {
    _util2.default.print('error', err.message);
    _util2.default.print('error', err.stack);
    _util2.default.print('error', '(fatal) Could not load the local version of botpress');
    _util2.default.print('Hint: 1) have you used `botpress init` to create a new bot the proper way?');
    _util2.default.print('Hint: 2) Do you have read and write permissions on the current directory?');
    _util2.default.print('-------------');
    _util2.default.print('If none of the above works, this might be a bug in botpress. ' + 'Please contact the Botpress Team on gitter and provide the printed error above.');
    process.exit(1);
  }

  const botfile = _path2.default.join(projectPath, 'botfile.js');
  if (!_fs2.default.existsSync(botfile)) {
    _util2.default.print('error', `(fatal) No ${_chalk2.default.bold('botfile.js')} file found at: ` + botfile);
    process.exit(1);
  }

  const getDefaultWatchIgnore = () => {
    const bf = require(botfile);
    const dataDir = _util2.default.getDataLocation(bf.dataDir, projectPath);
    return [dataDir, 'node_modules'];
  };

  const opts = options.opts();
  if (opts.watch || opts.w) {
    _util2.default.print('info', '*** watching files for changes ***');

    const argvWithoutWatch = process.argv.filter(arg => !/^(--watch|-w)$/.test(arg));
    argvWithoutWatch[0] = getPath(argvWithoutWatch[0]);

    const nodemonOptions = {
      cwd: process.cwd(),
      exec: argvWithoutWatch.join(' '),
      ext: opts.watchExt,
      watch: opts.watchDir && opts.watchDir.length ? opts.watchDir : undefined,
      ignore: opts.watchIgnore && opts.watchIgnore.length ? opts.watchIgnore : getDefaultWatchIgnore(),
      stdin: false,
      restartable: false
    };

    const mon = (0, _nodemon2.default)(nodemonOptions);
    mon.on('restart', (changedFile, two) => _util2.default.print('info', '*** restarting botpress because of file change: ', changedFile));

    (0, _monitorctrlc.monitorCtrlC)(() => {
      mon.emit('quit');
      setTimeout(() => process.exit(), 100);
    });
  } else {
    const bot = new Botpress({ botfile, options });
    bot.start();
  }
};
//# sourceMappingURL=start.js.map