import program from 'commander'
import pkg from '../package.json'
import pkgUpdate from '../scripts/util/pkg-update'
import taskInit from '../scripts/task/init'
import taskWebpack from '../scripts/task/webpack'
import taskPre from '../scripts/task/pre'
import taskClean from '../scripts/task/clean'
import taskPack from '../scripts/task/pack'
import taskUpload from '../scripts/task/upload'
import taskAsset from '../scripts/task/asset'
import taskImage from '../scripts/task/image'
import taskBabelAsset from '../scripts/task/babel-asset'
import taskServeClient from '../scripts/task/serve-client'
import taskPostcss from '../scripts/task/postcss'

pkgUpdate()

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

function keyMapTask(key) {
  switch (key) {
    case 'init':
      return taskInit
    case 'pre':
      return taskPre
    case 'webpack':
      return taskWebpack
    case 'clean':
      return taskClean
    case 'upload':
      return taskClean
    case 'pack':
      return taskPack
    case 'asset':
      return taskAsset
    case 'image':
      return taskImage
    case 'babel-asset':
      return taskBabelAsset
    case 'serve-client':
      return taskServeClient
    case 'postcss':
      return taskPostcss
    default:
  }
}

program.version(pkg.version)

// 初始化任务
// 将官方模板下载到本目录
program
  .command('init')
  .action(() => {
    taskInit()
  })

// webpack 任务
// 根据环境定义取不同的 config 值
program
  .command('webpack')
  .description('webpack files')
  .option('-e, --node_env [env]', 'define NODE_ENV, a string should be "development", "dev", "experiment", "exp", "production" or "prod"')
  .action((options) => {
    taskWebpack(options['node_env'])
  })

// pre 任务
program
  .command('pre')
  .description('remove tmp folders, make dist folders')
  .action(() => {
    taskPre()
  })

// clean 任务
program
  .command('clean')
  .description('remove all tmp folders')
  .action(() => {
    taskClean()
  })

// pack 任务
program
  .command('pack')
  .description('pack dist files')
  .action(() => {
    taskPack()
  })

// upload 任务
program
  .command('upload')
  .description('upload dist files to remote server')
  .action(() => {
    taskUpload()
  })

// 拷贝 asset 任务
program
  .command('asset')
  .description('ncp asset to dist')
  .action(() => {
    taskAsset()
  })

// image 任务
program
  .command('image')
  .description('ncp image to dist')
  .action(() => {
    taskImage()
  })

// babel-asset 任务，将 client/asset 中 *.bl.js 文件通过 babel 进行转换
// 根据环境定义取不同的 config 值
program
  .command('babel-asset')
  .description('use babel to compile js files those end with .bl.js in asset folder')
  .option('-e, --node_env [env]', 'define NODE_ENV, a string should be "development", "dev", "experiment", "exp", "production" or "prod"')
  .action((options) => {
    taskBabelAsset(options['node_env'])
  })

// serve-client 任务，启动静态资源服务器
// 根据环境定义取不同的 config 值
program
  .command('serve-client')
  .description('serve static files')
  .option('-e, --node_env [env]', 'define NODE_ENV, a string should be "development", "dev", "experiment", "exp", "production" or "prod"')
  .action((options) => {
    taskServeClient(options['node_env'])
  })

// postcss 任务
// 根据环境定义取不同的 config 值
program
  .command('postcss')
  .description('compile sass-like stylesheet files')
  .option('-e, --node_env [env]', 'define NODE_ENV, a string should be "development", "dev", "experiment", "exp", "production" or "prod"')
  .action((options) => {
    taskPostcss(options['node_env'])
  })

program
  .option('-t, --tasks <items>', 'list tasks to exec', list)
  .option('-e, --node_env [env]', 'define NODE_ENV, a string should be "development", "dev", "experiment", "exp", "production" or "prod"')

program.parse(process.argv)

if (program.tasks) {
  let taskChain = program.tasks
  let env = program['node_env']
  let idx = 0
  let exec = () => {
    return keyMapTask(taskChain[idx])(env)
      .then(() => {
        idx += 1
        if (idx < taskChain.length) {
          exec()
        }
      })
      .catch((err) => {
        throw new Error(err)
      })
  }
  exec()
}

@@ -1467,147 +1467,8 @@
 s')%0A
-  .option('-e, --node_env %5Benv%5D', 'define NODE_ENV, a string should be %22development%22, %22dev%22, %22experiment%22, %22exp%22, %22production%22 or %22prod%22')%0A
   .a
@@ -1466,39 +1466,32 @@
 es')%0A  .action((
-options
 ) =%3E %7B%0A    taskW
@@ -1497,23 +1497,23 @@
 Webpack(
-options
+program
 %5B'node_e
@@ -2463,147 +2463,8 @@
 r')%0A
-  .option('-e, --node_env %5Benv%5D', 'define NODE_ENV, a string should be %22development%22, %22dev%22, %22experiment%22, %22exp%22, %22production%22 or %22prod%22')%0A
   .a
@@ -2462,39 +2462,32 @@
 er')%0A  .action((
-options
 ) =%3E %7B%0A    taskB
@@ -2496,23 +2496,23 @@
 elAsset(
-options
+program
 %5B'node_e
@@ -2651,147 +2651,8 @@
 s')%0A
-  .option('-e, --node_env %5Benv%5D', 'define NODE_ENV, a string should be %22development%22, %22dev%22, %22experiment%22, %22exp%22, %22production%22 or %22prod%22')%0A
   .a
@@ -2650,39 +2650,32 @@
 es')%0A  .action((
-options
 ) =%3E %7B%0A    taskS
@@ -2685,23 +2685,23 @@
 eClient(
-options
+program
 %5B'node_e
@@ -2836,147 +2836,8 @@
 s')%0A
-  .option('-e, --node_env %5Benv%5D', 'define NODE_ENV, a string should be %22development%22, %22dev%22, %22experiment%22, %22exp%22, %22production%22 or %22prod%22')%0A
   .a
@@ -2843,23 +2843,16 @@
 action((
-options
 ) =%3E %7B%0A 
@@ -2870,15 +2870,15 @@
 css(
-options
+program
 %5B'no

fix node_env for single task