-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathgen-project-map.js
More file actions
executable file
·193 lines (169 loc) · 6.64 KB
/
Copy pathgen-project-map.js
File metadata and controls
executable file
·193 lines (169 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env node
'use strict';
/**
* gen-project-map.js — SigMap v0.4
* Generates PROJECT_MAP.md with import graph, class hierarchy, and route table.
*
* Usage:
* node gen-project-map.js Generate PROJECT_MAP.md
* node gen-project-map.js --version Print version and exit
* node gen-project-map.js --help Print usage and exit
*
* Zero npm dependencies — runs on Node.js 18+ with nothing installed.
*/
const fs = require('fs');
const path = require('path');
const VERSION = '0.4.0';
const OUTPUT_FILE = 'PROJECT_MAP.md';
// ---------------------------------------------------------------------------
// CLI
// ---------------------------------------------------------------------------
const args = process.argv.slice(2);
function detectInvokedAs() {
const argv1 = process.argv[1] || '';
const base = path.basename(argv1);
const baseNoExt = base.endsWith('.js') ? base.slice(0, -3) : base;
if (argv1.includes('/_npx/') || argv1.includes('\\_npx\\') ||
argv1.includes('/.npm/_') || argv1.includes('\\.npm\\_')) {
return 'npx gen-project-map';
}
if (baseNoExt === 'gen-project-map') return 'gen-project-map';
return 'node gen-project-map.js';
}
if (args.includes('--version')) {
console.log(`gen-project-map.js v${VERSION}`);
process.exit(0);
}
if (args.includes('--help')) {
const cmd = detectInvokedAs();
console.log([
`SigMap project map generator v${VERSION} (${cmd})`,
'',
'Usage:',
` ${cmd} Generate PROJECT_MAP.md`,
` ${cmd} --version Print version and exit`,
` ${cmd} --help Print this message`,
'',
'Configuration: gen-context.config.json (same file as gen-context.js)',
' srcDirs — directories to scan (default: ["src","app","lib",...])',
' exclude — patterns to skip (default: ["node_modules",".git",...])',
' maxDepth — recursion limit (default: 6)',
'',
'Output: PROJECT_MAP.md (project root)',
].join('\n'));
process.exit(0);
}
// ---------------------------------------------------------------------------
// Config — reuse src/config/loader.js
// ---------------------------------------------------------------------------
const { loadConfig } = require('./src/config/loader');
// ---------------------------------------------------------------------------
// Compact inline file walker
// ---------------------------------------------------------------------------
const DEFAULT_EXCLUDE = new Set([
'node_modules', '.git', 'dist', 'build', 'out',
'__pycache__', '.next', 'coverage', 'target', 'vendor', '.context',
]);
function walkDir(dir, excludeSet, maxDepth, depth, results) {
if (depth > maxDepth) return;
let entries;
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch (_) { return; }
for (const entry of entries) {
if (excludeSet.has(entry.name)) continue;
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
walkDir(full, excludeSet, maxDepth, depth + 1, results);
} else if (entry.isFile()) {
results.push(full);
}
}
}
function buildFileList(cwd, srcDirs, exclude, maxDepth) {
const excludeSet = new Set([...DEFAULT_EXCLUDE, ...exclude]);
const files = [];
for (const dir of srcDirs) {
const abs = path.isAbsolute(dir) ? dir : path.join(cwd, dir);
if (!fs.existsSync(abs)) continue;
walkDir(abs, excludeSet, maxDepth, 0, files);
}
// Also check for files directly in root (routes, config files, etc.)
// but only when srcDirs are explicit; skip to avoid noise
return files;
}
// ---------------------------------------------------------------------------
// Analyzers (lazy-required so errors don't abort the run)
// ---------------------------------------------------------------------------
function runAnalyzer(name, files, cwd) {
try {
const mod = require(`./src/map/${name}`);
return mod.analyze(files, cwd) || '';
} catch (err) {
console.warn(`[sigmap] ${name} analyzer failed: ${err.message}`);
return '';
}
}
// ---------------------------------------------------------------------------
// Format PROJECT_MAP.md
// ---------------------------------------------------------------------------
function formatOutput(sections) {
const now = new Date().toISOString().slice(0, 10);
const lines = [
'# Project Map',
`<!-- Generated by gen-project-map.js v${VERSION} on ${now} -->`,
'',
];
const parts = [
{ key: 'imports', header: '### Import graph', content: sections.imports },
{ key: 'classes', header: '### Class hierarchy', content: sections.classes },
{ key: 'routes', header: '### Route table', content: sections.routes },
{ key: 'env', header: '### Environment variables', content: sections.env },
{ key: 'buildci', header: '### Build & CI', content: sections.buildci },
{ key: 'manifests', header: '### Config & manifests', content: sections.manifests },
{ key: 'migrations', header: '### Database migrations', content: sections.migrations },
];
for (const { header, content } of parts) {
lines.push(header);
lines.push('');
if (content) {
lines.push(content);
} else {
lines.push('_No entries found._');
}
lines.push('');
}
return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
function main() {
const cwd = process.cwd();
const config = loadConfig(cwd);
const { srcDirs, exclude, maxDepth } = config;
console.log(`[sigmap] scanning ${srcDirs.join(', ')} (maxDepth=${maxDepth}) …`);
const files = buildFileList(cwd, srcDirs, exclude, maxDepth);
if (files.length === 0) {
console.warn(`[sigmap] no source files found — check srcDirs in gen-context.config.json`);
} else {
console.log(`[sigmap] found ${files.length} source files`);
}
const sections = {
imports: runAnalyzer('import-graph', files, cwd),
classes: runAnalyzer('class-hierarchy', files, cwd),
routes: runAnalyzer('route-table', files, cwd),
env: runAnalyzer('env-schema', files, cwd),
buildci: runAnalyzer('build-ci', files, cwd),
manifests: runAnalyzer('config-manifest', files, cwd),
migrations: runAnalyzer('migrations', files, cwd),
};
const output = formatOutput(sections);
const outPath = path.join(cwd, OUTPUT_FILE);
try {
fs.writeFileSync(outPath, output, 'utf8');
console.log(`[sigmap] wrote ${OUTPUT_FILE} (${output.length} bytes)`);
} catch (err) {
console.error(`[sigmap] failed to write ${OUTPUT_FILE}: ${err.message}`);
process.exit(1);
}
}
main();