first commit
This commit is contained in:
1
bin/.pydio
Normal file
1
bin/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
a295b73d-da90-4c93-9a60-86b6ef15a2b9
|
||||
1
bin/FileTpl/.pydio
Normal file
1
bin/FileTpl/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
aaea7bf8-ca33-4943-982b-b06379a36042
|
||||
9
bin/FileTpl/conterller.txt
Normal file
9
bin/FileTpl/conterller.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
import {Controller, Render, Get, UseBefore, Session} from "routing-controllers";
|
||||
import { utils } from "../../utils/utils";
|
||||
import { Service, Inject } from "typedi";
|
||||
|
||||
@Service()
|
||||
@Controller()
|
||||
export class ControllerName {
|
||||
|
||||
}
|
||||
7
bin/FileTpl/dao.txt
Normal file
7
bin/FileTpl/dao.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Service , Inject } from "typedi";
|
||||
import { getRepository } from "typeorm";
|
||||
|
||||
@Service()
|
||||
export class DaoName {
|
||||
|
||||
}
|
||||
9
bin/FileTpl/entity.txt
Normal file
9
bin/FileTpl/entity.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
|
||||
|
||||
@Entity("user")
|
||||
export class EntityName {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
user_id: number;
|
||||
|
||||
}
|
||||
14
bin/FileTpl/impl.txt
Normal file
14
bin/FileTpl/impl.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
import { UserService } from "../UserService";
|
||||
import { Service, Inject } from "typedi";
|
||||
import { UserDao } from "../../dao/UserDao";
|
||||
|
||||
@Service()
|
||||
export class ImplName implements ServiceName {
|
||||
|
||||
@Inject()
|
||||
private userDao: UserDao;
|
||||
|
||||
public async getAllUserService(): Promise<any> {
|
||||
return await this.userDao.AllUser();
|
||||
}
|
||||
}
|
||||
10
bin/FileTpl/jade.txt
Normal file
10
bin/FileTpl/jade.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
extends ./../layout
|
||||
|
||||
block title
|
||||
|
||||
meta(name="description" content="关于我们,企业联系,课程咨询,问题答疑")
|
||||
meta(name="keywords" content="编码猿,关于我们,怪客学堂")
|
||||
|
||||
block content
|
||||
.about(class="pages JadeName" data-module="JadeName")
|
||||
.container 测试
|
||||
12
bin/FileTpl/js.txt
Normal file
12
bin/FileTpl/js.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
define(['jquery','utils'],function ($,utils) {
|
||||
function JsName() {
|
||||
|
||||
}
|
||||
|
||||
JsName.prototype = {
|
||||
init () {
|
||||
}
|
||||
};
|
||||
|
||||
return new JsName();
|
||||
});
|
||||
3
bin/FileTpl/less.txt
Normal file
3
bin/FileTpl/less.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
.ClassName {
|
||||
color: red;
|
||||
}
|
||||
8
bin/FileTpl/model.txt
Normal file
8
bin/FileTpl/model.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
import { MinLength,IsInt, MaxLength,Allow,Validate } from 'class-validator';
|
||||
|
||||
export class ModelName {
|
||||
@MinLength(1,{
|
||||
message: 'id参数必须有'
|
||||
})
|
||||
id: string;
|
||||
}
|
||||
3
bin/FileTpl/service.txt
Normal file
3
bin/FileTpl/service.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface ServiceName {
|
||||
getOneUserService(id: string) : Promise<any>;
|
||||
}
|
||||
177
bin/kinit
Executable file
177
bin/kinit
Executable file
@@ -0,0 +1,177 @@
|
||||
#!/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)
|
||||
}
|
||||
Reference in New Issue
Block a user