-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-utils.mjs
80 lines (60 loc) · 2.12 KB
/
node-utils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
import _glob from 'fast-glob';
import { fileURLToPath } from 'node:url';
import fs from 'fs';
import { dirname } from 'path';
import { parse as acornParse } from 'acorn';
export async function resolve(dirs, id, importer, subdir = '', excludeDir = '') {
//todo: search only inside dirs
const dir = dirname(importer);
let path = dir + (subdir ? '/**/' + subdir : '') + '/**/' + id;
const found = await glob(excludeDir ? [path, '!' + excludeDir] : path, { absolute: true });
if (!found.length) {
if (dir === '/') {
return;
}
//console.log(`NOT FOUND ${id} in ${dir}, looking in parent dir`);
return resolve(dirs, id, dirname(dir) + '/dummy.file', subdir, dir);
}
found.sort((a, b) => a.length - b.length); // use shortest path
return found[0];
}
export function glob(path, options = { absolute: true }) {
if (typeof path === 'string') {
path = [path];
}
// fucking Windows...
path = path.map(path => path.replaceAll('\\', '/'));
if (options.cwd) {
let dir = options.cwd;
if (dir.startsWith('file:/')) {
dir = fileURLToPath(dir);
if (!fs.lstatSync(dir).isDirectory()) {
dir = dirname(dir);
}
options.cwd = dir;
}
}
return _glob(path, options);
}
function getExports(file) {
const code = acornParse(fs.readFileSync(file).toString(), { sourceType: 'module', ecmaVersion: 'latest' });
return code.body
.filter(node => node.type === 'ExportNamedDeclaration')
.map(node => {
if (node.declaration.type === 'VariableDeclaration') {
return node.declaration.declarations[0].id.name;
} else if (node.declaration.type === 'FunctionDeclaration') {
return node.declaration.id.name;
}
}).filter(Boolean);
}
export async function globExports(pattern) {
const files = await glob(pattern, { absolute: true });
const out = {};
for (const file of files) {
for (const name of getExports(file)) {
out[name] = file;
}
}
return out;
}