-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.ts
66 lines (59 loc) · 1.79 KB
/
context.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
58
59
60
61
62
63
64
65
66
import path from "node:path";
import {
replaceSpecifiers,
transpileSpecifier,
} from "./_transformations/specifiers.ts";
import { type Config, parse } from "./config.ts";
import { Project, ts } from "./deps.deno.ts";
const compilerOptions: ts.CompilerOptions = {
removeComments: false,
strict: true,
useDefineForClassFields: true,
};
interface Options {
readonly tsConfigFilePath?: string;
readonly compilerOptions?: ts.CompilerOptions;
readonly skipAddingFilesFromTsConfig?: boolean;
}
export class Context {
public baseDir: string;
public config: Config;
readonly project: Project;
private _runtime = "deno";
/**
* Synchronously loads `tsconfig.json` and `"files"`.
*/
constructor(options: Options) {
const { tsConfigFilePath } = options;
this.project = new Project({
compilerOptions,
...options,
});
const fs = this.project.getFileSystem();
if (tsConfigFilePath === undefined) {
this.baseDir = fs.getCurrentDirectory();
this.config = {};
return;
}
const result = ts.readConfigFile(tsConfigFilePath, fs.readFileSync);
this.baseDir = path.resolve(tsConfigFilePath, "../");
this.config = parse(result.config.deno2node ?? {});
}
resolve(...pathSegments: string[]) {
return path.join(this.baseDir, ...pathSegments);
}
changeRuntimeTo(newRuntime: string) {
const label = `Changing runtime from ${this._runtime} to ${newRuntime}`;
const fn = transpileSpecifier(this._runtime, newRuntime);
console.time(label);
for (const sourceFile of this.project.getSourceFiles()) {
if (sourceFile.getBaseName().includes(`.${this._runtime}.`)) {
sourceFile.forget();
} else {
replaceSpecifiers(sourceFile, fn);
}
}
this._runtime = newRuntime;
console.timeEnd(label);
}
}