177 lines
5.8 KiB
JavaScript
Executable File
177 lines
5.8 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
||
const { program } = require('commander');
|
||
const package = require("../package");
|
||
const fs = require("fs");
|
||
const path = require('path');
|
||
|
||
program
|
||
.requiredOption('-t, --file-type <type>', '文件类型: [controller,dao,entity,model,service,impl,jade,less,js]')
|
||
.option('-n, --file-name <name>', '被创建的文件名称')
|
||
.option('-p, --file-path <path>', '为 -t 参数指定 controller 文件夹名')
|
||
.option('-i, --is-create <is>', '是否为 -t 参数自动创建相关文件类型', false);
|
||
program.version(package.version, '-v, --version', '显示当前的版本');
|
||
|
||
program.parse(process.argv);
|
||
|
||
// 存储终端的参数
|
||
const programOptions = program.opts();
|
||
|
||
/**
|
||
* TODO 如果在Api或者Admnin文件夹下创建controller,那么也可以为他们关联创建更多的文件类型
|
||
* @type {string}
|
||
*/
|
||
|
||
// 各个文件路径
|
||
let DaoPath = pathJoin(`../src/dao/${programOptions.fileName}Dao.ts`);
|
||
let EntityPath = pathJoin(`../src/entity/${programOptions.fileName}Entity.ts`);
|
||
let ModelPath = pathJoin(`../src/model/${programOptions.fileName}Model.ts`);
|
||
let ServicePath = pathJoin(`../src/service/${programOptions.fileName}Service.ts`);
|
||
let ServiceImplPath = pathJoin(`../src/service/impl/${programOptions.fileName}ServiceImpl.ts`);
|
||
let JadePath = pathJoin(`../src/resources/view/home/page/${programOptions.fileName}.jade`);
|
||
let LessPath = pathJoin(`../src/resources/static/less/home/${programOptions.fileName}.less`);
|
||
let JsPath = pathJoin(`../src/resources/static/js/app/${programOptions.fileName}.js`);
|
||
let ControllerPath = "";
|
||
|
||
// 判断是否创建 controller 并且 路径文件夹名是否不为空
|
||
if (programOptions.fileType == 'controller' && (programOptions.filePath == 'home' || programOptions.filePath == 'api' || programOptions.filePath == 'admin') ) {
|
||
|
||
// 存储controller路径
|
||
ControllerPath = path.join(__dirname, `../src/controller/${programOptions.filePath}/${programOptions.fileName}Controller${UpperCase(programOptions.filePath)}.ts`);
|
||
|
||
// 是否需要为 controller 联合创建所有文件
|
||
if (programOptions.isCreate) {
|
||
|
||
switch (programOptions.filePath) {
|
||
// 创建所有文件
|
||
case 'home':
|
||
let FileList = ['controller','dao','entity','model','service','impl','jade','less','js'];
|
||
FileList.forEach(val => replaceTplText(val));
|
||
console.log("成功创建所有文件...");
|
||
break;
|
||
|
||
// 只创建ts文件
|
||
case 'api':
|
||
['controller','dao','entity','model','service','impl'].forEach(val => replaceTplText(val));
|
||
console.log("成功创建所有文件...");
|
||
break;
|
||
|
||
// 只创建ts文件
|
||
case 'admin':
|
||
['controller','dao','entity','model','service','impl'].forEach(val => replaceTplText(val));
|
||
console.log("成功创建所有文件...");
|
||
break;
|
||
|
||
default:
|
||
|
||
break
|
||
}
|
||
// 只创建 controller
|
||
} else {
|
||
|
||
// 生成并写入模板内容
|
||
replaceTplText('controller');
|
||
console.log("controller创建成功...")
|
||
}
|
||
}
|
||
// 创建 dao
|
||
if (programOptions.fileType == 'dao') replaceTplText('dao');
|
||
// 创建 entity
|
||
if (programOptions.fileType == 'entity') replaceTplText('entity');
|
||
// 创建 model
|
||
if (programOptions.fileType == 'model') replaceTplText('model');
|
||
// 创建 service
|
||
if (programOptions.fileType == 'service') replaceTplText('service');
|
||
// 创建 impl
|
||
if (programOptions.fileType == 'impl') replaceTplText('impl');
|
||
// 创建 jade
|
||
if (programOptions.fileType == 'jade') replaceTplText('jade');
|
||
// 创建 less
|
||
if (programOptions.fileType == 'less') replaceTplText('less');
|
||
// 创建 js
|
||
if (programOptions.fileType == 'js') replaceTplText('js');
|
||
|
||
|
||
/**
|
||
* 替换模板的内容,并将内容写入文件。
|
||
*/
|
||
function replaceTplText (action) {
|
||
switch (action) {
|
||
case 'controller':
|
||
let text = GetFileText('./FileTpl/conterller.txt').replace(/ControllerName/g, `${programOptions.fileName}Controller${UpperCase(programOptions.filePath)}`);
|
||
fs.writeFileSync(ControllerPath, text);
|
||
break;
|
||
|
||
case 'dao':
|
||
let dao = GetFileText('./FileTpl/dao.txt').replace(/DaoName/g, `${UpperCase(programOptions.fileName)}Dao`);
|
||
fs.writeFileSync(DaoPath, dao);
|
||
break;
|
||
|
||
case 'entity':
|
||
let entity = GetFileText('./FileTpl/entity.txt').replace(/EntityName/g, `${UpperCase(programOptions.fileName)}Entity`);
|
||
fs.writeFileSync(EntityPath, entity);
|
||
break;
|
||
|
||
case 'model':
|
||
let model = GetFileText('./FileTpl/model.txt').replace(/ModelName/g, `${UpperCase(programOptions.fileName)}Model`);
|
||
fs.writeFileSync(ModelPath, model);
|
||
break;
|
||
|
||
case 'service':
|
||
let service = GetFileText('./FileTpl/service.txt').replace(/ServiceName/g, `${UpperCase(programOptions.fileName)}Service`);
|
||
fs.writeFileSync(ServicePath, service);
|
||
break;
|
||
|
||
case 'impl':
|
||
let impl = GetFileText('./FileTpl/impl.txt').replace(/ImplName/g, `${UpperCase(programOptions.fileName)}ServiceImpl`);
|
||
impl = impl.replace(/ServiceName/g, `${UpperCase(programOptions.fileName)}Service`);
|
||
fs.writeFileSync(ServiceImplPath, impl);
|
||
break;
|
||
|
||
case 'jade':
|
||
let jade = GetFileText('./FileTpl/jade.txt').replace(/JadeName/g, programOptions.fileName);
|
||
fs.writeFileSync(JadePath, jade);
|
||
break;
|
||
|
||
case 'less':
|
||
let less = GetFileText('./FileTpl/less.txt').replace(/ClassName/g, programOptions.fileName);
|
||
fs.writeFileSync(LessPath, less);
|
||
break;
|
||
|
||
case 'js':
|
||
let js = GetFileText('./FileTpl/js.txt').replace(/JsName/g, programOptions.fileName);
|
||
fs.writeFileSync(JsPath, js);
|
||
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)
|
||
} |