"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const Utils = require("../../util/src");
const Path = require("path");
const FS = require("fs");
const ChildProcess = require("child_process");
var iOS;
(function (iOS) {
    function pick(device) {
        Utils.Console.info(`发送提取指令`);
        spawn('xcrun', ['simctl', 'openurl', device, `rubik:/test/pick`]);
        return Promise.resolve();
    }
    iOS.pick = pick;
    function run(casepath, mode, device, tiny = false) {
        return __awaiter(this, void 0, void 0, function* () {
            const cost = new Utils.Cost();
            const cases = new Array();
            if (tiny) {
                const subpaths = FS.readdirSync(casepath);
                for (const name of subpaths) {
                    if (FS.statSync(Path.join(casepath, name)).isFile() && Path.extname(name) === '.rcase') {
                        cases.push(JSON.parse(FS.readFileSync(Path.join(casepath, name)).toString()));
                    }
                }
            }
            else {
                Utils.FS.walk(casepath, (root, name, stats) => {
                    if (stats.isFile() && Path.extname(name) === '.rcase') {
                        cases.push(JSON.parse(FS.readFileSync(Path.join(root, name)).toString()));
                    }
                });
            }
            Utils.Console.info(`提取用例信息(${cases.length})个`);
            let index = 1;
            for (const acase of cases) {
                Utils.Console.info(`执行用例 ${acase.id} [${index}/${cases.length}]`);
                spawn('xcrun', ['simctl', 'terminate', device, 'com.cube.demo']);
                spawn('xcrun', ['simctl', 'launch', device, 'com.cube.demo']);
                const output = mode & Mode.Output ? '&output=true' : '';
                const portrait = mode & Mode.Portrait ? '&portrait=true' : '';
                spawn('xcrun', ['simctl', 'openurl', device, `rubik:/test?caseId=${acase.id}&source=rubik${output}${portrait}`]);
                const duration = 5000;
                Utils.Console.info(`等待用例执行(${duration / 1000}秒)...`);
                yield sleep(duration);
                spawn('xcrun', ['simctl', 'terminate', device, 'com.cube.demo']);
                index++;
            }
            cost.mark();
            return Promise.resolve();
        });
    }
    iOS.run = run;
})(iOS = exports.iOS || (exports.iOS = {}));
function spawn(cmd, args) {
    Utils.Console.info(`执行指令 : ${cmd} ${args ? args : ''}`);
    const result = ChildProcess.spawnSync(cmd, args);
    if (result.status !== 0) {
        Utils.Console.error(`  ${result.stderr.toString()}`);
    }
    else {
        if (result.stdout.toString().length > 0) {
            Utils.Console.info(`  ${result.stdout.toString()}`);
        }
    }
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
var Mode;
(function (Mode) {
    Mode[Mode["None"] = 0] = "None";
    Mode[Mode["Output"] = 1] = "Output";
    Mode[Mode["Portrait"] = 2] = "Portrait";
})(Mode = exports.Mode || (exports.Mode = {}));
//# sourceMappingURL=run.js.map