@lark-project/cli
Version:
飞书项目插件开发工具
46 lines (45 loc) • 1.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadHTMLTemplateAndCache = exports.getCachedHTMLByPlatform = void 0;
const path_1 = __importDefault(require("path"));
const get_html_template_from_page_server_1 = require("../../../api/get-html-template-from-page-server");
const fs_extra_1 = require("fs-extra");
const get_project_directory_1 = require("../../../utils/get-project-directory");
const logger_1 = require("../../../utils/logger");
function getCachePathByPlatform(platform) {
const cacheDir = path_1.default.resolve((0, get_project_directory_1.getProjectDirectory)(), '.lpm-cache');
return path_1.default.resolve(cacheDir, `${platform}.html`);
}
function getCachedHTMLByPlatform(platform) {
return (0, fs_extra_1.readFileSync)(getCachePathByPlatform(platform), { encoding: 'utf-8' });
}
exports.getCachedHTMLByPlatform = getCachedHTMLByPlatform;
function writeHTML2Cache(cachePath, html) {
const { dir } = path_1.default.parse(cachePath);
if (!(0, fs_extra_1.pathExistsSync)(dir)) {
(0, fs_extra_1.mkdirSync)(dir, { recursive: true });
}
(0, fs_extra_1.writeFileSync)(cachePath, html);
}
async function downloadHTMLTemplateAndCache(options) {
const htmlCachePath = getCachePathByPlatform(options.platform);
try {
const html = await (0, get_html_template_from_page_server_1.getHTMLTemplateFromPageServer)(options);
writeHTML2Cache(htmlCachePath, html);
return html;
}
catch (e) {
logger_1.logger.debug(e);
try {
// try read last cache
return getCachedHTMLByPlatform(options.platform);
}
catch (e) {
throw new Error('download html tpl failed, please retry.');
}
}
}
exports.downloadHTMLTemplateAndCache = downloadHTMLTemplateAndCache;