#!/usr/bin/env node
'use strict';

var _Create = require('./cli/cms/Create');

var _Create2 = _interopRequireDefault(_Create);

var _plugins = require('./cli/extend/plugins');

var _plugins2 = _interopRequireDefault(_plugins);

var _child_process = require('child_process');

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _commander = require('commander');

var _commander2 = _interopRequireDefault(_commander);

var _package = require('../package');

var _package2 = _interopRequireDefault(_package);

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

_commander2.default.version(_package2.default.version).option('-v, --version', 'version');
// .parse(process.argv)

_commander2.default.command('generate-posts').alias('gp').description('save post to file with type from folder').option('-t, --type [type]', 'posts status draft|other').option('-p, --path [path]', 'path /relative/path').option('-d, --destination [destination]', 'folder to save result').action(function (options) {
  var dir = process.cwd();
  if (process.env.ROOT) {
    dir = process.env.ROOT.replace(/\/$/, '');
  }
  process.env.DEBUG = 'generate-posts:*';
  var generateArgs = ['--harmony', __dirname + '/cli/process/generate-posts.js', 'ABE_WEBSITE=' + dir];
  if (options.destination != null) {
    generateArgs.push('ABE_DESTINATION=' + options.destination);
  }
  if (options.path != null) {
    generateArgs.push('ABE_PATH=' + options.path);
  }
  if (options.type != null) {
    generateArgs.push('ABE_STATUS=' + options.type);
  }

  var generate;
  if (__dirname.indexOf('dist') > -1) {
    generate = (0, _child_process.spawn)('node', generateArgs, { shell: true, stdio: 'inherit' });
  } else {
    generate = (0, _child_process.spawn)(_path2.default.join(__dirname, '..', 'node_modules', '.bin', 'babel-node'), generateArgs, { shell: true, stdio: 'inherit' });
  }

  generate.on('close', function (code) {
    console.log('child process exited with code ' + code);
    process.exit(0);
  });
}).on('--help', function () {
  console.log('  Examples:');
  console.log();
  console.log('    $ abe generate-posts --path /test --destination result --status publish');
  console.log();
});

_commander2.default.command('create [path]').alias('c').description('create new abe project').action(function (dest) {
  dest = dest != null ? dest : '';
  var dir = _path2.default.join(process.cwd(), dest);
  if (process.env.ROOT) {
    dir = _path2.default.join(process.env.ROOT, dest);
  }
  var create = new _Create2.default();
  if (typeof dir !== 'undefined' && dir !== null) {
    create.init(dir);
  } else {
    console.error('Error: no project path specified');
  }
}).on('--help', function () {
  console.log('  Examples:');
  console.log();
  console.log('    $ abe create');
  console.log('    $ abe create [destination]');
  console.log();
});

_commander2.default.command('serve').alias('s').description('create http server for abe').option('-p, --port [number]', 'change port of the web server').option('-i, --interactive', 'open browser on web server startup').action(function (options) {
  var dir = process.cwd();
  if (process.env.ROOT) {
    dir = process.env.ROOT;
  }
  var environment = process.env;
  environment.ROOT = dir;
  if (options.port != null) {
    environment.PORT = options.port;
  }
  var command;
  if (__dirname.indexOf('dist') > -1) {
    command = 'node --harmony ./dist/server/index.js';
  } else {
    command = _path2.default.join(__dirname, '..', 'node_modules', '.bin', 'babel-node') + ' --harmony ./src/server/index.js';
  }
  if (options.interactive != null) {
    command = 'OPENURL=1 ' + command;
  }
  process.chdir(__dirname + '/../');
  console.log('website started : ' + dir);
  var cp = (0, _child_process.exec)(command, { env: environment }, function (err, out, code) {
    if (err instanceof Error) throw err;
    process.stderr.write(err);
    process.stdout.write(out);
    process.exit(code);
  });
  cp.stderr.pipe(process.stderr);
  cp.stdout.pipe(process.stdout);
}).on('--help', function () {
  console.log('  Examples:');
  console.log();
  console.log('    $ abe serve');
  console.log();
});

_commander2.default.command('install [plugin]').alias('i').description('install abe plugin(s)').action(function (plugin) {
  var dir = process.cwd();
  if (process.env.ROOT) {
    dir = process.env.ROOT.replace(/\/$/, '');
  }

  if (typeof plugin !== 'undefined' && plugin !== null) {
    _plugins2.default.instance.install(dir, plugin);
  } else {
    _plugins2.default.instance.install(dir);
  }
}).on('--help', function () {
  console.log('  Examples:');
  console.log();
  console.log('    $ abe install');
  console.log('    $ abe install [plugin]');
  console.log();
});

_commander2.default.command('uninstall <plugin>').alias('un').description('uninstall abe plugin(s)').action(function (plugin) {
  var dir = process.env.ROOT.replace(/\/$/, '');
  if (process.env.ROOT) {
    dir = process.env.ROOT.replace(/\/$/, '');
  }

  if (typeof plugin !== 'undefined' && plugin !== null) {
    _plugins2.default.instance.uninstall(dir, plugin);
  }
}).on('--help', function () {
  console.log('  Examples:');
  console.log();
  console.log('    $ abe uninstall');
  console.log('    $ abe uninstall [plugin]');
  console.log();
});

_commander2.default.parse(process.argv);