-
Notifications
You must be signed in to change notification settings - Fork 14
/
routeTraverse.mjs
66 lines (62 loc) · 2.15 KB
/
routeTraverse.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
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import pkg from 'typescript';
const {transpile} = pkg;
const re = /import\('(.*)'\)/;
const cwd = process.cwd();
export async function traverseRoutes(startModulePart, path = '', result = []) {
try {
// const { routes } = await import(startModulePart);
const routes = await extractRoutes(startModulePart);
if (!routes) {
// console.log(`no routes in ${startModulePart}`);
return;
}
for (const r of routes) {
const imp = r.loadChildren?.toString() ?? r.loadComponent?.toString();
// console.log(await (r.loadChildren()));
if (r.path !== '**' && !r.path.includes(':')) {
const folder = re.exec(imp)?.[1];
if (!folder) {
return;
}
const cleanFolder = join(cwd, './src/app/', folder).replace(cwd, '');
const barePart = cleanFolder.substring(0, cleanFolder.lastIndexOf('/'));
result.push({
path: `${path}/${r.path}`,
modulePath: barePart,
});
if (r.loadChildren !== undefined) {
const modulePath = join(cwd , 'src/app/',folder+'.ts')
// console.log('travesing', modulePath);
await traverseRoutes(modulePath, `${path}/${r.path}`, result);
}
}
}
} catch (e) {
console.log(`error in ${startModulePart}`);
console.error(e);
}
// console.table(result);
return result;
}
async function extractRoutes(path) {
if (!existsSync(path)) {
console.log(`file not found ${path}`);
return [];;
}
const content = readFileSync(path, 'utf8'); // load ts fike into memory
const code = transpile(content, {
module: pkg.ModuleKind.ES2022,
}) // transpile to es2022 using typescript compiler
try {
const mod = await import(`data:application/javascript;base64,${btoa(code)}`) // import the transpiled code from a string
const routes = mod.routes || mod.default || [] // get the routes from the module (use default if routes is not found)
console.log(`found ${routes.length} routes in ${path}`)
return(routes)
} catch (e) {
// console.error(e);
console.log(`error in ${path}`);
}
return [];
}