"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 __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.name = void 0;
var common_1 = __importDefault(require("@hypgame/common"));
var commander_1 = require("commander");
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
var PlatformBuild_1 = require("./PlatformBuild/PlatformBuild");
var ImgCompressModule_1 = __importDefault(require("./PlatformBuild/module/ImgCompressModule"));
var FileUtil_1 = __importDefault(require("./util/FileUtil"));
var LogUtil_1 = __importDefault(require("./util/LogUtil"));
/** 获取参数 */
function getOptions(projectDir, channel, cmd) {
    return __awaiter(this, void 0, void 0, function () {
        var compressPath, PROJECT_DIR, cfgPath, cfgData, options;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (!!projectDir) return [3 /*break*/, 2];
                    return [4 /*yield*/, LogUtil_1["default"].logInput("请输入项目根目录", function (str) {
                            return fs_1["default"].existsSync(str);
                        })];
                case 1:
                    compressPath = _a.sent();
                    projectDir = compressPath;
                    _a.label = 2;
                case 2:
                    PROJECT_DIR = projectDir;
                    console.log("PROJECT_DIR", PROJECT_DIR);
                    cfgPath = path_1["default"].join(PROJECT_DIR, "assets", "AppVersionCfg.json");
                    console.log("cfgPath", cfgPath);
                    cfgData = FileUtil_1["default"].loadJsonByPath(cfgPath);
                    if (!cfgData) {
                        LogUtil_1["default"].logError("配置错误，请检查");
                        return [2 /*return*/];
                    }
                    // console.log(cfgData)
                    LogUtil_1["default"].logWarn("appName : " + cfgData.appName);
                    LogUtil_1["default"].logWarn("cosRootKey : " + cfgData.cosRootKey);
                    LogUtil_1["default"].logWarn("cdnRootUrl : " + cfgData.cdnRootUrl);
                    options = {
                        projectDir: projectDir,
                        channel: channel,
                        cmd: cmd
                    };
                    return [4 /*yield*/, checkPlatformChannel(cfgData, options)];
                case 3:
                    _a.sent();
                    return [2 /*return*/, options];
            }
        });
    });
}
var program = new commander_1.Command();
program
    .command('build [projectDir] [channel]')
    .description('build project with channel')
    // .option('-p, --project', 'Build by project')
    // .option('-pf, --platform', 'Build by platform')
    .action(function (projectDir, channel, cmd) { return __awaiter(void 0, void 0, void 0, function () {
    var options;
    return __generator(this, function (_a) {
        switch (_a.label) {
            case 0:
                console.log("输入【渠道】为", channel);
                return [4 /*yield*/, getOptions(projectDir, channel, cmd)];
            case 1:
                options = _a.sent();
                return [4 /*yield*/, PlatformBuild_1.platformBuildUpload(options)];
            case 2:
                _a.sent();
                return [2 /*return*/];
        }
    });
}); });
program
    .command('uploadCfg [projectDir] [channel]')
    .description('uploadCfg project with channel')
    // .option('-p, --project', 'Build by project')
    // .option('-pf, --platform', 'Build by platform')
    .action(function (projectDir, channel, cmd) { return __awaiter(void 0, void 0, void 0, function () {
    var options;
    return __generator(this, function (_a) {
        switch (_a.label) {
            case 0: return [4 /*yield*/, getOptions(projectDir, channel, cmd)];
            case 1:
                options = _a.sent();
                return [4 /*yield*/, PlatformBuild_1.platformUploadCfg(options)];
            case 2:
                _a.sent();
                return [2 /*return*/];
        }
    });
}); });
program
    .command('uploadWeb [projectDir] [channel]')
    .description('uploadWeb project with channel')
    // .option('-p, --project', 'Build by project')
    // .option('-pf, --platform', 'Build by platform')
    .action(function (projectDir, channel, cmd) { return __awaiter(void 0, void 0, void 0, function () {
    var options;
    return __generator(this, function (_a) {
        switch (_a.label) {
            case 0: return [4 /*yield*/, getOptions(projectDir, channel, cmd)];
            case 1:
                options = _a.sent();
                return [4 /*yield*/, PlatformBuild_1.platformUploadWeb(options)];
            case 2:
                _a.sent();
                return [2 /*return*/];
        }
    });
}); });
/** 检查参数:平台 渠道 */
function checkPlatformChannel(cfgData, options) {
    return __awaiter(this, void 0, void 0, function () {
        var channel, gameChannelList, channels, select;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    channel = options.channel;
                    gameChannelList = cfgData.gameChannelList || {};
                    if (!!gameChannelList[channel]) return [3 /*break*/, 2];
                    if (!channel) {
                        LogUtil_1["default"].logWarn("当前操作 [渠道] 不存在:" + channel);
                    }
                    channels = Object.keys(gameChannelList);
                    return [4 /*yield*/, LogUtil_1["default"].logInputSelect("请选择当前操作 [渠道] ", channels)];
                case 1:
                    select = _a.sent();
                    channel = select;
                    _a.label = 2;
                case 2:
                    LogUtil_1["default"].logWarn("当前操作 [渠道] " + channel);
                    options.channel = channel;
                    /** 初始化渠道信息 */
                    common_1["default"].init(cfgData, channel);
                    return [2 /*return*/];
            }
        });
    });
}
/** 手动压缩 */
function compress() {
    return __awaiter(this, void 0, void 0, function () {
        var compressPath, imgCompress;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, LogUtil_1["default"].logInput("请输入压缩路径", function (str) {
                        return fs_1["default"].existsSync(str);
                    })];
                case 1:
                    compressPath = _a.sent();
                    imgCompress = new ImgCompressModule_1["default"]();
                    return [4 /*yield*/, imgCompress.init(compressPath)];
                case 2:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    });
}
/** 初始化 */
function init() {
    return __awaiter(this, void 0, void 0, function () {
        var select, options, _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    if (!(process.argv.length <= 2)) return [3 /*break*/, 18];
                    return [4 /*yield*/, LogUtil_1["default"].logInputSelect("请选择功能", ["打包构建", "上传配置", "手动上传H5版本", "为构建版本，添加【加载远程bundle】功能", "压缩图片"])];
                case 1:
                    select = _b.sent();
                    options = null;
                    _a = select;
                    switch (_a) {
                        case "打包构建": return [3 /*break*/, 2];
                        case "上传配置": return [3 /*break*/, 5];
                        case "手动上传H5版本": return [3 /*break*/, 8];
                        case "压缩图片": return [3 /*break*/, 11];
                        case "为构建版本，添加【加载远程bundle】功能": return [3 /*break*/, 13];
                    }
                    return [3 /*break*/, 16];
                case 2: return [4 /*yield*/, getOptions(null)];
                case 3:
                    options = _b.sent();
                    return [4 /*yield*/, PlatformBuild_1.platformBuildUpload(options)];
                case 4:
                    _b.sent();
                    return [3 /*break*/, 17];
                case 5: return [4 /*yield*/, getOptions(null)];
                case 6:
                    options = _b.sent();
                    return [4 /*yield*/, PlatformBuild_1.platformUploadCfg(options)];
                case 7:
                    _b.sent();
                    return [3 /*break*/, 17];
                case 8: return [4 /*yield*/, getOptions(null)];
                case 9:
                    options = _b.sent();
                    return [4 /*yield*/, PlatformBuild_1.platformUploadWeb(options)];
                case 10:
                    _b.sent();
                    return [3 /*break*/, 17];
                case 11: return [4 /*yield*/, compress()];
                case 12:
                    _b.sent();
                    return [3 /*break*/, 17];
                case 13: return [4 /*yield*/, getOptions(null)];
                case 14:
                    options = _b.sent();
                    return [4 /*yield*/, PlatformBuild_1.platformAddRemoteBundle(options)];
                case 15:
                    _b.sent();
                    return [3 /*break*/, 17];
                case 16: return [3 /*break*/, 17];
                case 17: return [2 /*return*/];
                case 18:
                    program.parse(process.argv);
                    return [2 /*return*/];
            }
        });
    });
}
init();
var HYPPkgBuilder = /** @class */ (function () {
    function HYPPkgBuilder() {
    }
    /** 初始化 */
    HYPPkgBuilder.init = function (jsonData, targetChannel) {
    };
    HYPPkgBuilder.cosTypeRoot = "YpGame";
    return HYPPkgBuilder;
}());
exports["default"] = HYPPkgBuilder;
/** 还需要导出一些其他的，否则找不到 */
exports.name = "HYPPkgBuilder";
/** 导出需要的一些公共类 */
