57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
const { createReadStream } = require('fs')
|
|
|
|
class ReadApiList {
|
|
textArr = [];
|
|
status = false;
|
|
lineBuffer = [];
|
|
lineLength = 0;
|
|
stream = null
|
|
|
|
constructor(path) {
|
|
this.stream = createReadStream(path);
|
|
}
|
|
|
|
getFileList() {
|
|
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 (cleanedLine.includes("start -")) {
|
|
this.status = true;
|
|
} else if (cleanedLine.includes("end -")) {
|
|
this.status = false;
|
|
|
|
// 去除被注释的key代码
|
|
} else if (this.status && !cleanedLine.includes("//")) {
|
|
this.textArr.push(cleanedLine);
|
|
}
|
|
}
|
|
this.lineLength = 0; // 重置行缓冲区
|
|
} else {
|
|
if (this.lineLength >= this.lineBuffer.length) {
|
|
this.lineBuffer.push(byte);
|
|
} else {
|
|
this.lineBuffer[this.lineLength] = byte;
|
|
}
|
|
this.lineLength++;
|
|
}
|
|
}
|
|
});
|
|
|
|
this.stream.on('end', () => {
|
|
resolve(this.textArr);
|
|
});
|
|
|
|
this.stream.on('error', (err) => {
|
|
reject(err);
|
|
});
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = ReadApiList |