"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// import fs from 'fs-extra';
const chalk_1 = __importDefault(require("chalk"));
const commander_1 = require("commander");
const didyoumean_1 = __importDefault(require("didyoumean"));
const createLibrary_1 = __importDefault(require("./lib/createLibrary"));
const buildLibrary_1 = require("./lib/buildLibrary");
const createMaterial_1 = require("./lib/createMaterial");
const packageJson = require('../package.json');
const program = new commander_1.Command()
    .version(packageJson.version)
    // .arguments('<project-directory>')
    .usage(`${chalk_1.default.green('<command>')} [options]`)
    // .action(name => {
    //   projectName = name;
    // })
    // .option('--verbose', 'print additional logs')
    // .option('--typescript', 'code with typescript')
    .allowUnknownOption();
program
    .command('dev [entry]')
    .description('开启调试服务')
    .option('-o, --open', '自动打开浏览器')
    .action((name, cmd) => {
    const options = cleanArgs(cmd);
    buildLibrary_1.devServer(name, options);
});
program
    .command('dist [entry]')
    .description('构建素材库')
    .action((name, cmd) => {
    const options = cleanArgs(cmd);
    buildLibrary_1.build(name, options);
});
program
    .command('create-lib <name>')
    .description('新建素材库')
    .option('-a, --author <authorName>', 'Author of this materials lib')
    .option('-r, --registry <registry>', 'Template registry（npm | cnpm | tnpm）')
    .option('-t, --typescript', '是否需要支持ts')
    .action((name, cmd) => {
    const options = cleanArgs(cmd);
    createLibrary_1.default(name, options);
});
program
    .command('create-component <name>')
    .description('新建组件')
    .option('-a, --author <authorName>', 'Author of this component')
    .option('-r, --registry <registry>', 'Template registry（npm | cnpm | tnpm）')
    .action((name, cmd) => {
    const options = cleanArgs(cmd);
    createMaterial_1.createComponent(name, options);
});
program
    .command('create-plugin <name>')
    .description('新建插件')
    .option('-a, --author <authorName>', 'Author of this plugin')
    .option('-r, --registry <registry>', 'Template registry（npm | cnpm | tnpm）')
    .action((name, cmd) => {
    const options = cleanArgs(cmd);
    createMaterial_1.createPlugin(name, options);
});
program
    .command('create-action <name>')
    .description('新建动作')
    .option('-a, --author <authorName>', 'Author of this action')
    .option('-r, --registry <registry>', 'Template registry（npm | cnpm | tnpm）')
    .action((name, cmd) => {
    const options = cleanArgs(cmd);
    createMaterial_1.createAction(name, options);
});
program.arguments('<command>').action((cmd) => {
    program.outputHelp();
    console.log(`  ${chalk_1.default.red(`Unknown command ${chalk_1.default.yellow(cmd)}.`)}`);
    console.log();
});
program.on('--help', () => {
    console.log();
    console.log(`  运行 ${chalk_1.default.cyan('vision <command> --help')} 查看相关命令的详细信息`);
    console.log();
});
program.commands.forEach((c) => c.on('--help', () => console.log()));
program.parse(process.argv);
if (!process.argv.slice(2).length) {
    program.outputHelp();
}
function suggestCommands(cmd) {
    const availableCommands = program.commands.map((cmd) => {
        return cmd._name;
    });
    const suggestion = didyoumean_1.default(cmd, availableCommands);
    if (suggestion) {
        console.log(`  ${chalk_1.default.red(`Did you mean ${chalk_1.default.yellow(suggestion.toString())}?`)}`);
    }
}
function camelize(str) {
    return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''));
}
// commander passes the Command object itself as options,
// extract only actual options into a fresh object.
function cleanArgs(cmd) {
    const args = {};
    cmd.options.forEach((o) => {
        const key = camelize(o.long.replace(/^--/, ''));
        // if an option is not present and Command has a method with the same name
        // it should not be copied
        if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') {
            args[key] = cmd[key];
        }
    });
    return args;
}
//# sourceMappingURL=index.js.map