web-backset.cn/apps/server/script/new.mvc.js

64 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-02-11 13:21:06 +08:00
/* eslint-disable @typescript-eslint/no-var-requires */
const { writeFile } = require('fs/promises');
const inquirer = require('inquirer');
const { join } = require('path');
const chalk = require('chalk');
const upper1st = (word) => word.charAt(0).toUpperCase() + word.slice(1);
const controller = (name) => `import { Controller } from '@nestjs/common';
import { ${upper1st(name)}Service } from 'src/service/${name}.service';
@Controller()
export class ${upper1st(name)}Controller {
constructor(private readonly ${name}Service: ${upper1st(name)}Service) {}
}
`;
const mod = (name) => `import { Module } from '@nestjs/common';
import { ${upper1st(name)}Controller } from 'src/controller/${name}.controller';
import { ${upper1st(name)}Service } from 'src/service/${name}.service';
@Module({
controllers: [${upper1st(name)}Controller],
providers: [${upper1st(name)}Service],
})
export class ${upper1st(name)}Module {}
`;
const impl = (name) => `export interface ${upper1st(name)}ServiceImpl {}`;
const service = (name) => `import { Injectable } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import { ${upper1st(name)}ServiceImpl } from './impl/${name}.interface';
@Injectable()
export class ${upper1st(name)}Service implements ${upper1st(name)}ServiceImpl {
constructor(@InjectDataSource() private db: DataSource) {}
}
`;
inquirer
.prompt([
{
type: 'input',
name: 'name',
message: '输入模块名称',
},
])
.then(async (res) => {
const { name } = res;
const controllerDir = join(process.cwd(), 'src/controller');
const moduleDir = join(process.cwd(), 'src/module');
const implDir = join(process.cwd(), 'src/service/impl');
const serviceDir = join(process.cwd(), 'src/service');
await writeFile(`${controllerDir}/${name}.controller.ts`, controller(name));
await writeFile(`${moduleDir}/${name}.module.ts`, mod(name));
await writeFile(`${implDir}/${name}.interface.ts`, impl(name));
await writeFile(`${serviceDir}/${name}.service.ts`, service(name));
console.log(
chalk.blue(`! 请在 app.module.ts 中imports: [${upper1st(name)}Module]`),
);
});