-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpile.mjs
60 lines (50 loc) · 1.58 KB
/
transpile.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
import assert from "node:assert";
import path from "node:path";
import fs from "fs-extra";
import ts from "typescript";
async function transpileProject({ project, outDir }) {
const { config } = ts.readConfigFile(project, ts.sys.readFile);
assert.notStrictEqual(config, undefined);
const basePath = path.resolve(path.dirname(project));
const {
options,
fileNames: rootNames,
projectReferences,
errors,
} = ts.parseJsonConfigFileContent(config, ts.sys, basePath);
if (errors.length > 0) {
console.error(errors);
process.exit(1);
}
const program = ts.createProgram({
rootNames,
options,
host: ts.createCompilerHost(options),
projectReferences,
});
await Promise.all(
program
.getSourceFiles()
.filter((file) => !file.isDeclarationFile)
.map(async (file) => {
const declarationFile = ts.transpileDeclaration(file.text, {
compilerOptions: options,
reportDiagnostics: true,
});
if ((declarationFile.diagnostics ?? []).length > 0) {
console.error(declarationFile.diagnostics);
process.exit(1);
}
const relativeFilePath = path.relative(basePath, file.fileName);
const declarationPath = path.join(
outDir,
relativeFilePath.replace(/\.tsx?$/, ".d.ts"),
);
await fs.ensureDir(path.dirname(declarationPath));
await fs.writeFile(declarationPath, declarationFile.outputText, {
encoding: "utf8",
});
}),
);
}
transpileProject({ project: process.argv[2], outDir: process.argv[3] });