@rist/preset-react
Version:
前端react模板生成器
68 lines (67 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFiles = exports.BootstrapComponentAction = void 0;
const abstract_bootstrap_action_1 = require("../abstract/abstract.bootstrap.action");
const shelljs = require("shelljs");
const CheckExist_1 = require("../utils/checkExist/CheckExist");
const path = require("path");
const nameStyles = require("name-styles");
const rimraf = require("rimraf");
const ui_1 = require("../utils/ui");
const fs = require("fs");
class BootstrapComponentAction extends abstract_bootstrap_action_1.AbstractBootstrapAction {
async handle(params) {
const fileNamePascal = nameStyles.pascal(params.name);
const dirNameHyphen = nameStyles.hyphen(params.name);
const dirName = dirNameHyphen + '-component';
const fileName = fileNamePascal + 'Component';
const _path = params.path || process.cwd();
const sourcePath = path.resolve(__dirname, `../../solutions/template/components/${params.schematic}/*`);
const templatePath = path.resolve(__dirname, `../../solutions/template/components/${params.schematic}`);
let action;
if (!fs.existsSync(templatePath)) {
console.log(ui_1.INFO_PREFIX, "模板不存在");
process.exit(1);
}
if (action = await CheckExist_1.CheckExist(`${_path}/${dirName}`)) {
if (action.cover === true) {
await new Promise((res, rej) => {
rimraf(`${_path}\\${dirName}`, () => {
res("删除源文件成功");
});
}).then((resp) => {
console.log(ui_1.INFO_PREFIX, resp);
});
}
if (!action.cover) {
console.log(ui_1.INFO_PREFIX, "取消创建");
}
shelljs.cd(_path);
shelljs.chmod("777", process.cwd());
console.log(process.cwd());
shelljs.mkdir("-p", dirName);
shelljs.cd(dirName);
shelljs.cp('-R', sourcePath, `${_path}\\${dirName}`);
exports.createFiles(dirNameHyphen, fileName);
console.log(ui_1.INFO_PREFIX, "文件创建成功");
}
}
}
exports.BootstrapComponentAction = BootstrapComponentAction;
// 创建文件
exports.createFiles = (dirNameHyphen, name) => {
shelljs.ls().forEach((fileName) => {
if (/\.tpl$/ig.test(fileName)) {
let rename;
if (/^filename.+\.tpl$/ig.test(fileName)) {
rename = dirNameHyphen + '.' + fileName.replace(/^filename\.|\.tpl$/ig, '');
}
else {
rename = fileName.replace(/\.tpl$/ig, '');
}
shelljs.mv(fileName, rename);
shelljs.sed('-i', /%_ComponentName_%/g, name, rename);
shelljs.sed('-i', /%_StyleName_%/g, name, rename);
}
});
};