-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.ts
57 lines (45 loc) · 1.75 KB
/
index.ts
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
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import ts from "typescript";
import fs from "fs";
import { TransformConfiguration, TransformState } from "./class/transformState";
import { transformFile } from "./transform/transformFile";
import { LoggerProvider } from "./class/logProvider";
const DEFAULTS: TransformConfiguration = {
enabled: true,
};
export default function transform(program: ts.Program, userConfiguration: TransformConfiguration) {
userConfiguration = { ...DEFAULTS, ...userConfiguration };
if (userConfiguration.environmentRequires) {
for (const [k, v] of Object.entries(userConfiguration.environmentRequires)) {
if (
(typeof v === "boolean" && process.env[k] === undefined) ||
(typeof v === "string" && process.env[k] !== v)
) {
userConfiguration.enabled = false;
}
}
}
if (process.argv.includes("--verbose")) {
userConfiguration.verbose = true;
}
const logger = new LoggerProvider(userConfiguration.verbose!, userConfiguration.verbose!);
if (logger.verbose) {
logger.write("\n");
}
logger.infoIfVerbose(userConfiguration.enabled ? "Enabling debug macro emit" : "Skipping over debug macro emit");
const printer = ts.createPrinter({});
return (context: ts.TransformationContext): ((file: ts.SourceFile) => ts.Node) => {
const state = new TransformState(program, context, userConfiguration, logger);
if (state.symbolProvider.moduleFile === undefined) {
return (file) => file;
}
return (file: ts.SourceFile) => {
const result = transformFile(state, file);
if (process.env.EMIT_OUTPUT) {
fs.writeFileSync(file.fileName.replace(/\.(ts)$/gm, ".ts-output"), printer.printFile(result));
}
return result;
};
};
}