-
Notifications
You must be signed in to change notification settings - Fork 43
/
agda-deps.mjs
executable file
·67 lines (53 loc) · 1.69 KB
/
agda-deps.mjs
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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
function extractImports(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
const imports = [];
for (const line of lines) {
if (line.trim().startsWith('--')) continue; // Skip comments
const match = line.match(/^\s*(open\s+)?(import)\s+([A-Za-z0-9_.-]+)/);
if (match) {
imports.push(match[3]);
}
}
return imports;
}
function resolveImportPath(importName) {
const parts = importName.split('.');
const possiblePath = path.join(process.cwd(), ...parts) + '.agda';
return fs.existsSync(possiblePath) ? possiblePath : null;
}
function getDependencies(filePath, recursive = false, visited = new Set()) {
const absolutePath = path.resolve(filePath);
if (visited.has(absolutePath)) return [];
visited.add(absolutePath);
const imports = extractImports(absolutePath);
let dependencies = imports
.map(resolveImportPath)
.filter(Boolean);
if (recursive) {
for (const dep of dependencies) {
dependencies = dependencies.concat(getDependencies(dep, true, visited));
}
}
return [...new Set(dependencies)];
}
function main() {
const args = process.argv.slice(2);
if (args.length < 1) {
console.error('Usage: agda-deps <file.agda> [-r|--recursive]');
process.exit(1);
}
const filePath = args[0];
const recursive = args.includes('-r') || args.includes('--recursive');
try {
const dependencies = getDependencies(filePath, recursive);
dependencies.forEach(dep => console.log(path.relative(process.cwd(), dep)));
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
}
main();