#!/usr/bin/env node
require('../lib/utils/logger')
const program = require('commander')
const { initProject } = require('../lib/command/init')
const { install } = require('../lib/command/install')
const { bundle } = require('../lib/command/bundle')
const { injection } = require('../lib/command/injection')
const { deploy } = require('../lib/command/deploy')
const { cs, lg } = require('../lib/command/npm')
const { test } = require('../lib/command/test')
const { upload } = require('../lib/command/upload')
const { initNpmProject } = require('../lib/command/npm/init')
const { npmPublish } = require('../lib/command/npm/publish.js')
const appInfo = require('../package.json')

// Commands { name, alias, action, desc, help }
const commands = [{
    name: 'init <projectName>',
    desc: 'Initialize a new xUPD project named <projectName> in a directory of the same name.',
    options: [
        { flag: '--skip-install', defaultValue: false },
    ],
    action: initProject,
}, {
    name: 'install',
    options: [
        { flag: '--silent', defaultValue: false },
    ],
    desc: 'Install and link native dependencies',
    action: install,
}, {
    name: 'bundle',
    desc: 'Builds the javascript bundle for offline use',
    options: [
        { flag: '-p, --platform <platform>', desc: 'Either "iOS" or "Android" or "All"', defaultValue: 'All' },
        { flag: '--reset-cache', desc: 'Removes cached files', defaultValue: false },
    ],
    action: bundle,
}, {
    name: 'bundle-ios',
    desc: 'Short for bundle with option "--platform iOS"',
    action: bundle.bind(bundle, { platform: 'iOS' }),
}, {
    name: 'bundle-android',
    desc: 'Short for bundle with option "--platform Android"',
    action: bundle.bind(bundle, { platform: 'Android' }),
}, {
    name: 'deploy',
    desc: 'Deploy javascript bundle to Cocopods for iOS, and deploy to maven for Android.',
    options: [
        { flag: '-p, --platform <platform>', desc: 'Either "iOS" or "Android" or "All"', defaultValue: 'All' },
    ],
    action: deploy,
}, {
    name: 'deploy-ios',
    desc: 'Short for deploy with option "--platform iOS"',
    action: deploy.bind(bundle, { platform: 'iOS' }),
}, {
    name: 'deploy-android',
    desc: 'Short for deploy with option "--platform Android"',
    action: deploy.bind(bundle, { platform: 'Android' }),
}, {
    name: 'test',
    desc: 'An api for test',
    action: test,
},{
    name: 'injection <modules>',
    desc: 'Injection module pages for project. And option "-r" will injection a root entrance for project.',
    options:[
        { flag: '-r, --root', desc: 'Injection a root module as entrance', defaultValue:false},
    ],
    action: injection
}, {
    name: 'upload <modules>',
    desc: 'zip and upload pages at src/pages',
    action: upload,
}, {
    name: 'npm',
    desc: 'change npm source',
    options: [
        { flag: '-t, --type <type>', desc: '"tb" change to https://registry.npm.taobao.org;\n "org" change to https://registry.npmjs.org; \n "vv" change to http://verdaccio.vvtechnology.cn:4873/', defaultValue: 'vv' }
    ],
    action: cs.changeNpmSource,
}, {
    name: 'yarn',
    desc: 'change yarn source',
    options: [
        { flag: '-t, --type <type>', desc: '"tb" change to https://registry.npm.taobao.org;\n "org" change to https://registry.npmjs.org; \n "vv" change to http://verdaccio.vvtechnology.cn:4873/', defaultValue: 'vv' },
    ],
    action: cs.changeYarnSource,
}, {
    name: 'npminit <projectName>',
    desc: 'Initialize a new xUPD npm components project named <projectName> in a directory of the same name.',
    options: [
        { flag: '--skip-install', defaultValue: false }
    ],
    action: initNpmProject
}, {
    name: 'npmlogin',
    desc: 'login npm for diffient source',
    options: [
        { flag: '-t, --type <type>', desc: '"tb" login to https://registry.npm.taobao.org;\n "org" login to https://registry.npmjs.org; \n "vv" login to http://verdaccio.vvtechnology.cn:4873/', defaultValue: 'vv' }
    ],
    action: lg.npmlogin,
}, {
    name: 'npmpub',
    desc: 'publish components ',
    options: [
        { flag: '-t, --type <type>', desc: '"tb" publish to https://registry.npm.taobao.org;\n "org" login to https://registry.npmjs.org; \n "vv" login to http://verdaccio.vvtechnology.cn:4873/', defaultValue: 'vv' }
    ],
    action: npmPublish
}]

// 版本
program.version(appInfo.version)

commands.forEach(({ name, alias, options, action, desc, help }) => {
    let command = program.command(name)
    alias && command.alias(alias)
    desc && command.description(desc)

    options && options.forEach(option => {
        command.option(option.flag, option.desc, option.defaultValue)
    })

    command.action(action)

    help && command.on('--help', () => {
        if (typeof help === 'string') {
            console.log(help)
        } else {
            help(name.split(' ')[0])
        }
    })
})

program.parse(process.argv)
