const path = require('path');
const commander = require('commander');
const spawn = require('cross-spawn');
const chalk = require('chalk');

const newApp = require('./new');
const reinstallDev = require('./reinstallDev');
const watch = require('./watch');
const resetHard = require('./reset');

const exitWithError = message => {
  console.error(chalk.red(`ERROR: ${message}`));
  process.exit(1);
};

commander.version(
  require(path.join(__dirname, 'package.json')).version,
);

commander
  .command('new')
  .description('generate a new application')
  .arguments('<appName>')
  .option('-d, --dev <path>', 'relative path to development version of gluestick')
  .option('-s, --skip-main', 'gluestick will not generate main app')
  .option('-n, --npm', 'use npm instead of yarn')
  .action((appName, options) => {
    newApp(appName, options, exitWithError);
  });

commander
  .command('reinstall-dev')
  .description('reinstall gluestick dependency')
  .action(() => {
    reinstallDev(exitWithError);
  });

commander
  .command('watch')
  .description('watch and apply changes from gluestick to project')
  .action(() => {
    watch(exitWithError);
  });

commander
  .command('reset-hard')
  .description('remove gluestick and build, cache clean and reinstall-dev')
  .action(() => {
    resetHard();
  });

commander
  .command('*', null, { noHelp: true })
  .action(() => {
    const childProcess = spawn(
      './node_modules/.bin/gluestick',
      commander.rawArgs.slice(2),
      { stdio: 'inherit' },
    );
    childProcess.on('error', (error) => {
      console.error(chalk.red(error));
      process.exit(1);
    });
    childProcess.on('exit', (code) => {
      if (code !== 0) {
        process.exit(code);
      }
    });
  });

commander.parse(process.argv);

@@ -284,24 +284,96 @@
 ./reset');%0A%0A
+const version = require(path.join(__dirname, 'package.json')).version;%0A%0A
 const exitWi
@@ -489,38 +489,455 @@
 ion(
-%0A  require(path.join(__dirname
+version);%0A%0Aif (%5B'-v', '-V', '--version'%5D.indexOf(process.argv%5B2%5D) %3E= 0) %7B%0A  console.log(%60$%7Bchalk.green('gluestick-cli')%7D: $%7Bversion%7D (global binary)%60);%0A  let localVersion = 'n/a';%0A  let localInstalledVersion = '';%0A  try %7B%0A    localVersion = require(path.join(process.cwd(), 'package.json')).dependencies.gluestick;%0A  %7D catch (e) %7B%0A    // noop%0A  %7D%0A  try %7B%0A    localInstalledVersion = require(%0A      path.join(process.cwd(), 'node_modules/gluestick'
 , 'p
@@ -941,32 +941,38 @@
  'package.json')
+,%0A    
 ).version,%0A);%0A%0Ac
@@ -968,12 +968,408 @@
 sion
-,%0A);
+;%0A  %7D catch (e) %7B%0A    // noop%0A  %7D%0A  console.log(%0A    %60$%7Bchalk.green('gluestick')%7D: $%7BlocalVersion%7D$%7B%0A      localVersion !== localInstalledVersion && localInstalledVersion.length%0A        ? %60, installed $%7BlocalInstalledVersion%7D%60%0A        : ''%0A    %7D ($%7B%0A      localVersion !== 'n/a'%0A        ? 'local dependency in current directory'%0A        : 'not in gluestick project'%0A    %7D)%60,%0A  );%0A  process.exit(0);%0A%7D
 %0A%0Aco

improve version printing to account for local gs dependency (#981)