-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint-exports.js
executable file
·143 lines (117 loc) · 4.31 KB
/
print-exports.js
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
#!/usr/bin/env node --stack-size=32768 --max-old-space-size=65536
const fs = require('fs');
const typescript = require('typescript');
const { resolve: resolvePath } = require('path');
const { compilerOptions } = require('./tsconfig.json');
const { Worker } = require('worker_threads');
function sumExports(filePath, exportPrefix) {
if (!exportPrefix) {
throw new Error('No export pattern provided');
}
const program = typescript.createProgram([filePath], {
...compilerOptions,
lib: ['lib.es2023.full.d.ts'],
target: typescript.ScriptTarget.ES2022,
module: typescript.ModuleKind.NodeNext,
moduleResolution: typescript.ModuleResolutionKind.NodeNext,
});
const sourceFile = program.getSourceFileByPath(filePath.toLowerCase());
const checker = program.getTypeChecker();
const sourceFileSymbol = checker.getSymbolAtLocation(sourceFile);
const moduleExports = checker
.getExportsOfModule(sourceFileSymbol)
.map((exp) => exp.name)
.filter((name) => name.startsWith(exportPrefix));
const results = [];
for (const exp of moduleExports) {
const worker = new Worker(
`(${() => {
const typescript = require('typescript');
const { compilerOptions } = require('./tsconfig.json');
const { parentPort, workerData } = require('worker_threads');
const program = typescript.createProgram([workerData.filePath], {
...compilerOptions,
lib: ['lib.es2023.full.d.ts'],
target: typescript.ScriptTarget.ES2022,
module: typescript.ModuleKind.NodeNext,
moduleResolution: typescript.ModuleResolutionKind.NodeNext,
});
const sourceFile = program.getSourceFileByPath(workerData.filePath.toLowerCase());
const checker = program.getTypeChecker();
const sourceFileSymbol = checker.getSymbolAtLocation(sourceFile);
const moduleExport = checker
.getExportsOfModule(sourceFileSymbol)
.find((exp) => exp.name === workerData.exportName);
if (!moduleExport) {
throw new Error('No export found');
}
const expType = checker.getTypeOfSymbolAtLocation(
moduleExport,
moduleExport.declarations[0],
);
parentPort.postMessage(
checker.typeToString(expType, undefined, typescript.TypeFormatFlags.NoTruncation),
);
}})()`,
{
workerData: { exportName: exp, filePath },
eval: true,
},
);
results.push(
new Promise((resolve) => {
worker.on('message', (msg) => {
console.log(`Received '${msg}' for ${exp}`);
resolve(parseInt(String(msg), 10));
});
}),
);
}
Promise.all(results).then((values) => {
const sum = values.reduce((a, b) => a + b);
console.log(`Sum of ${exportPrefix} exports: ${sum}`);
});
}
function printExports(filePath, exportName) {
const program = typescript.createProgram([filePath], {
...compilerOptions,
lib: ['lib.es2023.full.d.ts'],
target: typescript.ScriptTarget.ES2022,
module: typescript.ModuleKind.NodeNext,
moduleResolution: typescript.ModuleResolutionKind.NodeNext,
});
const sourceFile = program.getSourceFileByPath(filePath.toLowerCase());
const checker = program.getTypeChecker();
const sourceFileSymbol = checker.getSymbolAtLocation(sourceFile);
let moduleExports = checker.getExportsOfModule(sourceFileSymbol);
if (exportName) {
moduleExports = moduleExports.filter((exp) => exp.name === exportName);
}
for (const exp of moduleExports) {
const expType = checker.getTypeOfSymbolAtLocation(exp, exp.declarations[0]);
if (expType.isStringLiteral()) {
console.log(`${exp.name} = ${expType.value}`);
} else {
console.log(
`${exp.name} = ${checker.typeToString(
expType,
undefined,
typescript.TypeFormatFlags.NoTruncation,
)}`,
);
}
}
}
function main(command, pathArg, ...args) {
const path = command === 'sum' ? pathArg : command;
const filePath = resolvePath(path.endsWith('.ts') ? path : `src/day-${path.padStart(2, '0')}.ts`);
if (!fs.existsSync(filePath)) {
throw new Error(`File not found: ${filePath}`);
}
if (command === 'sum') {
sumExports(filePath, ...args);
} else {
printExports(filePath, pathArg, ...args);
}
}
main(...process.argv.slice(2));