youzanyun-devtool-worker
Version:
88 lines (87 loc) • 3.3 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 constants_1 = require("./constants");
class PageHandler {
constructor({ beautyPcGuideDir, workDir, projectRootDir, projectName, groupPath, }) {
this.beautyPcGuideDir = beautyPcGuideDir;
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.editorDir = path_1.default.resolve(this.extensionDir, this.groupPath);
}
async isPageAddCustom() {
const isExist = await fs_extra_1.default.pathExists(this.editorDir);
if (!isExist) {
return {
isCustomed: false,
expandedList: []
};
}
const pages = await this.getAddPages();
const isCustomed = !!pages && pages.length > 0;
return {
isCustomed,
expandedList: pages,
};
}
async getAddPages() {
const pageFiles = await (0, fast_glob_1.default)('**/main.js', {
cwd: this.editorDir
});
const customInfo = pageFiles.map((file) => {
const filePath = file.split(path_1.default.sep).slice(0, -1).join(path_1.default.sep);
return {
title: filePath,
path: filePath,
isCustomed: true,
};
});
return customInfo;
}
async addPage(info) {
const pagePath = path_1.default.resolve(this.editorDir, info.name);
const isPageExist = await fs_extra_1.default.pathExists(pagePath);
if (isPageExist) {
throw new Error('页面已存在');
}
await fs_extra_1.default.ensureDir(pagePath);
await fs_extra_1.default.copy(path_1.default.resolve(this.beautyPcGuideDir, 'page/add/page1'), pagePath);
const config = {
"pageName": info.name,
"path": info.name,
"title": info.title
};
await fs_extra_1.default.writeFile(path_1.default.resolve(pagePath, 'config.json'), JSON.stringify(config));
}
async removePage(info) {
const pagePath = path_1.default.resolve(this.editorDir, info.name);
const isPageExist = await fs_extra_1.default.pathExists(pagePath);
if (isPageExist) {
await fs_extra_1.default.remove(pagePath);
}
}
async getComponents() {
const pageFiles = await (0, fast_glob_1.default)('**/main.js', {
cwd: this.editorDir
});
const customInfo = pageFiles.map((file) => {
const filePath = file.split(path_1.default.sep).slice(0, -1).join(path_1.default.sep);
return {
title: filePath,
path: filePath,
isCustomed: true,
};
});
return customInfo;
}
}
exports.default = PageHandler;
;