#!/usr/bin/env node

"use strict"

const chalk = require('chalk');
const cmd = require('commander');
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
let config = require('../package.json');
const version = require('../libs/utils/check-version');
const log = require('../libs/utils/log');
const i18n = require('../libs/i18n_json');
const whoami = require('../libs/whoami');

const logo =
`                                                                             
                                                                             
          ___           ___           ___           ___           ___        
         /\\  \\         /\\  \\         /\\  \\         /\\  \\         /\\__\\       
        /::\\  \\       /::\\  \\        \\:\\  \\       /::\\  \\       /:/  /       
       /:/\\:\\  \\     /:/\\:\\  \\        \\:\\  \\     /:/\\:\\  \\     /:/  /        
      /:/  \\:\\__\\   /::\\~\\:\\  \\       /::\\  \\   /::\\~\\:\\  \\   /:/__/  ___    
     /:/__/ \\:|__| /:/\\:\\ \\:\\__\\     /:/\\:\\__\\ /:/\\:\\ \\:\\__\\  |:|  | /\\__\\   
     \\:\\  \\ /:/  / \\/__\\:\\/:/  /    /:/  \\/__/ \\/__\\:\\/:/  /  |:|  |/:/  /   
      \\:\\  /:/  /       \\::/  /    /:/  /           \\::/  /   |:|__/:/  /    
       \\:\\/:/  /        /:/  /     \\/__/            /:/  /     \\::::/__/     
        \\::/__/        /:/  /                      /:/  /       ~~~~         
         ~~            \\/__/                       \\/__/                     
                                                                             
                                                                            

`;

console.log(chalk.cyan(logo));
console.log(i18n.get('hello.npm'));

config = _.merge(config, require('../config'));

let COMMANDMAP = {
  preview: require('../libs/preview'),     //预览
  comInit: require('../libs/init'),        //初始化
  login:   require('../libs/login'),       //登录
  publish: require('../libs/publish'),     //发布
  package: require('../libs/package'),     //打包
  login: require('../libs/login'),         // 登录
  locale: require('../libs/locale'),       // 语言环境
  localeClear: require('../libs/locale_clear') // 清空语言环境
}

function exec(command, ...args) {
  log.debugModeSwitch(args[0].debug);
  COMMANDMAP[command](config, ...args);
}

function list(val) {
  return val.split(',');
}

cmd
  .usage('[options] <folder|file...>')
  .version(config.version)
  .description(i18n.get('datav.title'))
  .option('-v --verbose', 'increase verbosity')
  .option('-E --exclude <items>', 'exclude file or folder', list)

cmd.command('set-key')
  .alias('login')
  .description('set-key with you username and token in the datav.aliyun.com')
  .action((...args) => exec('login', ...args))

cmd.command('init')
  .description('init com for datav-coms')
  .action((...args) => exec('comInit', ...args))

cmd
  .command('start')
  .alias('run')
  .option('-p --port [value]', 'custom server port')
  .option('-s --silent', 'keep silent')
  .option('-l --local', 'use local remote')
  .option('-i --locale [value]', 'language mode, ex: en-US')
  .description('start service for preview component')
  .action((...args) => exec('preview', ...args))

cmd
  .command('publish')
  .alias('pbl')
  .option('--region [value]', 'different component center region, you can --region inner , default is cn')
  .description('publish component')
  .action((...args) => exec('publish', ...args))

cmd
  .command('package')
  .alias('pack')
  .description('package component')
  .action((...args) => exec('package', ...args))

cmd
  .command('locale')
  .description('Set your locale')
  .action((...args) => exec('locale', ...args))

cmd
  .command('locale-clear')
  .alias('lc')
  .description('Clear your locale')
  .action((...args) => exec('localeClear', ...args))

cmd.command('help')
  .description('help')
  .action(() => cmd.help());

cmd.command('latest')
  .description('check ' + config.name + ' latest version')
  .action(() => version(config));

cmd.command('whoami')
  .description('display datav username, datav region')
  .action(whoami);

cmd.version('v' + config.version);

cmd.parse(process.argv);

if (!cmd.args.length) {
  cmd.help();
}

let cacheDir = path.join(config.root, config.cacheDir);
if (!fs.existsSync(cacheDir)) {
  fs.mkdirSync(cacheDir);
}