first commit
This commit is contained in:
1
bin/service-code-generation/.pydio
Normal file
1
bin/service-code-generation/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
1331c103-912a-4f75-9045-03aaa69824f8
|
||||
1
bin/service-code-generation/config/.pydio
Normal file
1
bin/service-code-generation/config/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
8bb6e31a-650f-4f25-9e76-72e5118f6438
|
||||
6
bin/service-code-generation/config/index.js
Normal file
6
bin/service-code-generation/config/index.js
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
// 需要被监听的api地址路径
|
||||
apiListPath: "F:\\desktop\\vue-class-api-tsx\\src\\core\\config\\configure.config.ts",
|
||||
// 生成的service层代码存放路径
|
||||
outPutServicePath: "F:\\desktop\\vue-class-api-tsx\\src\\core\\service"
|
||||
}
|
||||
29
bin/service-code-generation/index.js
Normal file
29
bin/service-code-generation/index.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const config = require('./config')
|
||||
const readApiList = require('./utils/ReadApiList')
|
||||
const file = require('./utils/File')
|
||||
let ServerMap = new Map();
|
||||
// Map(3) {
|
||||
// 'Car' => Map(2) { 'IndexTest' => 'IndexTest', 'carList' => 'carList' },
|
||||
// 'Index' => Map(1) { 'reg' => 'reg' },
|
||||
// 'Order' => Map(1) { 'test' => 'test' }
|
||||
// }
|
||||
let main = async () => {
|
||||
let readApi = new readApiList(config.apiListPath)
|
||||
let res = await readApi.getFileList()
|
||||
|
||||
res.forEach(v => {
|
||||
// [ 'Car', 'IndexTest' ]
|
||||
let key = v.split(":")[0].split("_")
|
||||
if (!ServerMap.get(key[0])) {
|
||||
ServerMap.set(key[0], new Map())
|
||||
}
|
||||
ServerMap.get(key[0]).set(key[1],key[1])
|
||||
});
|
||||
|
||||
// 根据接口对象的配置文件,自动生成service文件和代码
|
||||
ServerMap.forEach((value,key) => {
|
||||
file.createdService(key,value)
|
||||
})
|
||||
}
|
||||
|
||||
main()
|
||||
1
bin/service-code-generation/utils/.pydio
Normal file
1
bin/service-code-generation/utils/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
7fcbe2a8-4ef1-42fd-8123-b5ed331cbd41
|
||||
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
|
||||
61
bin/service-code-generation/utils/ReadApiList.js
Normal file
61
bin/service-code-generation/utils/ReadApiList.js
Normal file
@@ -0,0 +1,61 @@
|
||||
const { createReadStream } = require('fs')
|
||||
|
||||
class ReadApiList {
|
||||
apiListPath = "";
|
||||
lineLength = 0;
|
||||
textArr = [];
|
||||
lineBuffer = Buffer.alloc(4096);
|
||||
text = "";
|
||||
|
||||
constructor(path) {
|
||||
this.apiListPath = path
|
||||
}
|
||||
|
||||
getFileList() {
|
||||
this.text = createReadStream(this.apiListPath)
|
||||
return new Promise((s, e) => {
|
||||
this.textArr = [];
|
||||
let status = false
|
||||
this.text.on('data', (data) => {
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
if (data[i] == 10 || data[i] == 13) {
|
||||
if (data[i] == 10) {
|
||||
try {
|
||||
var line = this.lineBuffer.slice(0, this.lineLength);
|
||||
if (line.length != 0) {
|
||||
let str = line.toString('utf8')
|
||||
.replace(/^\s+|\s+$/g, '')
|
||||
.replace(/\'/g, "")
|
||||
.replace(/,/g, "")
|
||||
|
||||
if (str.includes("start -")) {
|
||||
status = true
|
||||
continue;
|
||||
}
|
||||
if (str.includes("end -")) {
|
||||
status = false
|
||||
break;
|
||||
}
|
||||
|
||||
if (status) {
|
||||
// console.log("str: ", str.split(":"))
|
||||
this.textArr.push(str);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this.lineLength = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.lineBuffer[this.lineLength] = data[i];
|
||||
this.lineLength++;
|
||||
}
|
||||
}
|
||||
|
||||
s(this.textArr)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ReadApiList
|
||||
Reference in New Issue
Block a user