"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 Core = require("../../core/src");
const Path = require("path");
const FS = require("fs");
const Walk = require("walk");
class Engine {
    createCases(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const cases = new Array();
            const projects = this.pickProjects(path);
            console.log(`扫描可识别的Cube工程[${path}]:\n${JSON.stringify(projects, undefined, 2)}`);
            for (const root of projects) {
                const acase = yield this.createCase(root);
                //  存储用例配置信息
                console.log(`存储用例配置信息至[${Path.join(root, `${acase.id}.rcase`)}]`);
                FS.writeFileSync(Path.join(root, `${acase.id}.rcase`), JSON.stringify(acase, undefined, 2), { encoding: 'utf8' });
                cases.push(acase);
            }
            return Promise.resolve(cases);
        });
    }
    pickProjects(root) {
        const roots = new Array();
        Walk.walkSync(root, {
            listeners: {
                file: (path, stats, next) => {
                    if (stats.type === 'file' && stats.name === 'package.json') {
                        roots.push(path);
                    }
                    next();
                }
            }
        });
        return roots;
    }
    createCase(path) {
        return __awaiter(this, void 0, void 0, function* () {
            console.log(`\n编译工程[${path}]...`);
            yield Core.AntCube.pack.buildProject(path, Path.join(path, 'dist'));
            console.log(`编译完成`);
            const pkg = JSON.parse(FS.readFileSync(Path.join(path, 'package.json')).toString());
            const acase = new Object(undefined);
            acase.id = pkg.name;
            if (pkg.case && pkg.case.id) {
                acase.id = pkg.case.id;
            }
            acase.poles = 'paint_message';
            let folderName = path.split('/').slice(-1).pop();
            if (folderName === '') {
                folderName = path.split('/').slice(-2, -1).pop();
            }
            if (pkg.type === 'templates') {
                acase.iOSScheme = `rubik:/launch?type=vue&path=RubikTest.bundle%2fproject%2ffalcon%2f${folderName}`;
            }
            else if (pkg.type === 'application') {
                acase.iOSScheme = `rubik:/launch?type=vue&path=RubikTest.bundle%2fproject%2fvue%2f${folderName}`;
            }
            acase.root = path;
            acase.sql = `INSERT OR REPLACE INTO rkt_case_manifest (ID,type,iosScheme) VALUES ('${acase.id}','Rubik','${acase.iOSScheme}')`;
            console.log(`创建用例配置:\n${JSON.stringify(acase, undefined, 2)}`);
            return Promise.resolve(acase);
        });
    }
}
exports.Engine = Engine;
//# sourceMappingURL=engine.js.map