"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 path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const fs_1 = __importDefault(require("fs"));
const other_1 = require("../../utils/other");
const axios_1 = __importDefault(require("axios"));
const ejs_1 = __importDefault(require("ejs"));
const app_1 = __importDefault(require("../../server/app"));
function launchServer(callback) {
    const server = new app_1.default();
    server.router.get('/', (ctx) => {
        callback(ctx);
    });
    server.launch().then(() => {
        const url = `http://localhost:${server.port}/`;
        console.log(`服务启动成功!`);
        console.log(`CDN列表访问地址: ${chalk_1.default.green(url)}`);
        (0, other_1.openInBrowser)(url);
    });
}
// 过滤掉不需要的资源
function filterUrls(list) {
    list['@syman/ark-ui'] = list['@syman/ark-ui'].filter((url) => url.endsWith('ark.min.js') ||
        url.endsWith('ark-ui.css') ||
        url.endsWith('iconfont.css'));
    list['@syman/ark-ui-bcl'] = list['@syman/ark-ui-bcl'].filter((url) => url.endsWith('ark.bcl.min.js') || url.endsWith('ark.bcl.min.css'));
    list['@syman/burgeon-r3'] = list['@syman/burgeon-r3'].filter((url) => url.endsWith('min.js') || url.endsWith('min.css'));
    return list;
}
function listOSS() {
    return __awaiter(this, void 0, void 0, function* () {
        const spinner = (0, ora_1.default)('正在请求远程cdn数据...').start();
        axios_1.default
            .get('https://ark-font.oss-cn-hangzhou.aliyuncs.com/oss.json') // 获取cdn列表
            .then((res) => {
            const result = res.data;
            if (result.code === 200) {
                // 渲染html模板
                const templatePath = path_1.default.join(__dirname, '../../assets/ejs-template/oss-cdn.html');
                const html = ejs_1.default.render(fs_1.default.readFileSync(templatePath).toString(), {
                    OSS_LIST: JSON.stringify(filterUrls(result.data)),
                });
                spinner.succeed('准备启动cdn列表界面');
                // 启动服务预览html
                launchServer((ctx) => {
                    ctx.type = 'html';
                    ctx.body = html;
                });
            }
        })
            .catch((err) => {
            spinner.stop();
            console.log('', err);
        });
    });
}
exports.default = listOSS;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibGlzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL2NvbW1hbmRzL29zcy9saXN0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7O0FBQUEsZ0RBQXVCO0FBQ3ZCLGtEQUF5QjtBQUN6Qiw4Q0FBcUI7QUFDckIsNENBQW1CO0FBQ25CLDZDQUFpRDtBQUNqRCxrREFBeUI7QUFDekIsOENBQXFCO0FBQ3JCLDJEQUFxQztBQUVyQyxTQUFTLFlBQVksQ0FBQyxRQUFhO0lBQ2pDLE1BQU0sTUFBTSxHQUFHLElBQUksYUFBTSxFQUFFLENBQUE7SUFDM0IsTUFBTSxDQUFDLE1BQU0sQ0FBQyxHQUFHLENBQUMsR0FBRyxFQUFFLENBQUMsR0FBUSxFQUFFLEVBQUU7UUFDbEMsUUFBUSxDQUFDLEdBQUcsQ0FBQyxDQUFBO0lBQ2YsQ0FBQyxDQUFDLENBQUE7SUFDRixNQUFNLENBQUMsTUFBTSxFQUFFLENBQUMsSUFBSSxDQUFDLEdBQUcsRUFBRTtRQUN4QixNQUFNLEdBQUcsR0FBRyxvQkFBb0IsTUFBTSxDQUFDLElBQUksR0FBRyxDQUFBO1FBQzlDLE9BQU8sQ0FBQyxHQUF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