106 lines
3.6 KiB
JavaScript
Executable File
106 lines
3.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
||
const {program} = require('commander');
|
||
const packageConfig = require("../package.json");
|
||
const fs = require("fs");
|
||
const path = require('path');
|
||
const Config = require("./config");
|
||
|
||
var isWin = /^win/.test(process.platform);
|
||
const RootPath = process.cwd();
|
||
|
||
program
|
||
.requiredOption('-t, --file-type <type>', '文件类型: [page,logic,service,impl,less]')
|
||
.option('-n, --file-name <name>', '被创建的文件名称')
|
||
.option('-i, --is-create <is>', '是否为 -t 参数自动创建相关文件类型', false);
|
||
program.version(packageConfig.version, '-v, --version', '显示当前的版本');
|
||
|
||
program.parse(process.argv);
|
||
|
||
// 存储终端的参数
|
||
let programOptions = program.opts();
|
||
let OtherFileType = program.parse(process.argv).args
|
||
|
||
// tsx 页面路径
|
||
let PagePath = isWin ? `${ RootPath }\\src\\application\\page\\${ UpperCase(programOptions.fileName) }.tsx` : `${ RootPath }/src/application/page/${ UpperCase(programOptions.fileName) }.tsx`;
|
||
// logic 页面路径
|
||
let LogicPath = isWin ? `${ RootPath }\\src\\application\\logic\\page\\${ UpperCase(programOptions.fileName) }.logic.ts` : `${ RootPath }/src/application/logic/page/${ UpperCase(programOptions.fileName) }.logic.ts`;
|
||
// service 页面路径
|
||
let ServicePath = isWin ? `${ RootPath }\\src\\core\\service\\${ UpperCase(programOptions.fileName) }.service.ts` : `${ RootPath }/src/core/service/${ UpperCase(programOptions.fileName) }.service.ts`;
|
||
// Impl 页面路径
|
||
let ImplPath = isWin ? `${ RootPath }\\src\\core\\service\\impl\\${ UpperCase(programOptions.fileName) }.service.impl.ts` : `${ RootPath }/src/core/service/impl/${ UpperCase(programOptions.fileName) }.service.impl.ts`;
|
||
// Less 页面路径
|
||
let LessPath = isWin ? `${ RootPath }\\src\\application\\assets\\less\\page\\${ UpperCase(programOptions.fileName) }.less` : `${ RootPath }/src/application/assets/less/page/${ UpperCase(programOptions.fileName) }.less`;
|
||
|
||
// 需要创建的文件类型
|
||
let FileList = ['page', 'logic', 'service', 'impl', 'less'];
|
||
|
||
// 是否需要为 controller 联合创建所有文件
|
||
if (programOptions.isCreate) {
|
||
FileList.forEach(val => replaceTplText(val));
|
||
console.log("成功创建文件...");
|
||
} else {
|
||
// 循环创建多个文件
|
||
if (OtherFileType.length != 0) {
|
||
[...OtherFileType, programOptions.fileType].forEach(v => replaceTplText(v))
|
||
} else {
|
||
replaceTplText(programOptions.fileType)
|
||
}
|
||
console.log("成功创建文件...");
|
||
}
|
||
|
||
/**
|
||
* 替换模板的内容,并将内容写入文件。
|
||
*/
|
||
let FileText = ""
|
||
|
||
function replaceTplText(action) {
|
||
switch (action) {
|
||
case 'page':
|
||
fs.writeFileSync(PagePath, Config.pageConfig(UpperCase(programOptions.fileName)));
|
||
break;
|
||
case 'logic':
|
||
fs.writeFileSync(LogicPath, Config.logicConfig(UpperCase(programOptions.fileName)));
|
||
break;
|
||
case 'service':
|
||
fs.writeFileSync(ServicePath, Config.serviceConfig(UpperCase(programOptions.fileName)));
|
||
break;
|
||
case 'impl':
|
||
fs.writeFileSync(ImplPath, Config.implPathConfig(UpperCase(programOptions.fileName)));
|
||
break;
|
||
case 'less':
|
||
fs.writeFileSync(LessPath, Config.lessConfig(UpperCase(programOptions.fileName)));
|
||
break;
|
||
default:
|
||
break
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 单词首字母大写
|
||
* @param str
|
||
* @returns {void | string | *}
|
||
* @constructor
|
||
*/
|
||
function UpperCase(str) {
|
||
return str.replace(str[0], str[0].toUpperCase())
|
||
}
|
||
|
||
/**
|
||
* 读取文件内容
|
||
* @param path
|
||
* @returns {*}
|
||
* @constructor
|
||
*/
|
||
function GetFileText(path) {
|
||
return (fs.readFileSync(pathJoin(path), 'utf-8')).toString();
|
||
}
|
||
|
||
/**
|
||
* 拼接路径
|
||
* @param paths
|
||
* @returns {string}
|
||
*/
|
||
function pathJoin(paths) {
|
||
return path.join(__dirname, paths)
|
||
}
|