#!/usr/bin/env node
import program from 'commander'
import inquirer from 'inquirer'
import chalk from 'chalk'
import { InterfaceLog } from '../utils/log'
const version = require('../../package.json').engines.node
const { checkVersion } = require('../utils')
const interfaceLog = new InterfaceLog('file', 'error', false)
// 检查版本
checkVersion(version)
//  注册命令
program
  .version(require('../../package.json').version)
  .usage('<command> [options]')

program
  .command('init <page-type>')
  .description('创建新项目结构，可以选择前台consumer或者后台admin')
  .option('-t, --type <pagetype>', '结构类型参数')
  .action((name: string, cmd: any) => {
    if (name === 'consumer') {
      console.log(chalk.red('开发中，敬请期待...'))
      return
    }
    const options = cmd
    require('../lib/init')(name, options)
  })
program
  .command('interface')
  .description('根据模块编码生成前端接口调用结构')
  .option('-c, --code <code>', '模块编码')
  .option('-dp, --dataPath <path>', '下载地址')
  .option('-p, --proxy <url>', '代理获取数据地址')
  .action((action: any, options: any) => {
    let { code, dataPath, proxy} = action
    if (!code || !dataPath || !proxy) {
      interfaceLog.error('code、dataPath、proxy为必传项')
      return
    }
    proxy = proxy.replace(/%26/, '&')
    import('../lib/interface').then(res => {
      res['default'](code, proxy, dataPath)
    })
  })
program.parse(process.argv)
