"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const oss_1 = require("utils/oss");
const inquirer_1 = __importDefault(require("inquirer"));
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const oss_2 = __importDefault(require("utils/oss"));
const ora_1 = __importDefault(require("ora"));
const callback_1 = require("./callback");
const file_1 = require("utils/file");
const fsEx = require('fs-extra');
function uploadOssList(options) {
    return __awaiter(this, void 0, void 0, function* () {
        yield options.callback();
    });
}
function confirmUpload(value) {
    return __awaiter(this, void 0, void 0, function* () {
        const canUpload = (yield inquirer_1.default.prompt({
            type: 'confirm',
            name: 'canUpload',
            message: `请确认是否上传到${value}目录下`,
        })).canUpload;
        return canUpload;
    });
}
function uploadOSS(options) {
    var _a, _b;
    return __awaiter(this, void 0, void 0, function* () {
        // 上传到线上时需要提示(即/CDN开头的路径)
        const isPublishToProduct = (_a = options.ossPath) === null || _a === void 0 ? void 0 : _a.startsWith(options.name);
        if (isPublishToProduct) {
            const isConfirm = yield confirmUpload(options.name);
            if (!isConfirm) {
                return;
            }
        }
        // 如果用户清除缓存
        if (options.clearCache) {
            fsEx.removeSync(oss_1.OSS_ENV_PATH);
        }
        yield oss_2.default.init();
        const ossPath = options.ossPath || '';
        // 生产环境如果已存在oss目录，就不允许上传。防止覆盖
        if (isPublishToProduct) {
            const isExistOssPath = yield oss_2.default.isExistOssPath(ossPath);
            if (isExistOssPath) {
                console.log(chalk_1.default.red(`oss路径${options.ossPath}已存在!请重新指定--ossPath参数`));
                return;
            }
        }
        let localPath = '';
        if (options.localPath && options.localPath.startsWith('/')) {
            // 绝对路径
            localPath = options.localPath;
        }
        else {
            localPath = path_1.default.resolve(process.cwd(), options.localPath || '');
        }
        const spinner = (0, ora_1.default)(`正在上传文件至oss目录${ossPath}，请稍等...`).start();
        spinner.text = '';
        const status = yield oss_2.default.multipleFilesUpload(ossPath, localPath);
        if (!status) {
            fsEx.removeSync(oss_1.OSS_ENV_PATH); // 清除配置
            spinner.fail(chalk_1.default.red('文件上传失败!'));
            return;
        }
        // 不发正式版的话就不更新oss列表
        if (!((_b = options.ossPath) === null || _b === void 0 ? void 0 : _b.startsWith(options.name)) && options.name !== './DOC') {
            spinner.succeed(chalk_1.default.green('文件上传成功!'));
            return;
        }
        // 自动更新oss上的json
        yield uploadOssList(options);
        spinner.succeed(chalk_1.default.green('文件上传成功!'));
    });
}
function upload(options) {
    // 传参
    if (options.doc) {
        // 上传文档
        const packageInfo = (0, file_1.getPackageInfo)();
        options.ossPath = `/DOC/${packageInfo.name}`;
        options.localPath = './.arkdoc-dist';
        options.callback = callback_1.uploadOssDocCallback;
        options.name = '/DOC';
    }
    else {
        // 根据上传目录上传文件
        options.callback = callback_1.uploadOssCallback;
        options.name = '/CDN';
    }
    uploadOSS(options);
}
exports.default = upload;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXBsb2FkLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vY29tbWFuZHMvb3NzL3VwbG9hZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7OztBQUFBLG1DQUF3QztBQUN4Qyx3REFBK0I7QUFDL0IsZ0RBQXVCO0FBQ3ZCLGtEQUF5QjtBQUN6QixvREFBbUM7QUFDbkMsOENBQXFCO0FBQ3JCLHlD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