#!/usr/bin/env node
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Commander = require("commander");
const engine_1 = require("./engine");
const Case = __importStar(require("../../case/src"));
const Utils = __importStar(require("../../util/src"));
const Path = __importStar(require("path"));
const Connect = __importStar(require("../../connect/src"));
const Request = __importStar(require("request"));
const UrlEncode = __importStar(require("urlencode"));
const pkg = require('../../../package.json');
Commander.version(`${pkg.version}`);
Commander
    .command('build')
    .description('编译工程')
    .option('-p, --path <v>', '目标工程路径')
    .option('-d, --dist <v>', '编译结果路径')
    .option('-a, --all <v>', '批量将同一文件夹下的所有Cube有效工程进行编译，编译结果将存放到每个工程的dist目录下')
    .action(cmd => {
    Utils.Console.info(`编译工程
        工程路径: ${cmd.path}
        结果路径: ${cmd.dist}
        批量工程: ${cmd.all}
        `);
    if (cmd.all) {
        engine_1.AntCube.pack.buildAllProjects(cmd.all).then(value => {
            Utils.Console.info(`编译完成`);
            process.exit(0);
        }).catch(reason => {
            Utils.Console.error(`${reason}`);
            process.exit(1);
        });
    }
    else {
        const dist = cmd.dist ? cmd.dist : Path.join(cmd.path, '/dist');
        engine_1.AntCube.pack.buildProject(cmd.path, dist).then(value => {
            if (value.success) {
                Utils.Console.info(`编译完成，编译产物路径 [${dist}]`);
                process.exit(0);
            }
            else {
                Utils.Console.error(`${value.message}`);
                process.exit(1);
            }
        }).catch(reason => {
            Utils.Console.error(`build project failure\n${reason.message}`);
            process.exit(1);
        });
    }
});
Commander
    .command('server')
    .description('启动通信服务器')
    .action(cmd => {
    Connect.launch().then(value => {
        Utils.Console.info(`服务启动完毕，请勿关闭终端窗口`);
        Connect.Router.launch();
    }).catch(reason => {
        Utils.Console.error(`服务启动异常 [${reason}]`);
        process.exit(1);
    });
});
Commander
    .command('qrcode')
    .description('显示可供连接的二维码')
    .action(cmd => {
    Request.get(`http://${Connect.Singleton.IP}:${Connect.Singleton.PORT}/api?action=qrcode`, (error, response, body) => {
        if (error) {
            Utils.Console.error(`${error}`);
        }
        else {
            Utils.Console.info(`\n${body}`);
        }
        process.exit(0);
    });
});
Commander
    .command('device')
    .description('显示当前连接的设备列表')
    .action(cmd => {
    Request.get(`http://${Connect.Singleton.IP}:${Connect.Singleton.PORT}/api?action=devices`, (error, response, body) => {
        if (error) {
            Utils.Console.error(`${error}`);
        }
        else {
            const sessions = body.length > 0 ? JSON.parse(body) : [];
            const names = new Array();
            if (sessions.length > 0) {
                for (const { name: n } of sessions) {
                    names.push(n);
                }
            }
            Utils.Console.info(`\n${names.length > 0 ? names.join('\n') : '无连接设备'}`);
        }
        process.exit(0);
    });
});
Commander
    .command('preview')
    .description('预览指定工程')
    .option('-p, --path <v>', '待预览工程的根目录（包含 package.json 文件的目录）')
    .action(cmd => {
    Request.get(`http://${Connect.Singleton.IP}:${Connect.Singleton.PORT}/api?action=preview&path=${UrlEncode.encode(Path.resolve('./', cmd.path))}`, (error, response, body) => {
        if (error) {
            Utils.Console.error(`${error}`);
        }
        process.exit(0);
    });
});
Commander
    .command('caseserver')
    .description('启动用例server')
    .action(cmd => {
    Case.Build.ftp.run().then(value => {
        Utils.Console.info(`服务启动完毕，请勿关闭终端窗口`);
    }).catch(reason => {
        Utils.Console.error(`服务启动异常 [${reason}]`);
        process.exit(1);
    });
});
Commander
    .command('runcase')
    .description('执行用例')
    .option('-c, --case <v>', 'case所在工程根目录，支持将多个工程放置在同一个目录下批量处理')
    .option('-d, --device <v>', '运行case的设备ID')
    .action(cmd => {
    Utils.Console.info(`***********
    用例工程(必要) : ${cmd.case}
    设备ID(必要) : ${cmd.device}
        `);
    if (cmd.case && cmd.device) {
        Case.iOS.run(cmd.case, cmd.device).then(value => {
            Utils.Console.info(`用例执行完成`);
            process.exit(0);
        }).catch(reason => {
            Utils.Console.error(`用例执行失败 : ${reason}`);
            process.exit(2);
        });
    }
    else {
        Utils.Console.error(`缺少必要参数`);
        process.exit(1);
    }
});
Commander
    .command('buildcase')
    .description('构建用例')
    .option('-p, --path <v>', 'case所在工程根目录，支持将多个工程放置在同一个目录下批量处理')
    .action(cmd => {
    Utils.Console.info(`***********
    用例工程(必要) : ${cmd.path}
        `);
    if (!cmd.path) {
        Utils.Console.error(`缺少必要参数`);
        process.exit(2);
    }
    Case.Build.allCases(cmd.path).then(value => {
        Utils.Console.info(`用例构造完成`);
        process.exit(0);
    }).catch(reason => {
        Utils.Console.error(`用例构建失败 : ${reason}`);
        process.exit(1);
    });
});
Commander
    .command('pickcase')
    .description('提取用例执行结果到vscode，使用期需先连接vscode')
    .option('-d, --device <v>', '运行case的设备ID')
    .action(cmd => {
    if (!cmd.device) {
        Utils.Console.error(`缺少必要参数`);
        process.exit(1);
    }
    Case.iOS.pick(cmd.device).then(value => {
        Utils.Console.info(`已发送提取指令`);
        process.exit(0);
    }).catch(reason => {
        Utils.Console.error(`提取结果失败 : ${reason}`);
        process.exit(2);
    });
});
Commander
    .command('storebaseline')
    .description('更新store')
    .option('-p, --path <v>', '基线根路径')
    .action(cmd => {
    if (cmd.path) {
        Utils.Console.info(`开始更新基线 [${cmd.path}]`);
        Case.Store.updateBaseline(cmd.path).then(value => {
            Utils.Console.info(`更新完成`);
            process.exit(0);
        }).catch(reason => {
            Utils.Console.error(`更新失败 : ${reason}`);
            process.exit(2);
        });
    }
    else {
        Utils.Console.error(`缺少必要参数`);
        process.exit(1);
    }
});
Commander
    .command('storecheck')
    .description('校验特征')
    .option('-p, --path <v>', '基线根路径')
    .action(cmd => {
    if (cmd.path) {
        Utils.Console.info(`开始校验 [${cmd.path}]`);
        Case.check(cmd.path).then((value) => __awaiter(void 0, void 0, void 0, function* () {
            Utils.Console.info(`校验完成`);
            yield Case.report(value);
            process.exit(0);
        })).catch(reason => {
            Utils.Console.error(`校验失败 : ${reason}`);
            process.exit(2);
        });
    }
    else {
        Utils.Console.error(`缺少必要参数`);
        process.exit(1);
    }
});
Commander
    .command('tinysplit')
    .description('拆分用例')
    .option('-p, --path <v>', '小程序工程根路径')
    .action(cmd => {
    if (cmd.path) {
        Utils.Console.info(`开始拆分 [${cmd.path}]`);
        Case.TinySuit.split(cmd.path).then((value) => __awaiter(void 0, void 0, void 0, function* () {
            Utils.Console.info(`拆分完成`);
            process.exit(0);
        })).catch(reason => {
            Utils.Console.error(`拆分失败 : ${reason}`);
            process.exit(2);
        });
    }
    else {
        Utils.Console.error(`缺少必要参数`);
        process.exit(1);
    }
});
Commander.parse(process.argv);
process.on("beforeExit", e => {
    console.log('beforeExit');
    Utils.Net.cleanPort(Connect.Singleton.PORT);
});
process.on("SIGABRT", e => {
    console.log('SIGABRT');
    Utils.Net.cleanPort(Connect.Singleton.PORT);
});
process.on("SIGINT", e => {
    console.log('SIGINT');
    Utils.Net.cleanPort(Connect.Singleton.PORT);
});
process.on("uncaughtException", e => {
    console.log('uncaughtException');
    Utils.Net.cleanPort(Connect.Singleton.PORT);
});
//# sourceMappingURL=cli.js.map