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

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator"));

var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));

var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));

var _yargs = _interopRequireDefault(require("yargs"));

var _requireDirAll = _interopRequireDefault(require("require-dir-all"));

var _chalk = _interopRequireDefault(require("chalk"));

var _banner = _interopRequireDefault(require("./banner"));

var _reporter = _interopRequireDefault(require("./tasks/utils/reporter"));

console.clear();
console.log(_banner.default);
const portOption = {
  default: 9000,
  describe: 'Run on specific port'
};
const forceOption = {
  default: false,
  describe: 'Run even though it does not meet minumum requirements, useful for CI environment',
  type: 'boolean'
};
const linkOptions = {
  path: {
    default: '../mono-cosmic',
    describe: 'Path to the cosmic folder'
  }
};
const distTagOptions = {
  describe: 'Custom npm dist tag'
};
const localsOptions = {
  describe: 'List of allowed moment locals ',
  type: 'array'
};
const i18nOptions = {
  default: null,
  describe: 'Whether or not to copy i18n resource bundles to use for i18n backend'
};
const customScriptOptions = {
  default: null,
  describe: 'Name of a custom script to run first (should be in package.json of the app you are building)'
};

const argv = _yargs.default.usage('dr <command> [args]').command('dev [port]', 'Run development build with HMR enabled', cm => {
  cm.positional('port', portOption);
  cm.option('theme', {
    default: null,
    describe: 'The theme name to build the app with.'
  });
  cm.option('i18n', i18nOptions);
  cm.option('force', forceOption);
  cm.option('customScript', customScriptOptions);
}).command('prod [port]', 'Run production build (minified), good for testing in different browsers', cm => {
  cm.positional('port', portOption);
  cm.option('forceOption', forceOption);
  cm.option('theme', {
    default: null,
    describe: 'The theme name to build the app with.'
  });
  cm.option('locals', localsOptions);
}).command('build', 'Build/transpile the code, if used inside an application it will create an artifact otherwise it will transpile the package into a lib folder', cm => {
  cm.option('force', forceOption);
  cm.option('theme', {
    default: null,
    describe: 'The theme name to build the package with. Will alter the pushed package name.'
  });
  cm.option('i18n', i18nOptions);
  cm.option('customScript', customScriptOptions);
  cm.option('windowVersion', {
    default: null,
    describe: 'The version to use for the window.VERSION console value for the built artifact'
  });
  cm.option('apiKey', {
    default: null,
    describe: 'The API key to use to get latest package version from Artifactory/nuget'
  });
  cm.option('latestCosmic', {
    default: false,
    describe: 'Set resolutions to latest alpha releases of cosmic packages for dev builds'
  });
  cm.option('locals', localsOptions);
  cm.option('noYarn', {
    default: false,
    describe: 'Dont do a yarn install before build. Defaults to false'
  });
  cm.option('docs', {
    default: false,
    describe: 'Create documentation for the repository and all related packages'
  });
  cm.option('removeDataTestIdProps', {
    default: true,
    describe: 'Set whether data-test-id properties should be removed for build.'
  });
}).command('releasePackages', 'Publish monorepo packages to npm using lerna', cm => {
  cm.option('force', forceOption);
  cm.option('forcePublish', {
    default: null,
    describe: 'Force publish for the specified packages (comma-separated) or all packages using \'all\'.'
  });
}).command('releaseApp', 'Publish app to both npm and octo', cm => {
  cm.option('force', forceOption);
  cm.option('npm', {
    default: false,
    describe: 'Publish npm package'
  });
  cm.option('nuget', {
    default: false,
    describe: 'Publish nuget package'
  });
  cm.option('nugetPackageName', {
    default: false,
    describe: 'To override nuget package name'
  });
  cm.option('apiKey', {
    default: null,
    describe: 'The API key to use to  get latest package version and publish to Artifactory/nuget'
  });
  cm.option('theme', {
    default: null,
    describe: 'The theme name to build the package with. Will alter the pushed package name.'
  });
  cm.option('i18n', i18nOptions);
  cm.option('customScript', customScriptOptions);
  cm.option('latestCosmic', {
    default: false,
    describe: 'Set resolutions to latest alpha releases of cosmic packages for dev releases'
  });
  cm.option('tag', distTagOptions);
  cm.option('locals', localsOptions);
  cm.option('noYarn', {
    default: false,
    describe: 'Dont do a yarn install before build. Defaults to false'
  });
  cm.option('docs', {
    default: false,
    describe: 'Create documentation for the repository and all related packages'
  });
  cm.option('removeDataTestIdProps', {
    default: true,
    describe: 'Set whether data-test-id properties should be removed for build.'
  });
  cm.option('allowRelease', {
    default: false,
    describe: 'Allow building packages with tags from the release branch'
  });
}).command('init', 'Initial setup of a mono repo for development (or yarn install for non-mono-repos). Optionally links local and links apps', cm => {
  cm.option('link', {
    default: true,
    describe: 'Run lerna link --force-local'
  });
  cm.option('freeze', {
    default: false,
    describe: 'Run lerna bootstrap --production (yarn install --frozen-lockfile)'
  });
}).command('buildInfo', 'Display build compatibility info table').command('appConfig', 'Update the cosmic config for a running app').command('appI18n', 'Copy i18n resource bundles from package folders to the app folder for i18n backend').command('dep', 'Create dependency graph').command('clean', 'Remove all node_modules, both from root and individual package. Also from any apps in apps folder if using workspaces').command('link [path]', 'Link all cosmic packages to current app, used for development', cm => {
  cm.positional('path', linkOptions.path);
}).command('unlink', 'Unlink all cosmic packages from current app').command('updatePackages [npmTag]', 'Set fixed version of package based of npm tag. Useful before releasing an app', cm => {
  cm.positional('npmTag', {
    default: 'latest',
    describe: 'npmTag name to get the version number from'
  });
}).command('doc', 'Generates repository documentation for packages', cm => {
  cm.option('repoPath', {
    describe: 'Supply repo root directory absolute path if current working directory is not the repo root directory'
  });
  cm.option('force', forceOption);
}).command('serve [port] [root]', 'Run application that has already been built into the _dist folder', cm => {
  cm.positional('port', portOption);
  cm.positional('root', {
    default: '_dist',
    describe: 'Where the built application code is, default is _dist'
  });
  cm.option('forceOption', forceOption);
}).help().version().argv;

const args = argv._;
const tasks = (0, _requireDirAll.default)('./tasks');

if (args.length === 0) {
  // show available tasks
  console.log('');
  console.log(_chalk.default.dim('Tasks available:'), _chalk.default.green((0, _keys.default)(tasks).join(', ')));
  console.log(_chalk.default.dim('Run \'dr --help\' for more info'));
  process.exit(0); // eslint-disable-line
}

const taskName = args[0];
const taskFn = tasks[taskName]; // const taskArgs = args.slice(1)

if (tasks.hasOwnProperty(args[0])) {
  (0, _asyncToGenerator2.default)(function* () {
    try {
      _promise.default.resolve(taskFn(argv)).then(task => task((0, _reporter.default)(taskName))());
    } catch (e) {
      console.log(e);
      process.exit(1);
      throw e;
    }
  })();
} else {
  console.log('No task found called:', taskName);
}