youzanyun-devtool-worker
Version:
88 lines (87 loc) • 3.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const fast_glob_1 = tslib_1.__importDefault(require("fast-glob"));
const nunjucks_1 = tslib_1.__importDefault(require("nunjucks"));
const constants_1 = require("./constants");
class PageHandler {
constructor({ beautyH5GuideDir, workDir, projectRootDir, projectName, groupPath, }) {
this.beautyH5GuideDir = beautyH5GuideDir;
this.workDir = workDir;
this.projectRootDir = projectRootDir;
this.projectName = projectName;
this.groupPath = groupPath;
}
async start() {
this.extensionDir = path_1.default.resolve(this.projectRootDir, `${this.projectName}-ui/${constants_1.WORK_DIR_NAME}/src`);
this.pageAddDir = path_1.default.resolve(this.extensionDir, this.groupPath);
}
async isPageAddCustom() {
const isExist = await fs_extra_1.default.pathExists(this.pageAddDir);
if (!isExist) {
return {
isCustomed: false,
expandedList: []
};
}
const pages = await this.getAddPages();
const isCustomed = !!pages && pages.length > 0;
return {
isCustomed,
expandedList: pages
};
}
async getAddPages() {
const configFiles = await (0, fast_glob_1.default)('**/config.json', {
cwd: this.pageAddDir
});
const configInfo = configFiles.map((file) => {
const config = fs_extra_1.default.readJsonSync(path_1.default.resolve(this.pageAddDir, file));
return Object.assign(Object.assign({}, config), { name: config.pageName, isCustomed: true });
});
return configInfo;
}
async savePageAdd(info) {
try {
const { name, title, customConfig, } = info;
const pagePath = path_1.default.resolve(this.pageAddDir, name);
const isPageExist = await fs_extra_1.default.pathExists(pagePath);
if (isPageExist) {
throw new Error('页面已存在');
}
await fs_extra_1.default.ensureDir(pagePath);
if (customConfig) {
if (customConfig.autoCreateFiles) {
for (let filePath of customConfig.autoCreateFiles) {
await fs_extra_1.default.copy(path_1.default.resolve(this.beautyH5GuideDir, filePath), path_1.default.resolve(pagePath, path_1.default.basename(filePath)));
}
}
if (customConfig.njkFiles) {
for (let njkFile of customConfig.njkFiles) {
const fileName = path_1.default.basename(njkFile).split('.njk')[0];
await fs_extra_1.default.ensureFile(path_1.default.resolve(pagePath, fileName));
const fileTpl = await fs_extra_1.default.readFile(path_1.default.resolve(this.beautyH5GuideDir, njkFile), 'utf8');
const fileNun = nunjucks_1.default.compile(fileTpl);
const fileView = fileNun.render({ info });
await fs_extra_1.default.writeFile(path_1.default.resolve(pagePath, fileName), fileView);
}
}
}
}
catch (err) {
await this.removePageAdd(info);
throw err;
}
}
async removePageAdd(info) {
const pagePath = path_1.default.resolve(this.pageAddDir, info.name);
const isPageExist = await fs_extra_1.default.pathExists(pagePath);
if (isPageExist) {
await fs_extra_1.default.remove(pagePath);
}
}
}
exports.default = PageHandler;
;