Files
编码猿 dc70b5fc26 rr
2025-08-29 22:07:01 +08:00

61 lines
2.1 KiB
JavaScript

const { writeFileSync, existsSync } = require("fs");
const config = require("../config");
class File {
createdService(ServiceName, ServiceMethods) {
console.log("ServiceName: ", ServiceName);
console.log("ServiceMethods: ", 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