"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 Falcon = __importStar(require("./falcon"));
const Vue = __importStar(require("./vue"));
const Utils = __importStar(require("../../util/src"));
const Project = __importStar(require("../../project/src"));
const Path = __importStar(require("path"));
class ACPack {
    buildProject(path, output) {
        return __awaiter(this, void 0, void 0, function* () {
            const pkg = Utils.ACUtils.pickProjectPackage(path);
            switch (pkg.type) {
                case Project.ProjectType.Application:
                    if (Project.ProjectSubType.App === pkg.subtype) {
                        const result = yield Vue.build(path, output);
                        return Promise.resolve({ success: true, message: result });
                    }
                    return Promise.reject(new Error(`无效的待编译项目子类型 [${pkg.subtype}] [${path}]`));
                case Project.ProjectType.Falcons:
                    yield Falcon.build(path, output);
                    return Promise.resolve({ success: true });
                default:
                    return Promise.reject(new Error(`未发现可编译项目 [${path}]`));
            }
        });
    }
    buildAllProjects(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const cost = new Utils.Cost();
            const projects = this.pickProjects(path);
            Utils.Console.info(`扫描可识别的Cube工程（${projects.length}个）[${path}]:\n${JSON.stringify(projects, undefined, 2)}`);
            let index = 1;
            let successed = 0;
            let failured = 0;
            for (const root of projects) {
                const dist = Path.join(root, 'dist');
                Utils.Console.info(`开始编译工程[${index}/${projects.length}][${root}]`);
                index++;
                try {
                    yield this.buildProject(root, dist);
                    successed++;
                }
                catch (error) {
                    Utils.Console.error(`编译工程异常: ${error} [${root}]`);
                    failured++;
                    continue;
                }
            }
            Utils.Console.info(`共成功编译工程${successed}个，失败${failured}个 [${path}]`);
            cost.mark();
        });
    }
    pickProjects(path) {
        const roots = new Array();
        Utils.FS.walk(path, (root, name, stats) => {
            if (name === 'package.json') {
                roots.push(root);
            }
        });
        return roots;
    }
}
exports.ACPack = ACPack;
//# sourceMappingURL=pack.js.map