cybertron-utils
Version:
cybertron components manage
41 lines (35 loc) • 1.39 kB
JavaScript
const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const { replaceJs } = require('../utils/replace');
const beautify = require('js-beautify').js;
const execSync = require('child_process').execSync;
const cwd = process.cwd();
const cnofigPath = path.join(cwd, './src/config.js');
const config = require(cnofigPath);
module.exports = {
command: 'create <componentName> <groupName>',
desc: '创建组件',
async handler(argv) {
const componentName = argv.componentName;
const groupName = argv.groupName;
if (!(/^[A-Z][A-z0-9]*$/).test(componentName)) {
return console.log(chalk.red('请输入首字母为大写的组件名称'));
}
if (fs.existsSync(path.join(cwd, `./src/${componentName}`))) {
return console.log(chalk.red('请检查文件夹是否已存在'));
}
execSync(`cp -r ../template ${path.join(cwd, `./src/`)}`, {
cwd: __dirname
});
execSync(`mv ./src/template ./src/${componentName}`);
const jsPath = `./src/${componentName}/js/index.jsx`;
const jsText = fs.readFileSync(jsPath, { encoding: 'utf-8' });
fs.writeFileSync(jsPath, replaceJs(jsText, componentName, groupName));
config[componentName] = {
version: "1.0.0"
};
let newConfig = `module.exports = ${JSON.stringify(config)};`;
fs.writeFileSync(cnofigPath, beautify(newConfig, { indent_size: 4, space_in_empty_paren: true }));
}
};