first commit
This commit is contained in:
59
bin/service-code-generation/utils/File.js
Normal file
59
bin/service-code-generation/utils/File.js
Normal file
@@ -0,0 +1,59 @@
|
||||
const { writeFileSync, existsSync } = require("fs");
|
||||
const config = require("../config");
|
||||
|
||||
class File {
|
||||
|
||||
createdService(ServiceName, ServiceMethods) {
|
||||
let fullPath = `${config.outPutServicePath}\\${ServiceName}.service.ts`
|
||||
if (!existsSync(fullPath)) {
|
||||
let mArray = Array.from(ServiceMethods, ([name, value]) => ({ name, value }));
|
||||
writeFileSync(fullPath, this.stripIndent`
|
||||
// @ts-nocheck
|
||||
import { GET, Mapper, POST, PUT, DELETE } from "@ann/Http.annotation";
|
||||
import { property, defaultValue, deserialize } from "@ann/JsonToClass";
|
||||
|
||||
// 建议用工具json-class-desktop生成这个类 类型
|
||||
export class ResponseType {
|
||||
@property("id")
|
||||
user_id!: number;
|
||||
}
|
||||
|
||||
export class ${ServiceName}Service {
|
||||
${mArray.map(v => {
|
||||
return `
|
||||
@GET()
|
||||
@Mapper(ResponseType)
|
||||
public async ${v.name} (params: object): Promise<ResponseType[]> {}
|
||||
`
|
||||
}).toString().replaceAll(",", "")
|
||||
}
|
||||
}
|
||||
`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stripIndent(template, ...expressions) {
|
||||
let result = template.reduce((prev, next, i) => {
|
||||
let expression = expressions[i - 1];
|
||||
if ( Array.isArray(expression)) {
|
||||
expression = expression.join('');
|
||||
}
|
||||
return prev + expression + next;
|
||||
});
|
||||
|
||||
const match = result.match(/^[^\S\n]*(?=\S)/gm);
|
||||
const indent = match && Math.min(...match.map(el => el.length));
|
||||
|
||||
if (indent) {
|
||||
const regexp = new RegExp(`^.{${indent}}`, 'gm');
|
||||
result = result.replace(regexp, '');
|
||||
}
|
||||
|
||||
result = result.replace(/^[^\S\n]+/gm, ' ');
|
||||
result = result.trim();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new File
|
||||
Reference in New Issue
Block a user