100 lines
2.7 KiB
JavaScript
100 lines
2.7 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const root = process.cwd();
|
|
const tempDir = path.join(root, 'temp');
|
|
const serviceRoot = path.join(root, 'app', 'service');
|
|
|
|
function walk(dir) {
|
|
const out = [];
|
|
for (const name of fs.readdirSync(dir)) {
|
|
const full = path.join(dir, name);
|
|
const st = fs.statSync(full);
|
|
if (st.isDirectory()) out.push(...walk(full));
|
|
else if (name.endsWith('.js')) out.push(full);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function extractClassMethods(content) {
|
|
const methods = [];
|
|
const classIdx = content.indexOf('class ');
|
|
if (classIdx < 0) return methods;
|
|
const braceStart = content.indexOf('{', classIdx);
|
|
if (braceStart < 0) return methods;
|
|
|
|
let i = braceStart + 1;
|
|
let depth = 1;
|
|
let body = '';
|
|
while (i < content.length && depth > 0) {
|
|
const ch = content[i++];
|
|
if (ch === '{') depth++;
|
|
if (ch === '}') depth--;
|
|
if (depth > 0) body += ch;
|
|
}
|
|
|
|
let p = 0;
|
|
let nested = 0;
|
|
while (p < body.length) {
|
|
const ch = body[p];
|
|
if (ch === '{') { nested++; p++; continue; }
|
|
if (ch === '}') { nested--; p++; continue; }
|
|
|
|
if (nested === 0) {
|
|
const slice = body.slice(p);
|
|
const m = slice.match(/^\s*(?:async\s+)?([A-Za-z_$][\w$]*)\s*\(/);
|
|
if (m) {
|
|
const name = m[1];
|
|
if (name !== 'constructor') methods.push(name);
|
|
|
|
// move to method body end
|
|
p += m[0].length;
|
|
while (p < body.length && body[p] !== '{') p++;
|
|
if (body[p] === '{') {
|
|
p++;
|
|
let inMethod = 1;
|
|
while (p < body.length && inMethod > 0) {
|
|
if (body[p] === '{') inMethod++;
|
|
else if (body[p] === '}') inMethod--;
|
|
p++;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
p++;
|
|
}
|
|
|
|
return [...new Set(methods)];
|
|
}
|
|
|
|
const files = walk(serviceRoot)
|
|
.map(full => path.relative(root, full).replace(/\\/g, '/'))
|
|
.sort((a, b) => a.localeCompare(b));
|
|
|
|
const inventory = [];
|
|
for (const rel of files) {
|
|
const full = path.join(root, rel);
|
|
const content = fs.readFileSync(full, 'utf8');
|
|
const methods = extractClassMethods(content);
|
|
inventory.push({ file: rel, methodCount: methods.length, methods });
|
|
}
|
|
|
|
const now = new Date().toISOString();
|
|
let md = '';
|
|
md += `自动生成时间: ${now}\n\n`;
|
|
md += `扫描范围: app/service/**/*.js\n`;
|
|
md += `文件数: ${inventory.length}\n\n`;
|
|
for (const item of inventory) {
|
|
md += `- ${item.file} (${item.methodCount})\n`;
|
|
if (item.methods.length === 0) {
|
|
md += ` - <no-class-method-found>\n`;
|
|
} else {
|
|
md += ` - ${item.methods.join(', ')}\n`;
|
|
}
|
|
}
|
|
|
|
if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir, { recursive: true });
|
|
fs.writeFileSync(path.join(tempDir, 'tmp-service-method-inventory.md'), md, 'utf8');
|
|
console.log('WROTE temp/tmp-service-method-inventory.md');
|