"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 FS = __importStar(require("fs"));
const Path = __importStar(require("path"));
const Urlencode = __importStar(require("urlencode"));
const Utils = __importStar(require("../../../util/src"));
const Model = __importStar(require("../model"));
function build(path) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!FS.existsSync(Path.join(path, 'dist'))) {
            return Promise.reject(Error(`未找到可提取的cube编译产物`));
        }
        const cases = new Array();
        Utils.Console.info(`提取工程配置 [${Path.join(path, '/dist/app.manifest')}]`);
        const appManifest = JSON.parse(FS.readFileSync(Path.join(path, '/dist/app.manifest')).toString());
        const casePath = Path.join(path, 'dist/_rubik');
        Utils.FS.rmdirs(casePath);
        Utils.FS.mkdirs(casePath);
        let index = 1;
        Utils.FS.walk(Path.join(path, 'dist'), (root, name, stats) => {
            if (Path.extname(name) === '.bin') {
                Utils.Console.info(`构建页面用例 [${root}] [${index}]`);
                let tplManifest = JSON.parse(FS.readFileSync(Path.join(root, 'main.json')).toString());
                tplManifest = JSON.parse(tplManifest.meta);
                const acase = Model.Case.defaultCase();
                acase.poles = Model.Case.Pole.Snapshot;
                acase.type = Model.Case.Type.Falcon;
                acase.subtype = Model.Case.Subtype.OnlyPaintCheck;
                acase.version = tplManifest.version;
                acase.ID = Urlencode.encode(`${acase.type}_${acase.poles}_${appManifest.name}_${tplManifest.name}`);
                //  临时使用scheme字段存储用例源文件路径
                acase.scheme = root;
                cases.push(acase);
                index++;
            }
        });
        // await Promise.all(tasks);
        for (const acase of cases) {
            yield updateProjectResource(casePath, acase, acase.scheme);
        }
        Utils.FS.rmdirs(casePath);
        return Promise.resolve(cases);
    });
}
exports.build = build;
function updateProjectResource(casePath, acase, root) {
    return __awaiter(this, void 0, void 0, function* () {
        Utils.FS.copy(root, Path.join(casePath, `${acase.ID}/dist`));
        Utils.FS.copy(Path.join(Path.dirname(root), 'app.manifest'), Path.join(casePath, `${acase.ID}/app.manifest`));
        const distPath = Path.join(casePath, `${acase.ID}.zip`);
        yield Utils.FS.zip(Path.join(casePath, acase.ID), distPath);
        Utils.FS.rmdirs(Path.join(casePath, acase.ID));
        //  TODO: check case version
        Utils.Console.info(`上传用例工程文件 [${distPath}]`);
        const url = yield Utils.Basement.upload(distPath, `${acase.ID}`);
        Utils.Console.info(`用例工程文件上传成功 [${url}]`);
        acase.scheme = `rubik:/launch?type=falcon&dist=${Urlencode.encode(url)}`;
        return Promise.resolve();
    });
}
//# sourceMappingURL=falcon.js.map