UNPKG

@erda-ui/cli

Version:

Command line interface for rapid Erda UI development

68 lines (67 loc) 3.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const superagent_1 = __importDefault(require("superagent")); const path_1 = __importDefault(require("path")); const env_1 = require("./util/env"); const log_1 = require("./util/log"); const downloadFile = (url, savePath) => { return superagent_1.default.get(`https:${url.replace('https:', '')}`).then((res) => { let data = ''; if (res.request.url.match(/\.js$/)) { data = res.body.toString('utf8'); } else { data = res.text; } if (res.status === 200 && (data.startsWith('@font-face') || data.includes('<symbol') || data.startsWith('(function(){window.__iconpark__'))) { fs_1.default.writeFile(savePath, data, (e) => { if (e) { (0, log_1.logError)('write iconfont file failed!', e); } }); } else { throw new Error(`Got error when downloading ${url}`); } }); }; const LocalIcon = () => { const htmlPath = path_1.default.resolve((0, env_1.getPublicDir)(), './static/shell/index.html'); fs_1.default.readFile(htmlPath, 'utf8', (err, content) => { if (err) (0, log_1.logError)('read index.html failed'); const iconfontRegex = /\/\/at.alicdn.com\/t\/(([^.]+)\.(css|js))/g; const iconparkRegex = /https:\/\/lf1-cdn-tos\.bytegoofy\.com\/obj\/iconpark\/(([^"']+)\.js)/g; let matchedIconfontFile = iconfontRegex.exec(content); let replacedContent = content; while (matchedIconfontFile) { const iconfontFilePath = matchedIconfontFile[0]; (0, log_1.logSuccess)('get matched iconfont file', iconfontFilePath); downloadFile(iconfontFilePath, path_1.default.resolve((0, env_1.getPublicDir)(), `./static/${matchedIconfontFile[1]}`)); replacedContent = replacedContent.replace(iconfontFilePath, `/static/${matchedIconfontFile[1]}`); matchedIconfontFile = iconfontRegex.exec(replacedContent); } let matchedIconparkFile = iconparkRegex.exec(replacedContent); while (matchedIconparkFile) { const iconparkFilePath = matchedIconparkFile[0]; (0, log_1.logSuccess)('get matched iconpark file', iconparkFilePath); downloadFile(iconparkFilePath, path_1.default.resolve((0, env_1.getPublicDir)(), `./static/${matchedIconparkFile[1]}`)); replacedContent = replacedContent.replace(iconparkFilePath, `/static/${matchedIconparkFile[1]}`); matchedIconparkFile = iconparkRegex.exec(replacedContent); } fs_1.default.writeFile(htmlPath, replacedContent, (e) => { if (e) { (0, log_1.logError)('rewrite index.html failed!', e); } else { (0, log_1.logSuccess)('rewrite index.html completed'); } }); }); }; exports.default = LocalIcon;