This commit is contained in:
编码猿
2025-08-29 22:07:01 +08:00
parent 08057d0276
commit dc70b5fc26
10 changed files with 169 additions and 56 deletions

View File

@@ -1,6 +1,6 @@
module.exports = {
// 需要被监听的api地址路径
apiListPath: "F:\\desktop\\vue-class-api-tsx\\src\\core\\config\\configure.config.ts",
apiListPath: "/Users/bmy/source/TsxVueClassApi/src/core/config/configure.config.ts",
// 生成的service层代码存放路径
outPutServicePath: "F:\\desktop\\vue-class-api-tsx\\src\\core\\service"
outPutServicePath: "/Users/bmy/source/TsxVueClassApi/src/core/service"
}

View File

@@ -8,22 +8,26 @@ let ServerMap = new Map();
// 'Order' => Map(1) { 'test' => 'test' }
// }
let main = async () => {
// 读取配置文件中,注释 start 和 end 中间的 对象代码
let readApi = new readApiList(config.apiListPath)
let res = await readApi.getFileList()
console.log("getFileList: ", res)
// 处理读取到的 字符串数组将多个key方法进行分类归入不同的class文件中
// 比如 Car_carList 和 Car_addCar应该同属 Car.service.ts
// carList 和 addCar 则为文件中class的方法
res.forEach(v => {
// [ 'Car', 'IndexTest' ]
let key = v.split(":")[0].split("_")
if (!ServerMap.get(key[0])) {
ServerMap.set(key[0], new Map())
}
ServerMap.set(key[0], new Map())
}
ServerMap.get(key[0]).set(key[1],key[1])
});
// 根据接口对象的配置文件自动生成service文件和代码
ServerMap.forEach((value,key) => {
file.createdService(key,value)
})
// 根据上面的输出结果自动生成service文件和代码
ServerMap.forEach((value,key) => file.createdService(key,value))
}
main()

View File

@@ -4,8 +4,10 @@ const config = require("../config");
class File {
createdService(ServiceName, ServiceMethods) {
let fullPath = `${config.outPutServicePath}\\${ServiceName}.service.ts`
if (!existsSync(fullPath)) {
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
@@ -29,7 +31,7 @@ class File {
}
}
`)
}
// }
}

View File

@@ -1,59 +1,55 @@
const { createReadStream } = require('fs')
class ReadApiList {
apiListPath = "";
lineLength = 0;
textArr = [];
lineBuffer = Buffer.alloc(4096);
text = "";
status = false;
lineBuffer = [];
lineLength = 0;
stream = null
constructor(path) {
this.apiListPath = path
this.stream = createReadStream(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, "")
return new Promise((resolve, reject) => {
this.stream.on('data', (data) => {
for (let i = 0; i < data.length; i++) {
const byte = data[i];
if (byte === 10 || byte === 13) { // 处理 \n 和 \r
if (this.lineLength > 0) {
const line = Buffer.from(this.lineBuffer.slice(0, this.lineLength)).toString('utf8');
const cleanedLine = line.trim().replace(/'|,/g, '');
if (str.includes("start -")) {
status = true
continue;
}
if (str.includes("end -")) {
status = false
break;
}
if (cleanedLine.includes("start -")) {
this.status = true;
} else if (cleanedLine.includes("end -")) {
this.status = false;
if (status) {
// console.log("str: ", str.split(":"))
this.textArr.push(str);
}
}
} finally {
this.lineLength = 0;
// 去除被注释的key代码
} else if (this.status && !cleanedLine.includes("//")) {
this.textArr.push(cleanedLine);
}
}
this.lineLength = 0; // 重置行缓冲区
} else {
this.lineBuffer[this.lineLength] = data[i];
if (this.lineLength >= this.lineBuffer.length) {
this.lineBuffer.push(byte);
} else {
this.lineBuffer[this.lineLength] = byte;
}
this.lineLength++;
}
}
});
s(this.textArr)
})
this.stream.on('end', () => {
resolve(this.textArr);
});
this.stream.on('error', (err) => {
reject(err);
});
})
}
}