"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 Utils = __importStar(require("../../../util/src"));
const Model = __importStar(require("../model"));
const Vue = __importStar(require("./vue"));
const Falcon = __importStar(require("./falcon"));
const Tiny = __importStar(require("./tiny"));
const handlers = Object.create(null);
handlers[`${Model.Case.Type.Falcon}`] = Falcon.build;
handlers[`${Model.Case.Type.Vue}`] = Vue.build;
handlers[`${Model.Case.Type.Tiny}`] = Tiny.build;
function build(path) {
    return __awaiter(this, void 0, void 0, function* () {
        const cost = new Utils.Cost();
        const cases = new Array();
        const projects = pickProjects(path);
        Utils.Console.info(`扫描可识别的工程（${projects.length}个）[${path}]`);
        for (const project of projects) {
            const func = handlers[`${projectType(project)}`];
            if (func) {
                Utils.Console.info(`构造工程内相关用例 [${project}]`);
                const subCases = yield func(project);
                cases.push(...subCases);
            }
            else {
                Utils.Console.error(`未识别的工程类型 [${project}]`);
            }
        }
        cost.mark();
        return Promise.resolve(cases);
    });
}
exports.build = build;
function projectType(root) {
    if (FS.existsSync(Path.join(root, 'package.json'))) {
        if (FS.existsSync(Path.join(root, 'src/app.js')) && FS.existsSync(Path.join(root, 'src/app.json'))) {
            return Model.Case.Type.Tiny;
        }
        const pkg = JSON.parse(FS.readFileSync(Path.join(root, `package.json`)).toString());
        if (pkg.type === Model.Case.Type.Vue) {
            return Model.Case.Type.Vue;
        }
        else if (pkg.type === Model.Case.Type.Falcon) {
            return Model.Case.Type.Falcon;
        }
    }
    return Model.Case.Type.Unkonwn;
}
function pickProjects(root) {
    const roots = new Array();
    if (!FS.statSync(root).isDirectory()) {
        //  滤除非文件夹类型
        return roots;
    }
    const ptype = projectType(root);
    if (Model.Case.Type.Unkonwn !== ptype) {
        roots.push(root);
    }
    else {
        //  包含子工程
        const subpaths = FS.readdirSync(root);
        for (const subpath of subpaths) {
            roots.push(...pickProjects(Path.join(root, subpath)));
        }
    }
    return roots;
}
//# sourceMappingURL=index.js.map