"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 });
exports.uploadOssDocCallback = exports.uploadOssCallback = void 0;
const file_1 = require("utils/file");
const path_1 = __importDefault(require("path"));
const oss_1 = __importDefault(require("utils/oss"));
const axios_1 = __importDefault(require("axios"));
const fsEx = require('fs-extra');
function uploadOssCallback() {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise((resolve) => {
            axios_1.default
                .get('https://ark-font.oss-cn-hangzhou.aliyuncs.com/oss.json') // 获取cdn列表
                .then((res) => __awaiter(this, void 0, void 0, function* () {
                const result = res.data;
                if (result.code !== 200) {
                    return;
                }
                const oldJson = JSON.parse(JSON.stringify(result.data));
                const packageInfo = (0, file_1.getPackageInfo)();
                const packageName = packageInfo.name;
                const packageVersion = packageInfo.version;
                if (!packageName || !packageVersion) {
                    return;
                }
                if (['@syman/ark-ui', '@syman/ark-ui-bcl', '@syman/burgeon-r3'].includes(packageName)) {
                    const index = result.data[packageName].findIndex((item) => item.includes(packageVersion));
                    // 如果版本列表里没有这次发版信息，则添加。并创建上一次json的备份文件(oss.backup.json)
                    if (index > -1) {
                        // console.log('已存在', result.data[packageName][index])
                        return;
                    }
                    if (packageName === '@syman/ark-ui') {
                        result.data[packageName].push(`https://oss.font.burgeononline.com/CDN/@syman/ark-ui/${packageVersion}/ark.min.js`, `https://oss.font.burgeononline.com/CDN/@syman/ark-ui/${packageVersion}/styles/ark-ui.css`, `https://oss.font.burgeononline.com/CDN/@syman/ark-ui/${packageVersion}/styles/bjIconfonts/iconfont.css`);
                    }
                    if (packageName === '@syman/ark-ui-bcl') {
                        result.data[packageName].push(`https://oss.font.burgeononline.com/CDN/@syman/ark-ui-bcl/${packageVersion}/ark.bcl.min.css`, `https://oss.font.burgeononline.com/CDN/@syman/ark-ui-bcl/${packageVersion}/ark.bcl.min.js`);
                    }
                    if (packageName === '@syman/burgeon-r3') {
                        result.data[packageName].push(`https://oss.font.burgeononline.com/CDN/@syman/burgeon-r3/${packageVersion}/r3.min.css`, `https://oss.font.burgeononline.com/CDN/@syman/burgeon-r3/${packageVersion}/r3.min.js`);
                    }
                }
                const ossPath = path_1.default.resolve(__dirname, '../../temp/oss.json');
                const oldOssPath = path_1.default.resolve(__dirname, '../../temp/oss.backup.json');
                fsEx.ensureFileSync(path_1.default.resolve(__dirname, ossPath));
                fsEx.ensureFileSync(path_1.default.resolve(__dirname, oldOssPath));
                fsEx.writeJsonSync(ossPath, result, { spaces: 2 });
                fsEx.writeJsonSync(oldOssPath, oldJson, { spaces: 2 });
                const isSuccess = yield oss_1.default.uploadFiles('/oss.json', path_1.default.resolve(__dirname, ossPath));
                if (isSuccess) {
                    yield oss_1.default.uploadFiles('/oss.backup.json', path_1.default.resolve(__dirname, oldOssPath));
                }
            }))
                .finally(() => {
                resolve(true);
            });
        });
    });
}
exports.uploadOssCallback = uploadOssCallback;
function uploadOssDocCallback() {
    return __awaiter(this, void 0, void 0, function* () {
        // 文档上传
        return new Promise((resolve) => {
            axios_1.default
                .get('https://ark-font.oss-cn-hangzhou.aliyuncs.com/ossDoc.json') // 获取cdn列表
                .then((res) => __awaiter(this, void 0, void 0, function* () {
                const result = res.data;
                if (result.code !== 200) {
                    return;
                }
                const oldJson = JSON.parse(JSON.stringify(result.data));
                const packageInfo = (0, file_1.getPackageInfo)();
                const packageName = packageInfo.name;
                result.data[packageName] = `https://oss.font.burgeononline.com/DOC/${packageName}/index.html`;
                const ossPath = path_1.default.resolve(__dirname, '../../temp/ossDoc.json');
                const oldOssPath = path_1.default.resolve(__dirname, '../../temp/ossDoc.backup.json');
                fsEx.ensureFileSync(path_1.default.resolve(__dirname, ossPath));
                fsEx.ensureFileSync(path_1.default.resolve(__dirname, oldOssPath));
                fsEx.writeJsonSync(ossPath, result, { spaces: 2 });
                fsEx.writeJsonSync(oldOssPath, oldJson, { spaces: 2 });
                const isSuccess = yield oss_1.default.uploadFiles('/ossDoc.json', path_1.default.resolve(__dirname, ossPath));
                if (isSuccess) {
                    yield oss_1.default.uploadFiles('/ossDoc.backup.json', path_1.default.resolve(__dirname, oldOssPath));
                }
            }))
                .finally(() => {
                resolve(true);
            });
        });
    });
}
exports.uploadOssDocCallback = uploadOssDocCallback;
//# sourceMappingURL=data:application/json;base64, 