UNPKG

create-bfe-cttq

Version:

CTTQ大前端脚手架项目

78 lines (75 loc) 2.18 kB
/** * 模版库管理工具 */ const fs = require('fs-extra'); const path = require('path'); const { downloadTemplate, removeDirectory } = require("./util/downloadTemplate"); const TemplatePath = ".template"; const single = "single" module.exports = class TemplateManager { /** * 单例 */ static get instace () { if (this[single]) { return this.single } return this[single] = new this() // 如果没有值就new 构造函数 } /** * 构造器 * @param {*} context 项目上下文路径地址 * @param {*} version 模版库的版本号 * @returns 实例对象 */ constructor(context, version) { const sourceClass = this.constructor; if (sourceClass[single]) { return sourceClass[single] } this.context = context; this.version = version; this.realVersion = ""; sourceClass[single] = this } /** * 模版库本地下载地址 * @returns 地址 */ rootPath() { let rootPath = path.resolve(__dirname, TemplatePath); fs.ensureDirSync(rootPath); return rootPath; } /** * 下载模版库 */ async download() { let url = `direct:https://gitlab.cttq.com/bfe-cli/cttq-template/-/archive/${this.version}/cttq-template-${this.version}.zip` await downloadTemplate(url, this.rootPath(), false); // 获取版本信息 let realVersionPath = path.resolve(this.rootPath(), "metadata.json"); if (fs.existsSync(realVersionPath)) { let metadata = JSON.parse(fs.readFileSync(realVersionPath, "utf-8") || "{}"); if (metadata.version) { this.realVersion = metadata.version; } else { this.realVersion = this.version; } } } /** * 获取具体模块的地址 * @param {*} templatePath 模块名称 * @returns 路径 */ pathAt(templatePath) { return path.resolve(this.rootPath(), templatePath); } /** * 删除模版库 */ delete() { removeDirectory(this.rootPath()); } }