forked from microsoft/DefinitelyTyped-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typescript-installer.ts
88 lines (78 loc) · 2.75 KB
/
typescript-installer.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import assert = require("assert");
import { exec } from "child_process";
import * as fs from "fs-extra";
import * as os from "os";
import * as path from "path";
import * as process from "process";
import { TypeScriptVersion } from "@definitelytyped/typescript-versions";
export type TsVersion = TypeScriptVersion | "local";
const installsDir = path.join(os.homedir(), ".dts", "typescript-installs");
export async function installAllTypeScriptVersions() {
for (const v of TypeScriptVersion.shipped) {
await install(v);
}
// `shipped + [rc, next] == supported` during the RC period. During that time, typescript@rc needs to be installed too.
if (TypeScriptVersion.shipped.length + 2 === TypeScriptVersion.supported.length) {
await install("rc");
}
await installTypeScriptNext();
}
export async function installTypeScriptNext() {
await install("next");
}
async function install(version: TsVersion | "next" | "rc"): Promise<void> {
if (version === "local") {
return;
}
const dir = installDir(version);
if (!(await fs.pathExists(dir))) {
console.log(`Installing to ${dir}...`);
await fs.mkdirp(dir);
await fs.writeJson(path.join(dir, "package.json"), {
description: `Installs typescript@${version}`,
repository: "N/A",
license: "MIT",
dependencies: {
typescript: version,
},
});
await execAndThrowErrors("npm install --ignore-scripts --no-shrinkwrap --no-package-lock --no-bin-links", dir);
console.log("Installed!");
console.log("");
}
}
export function cleanTypeScriptInstalls(): Promise<void> {
return fs.remove(installsDir);
}
export function typeScriptPath(version: TsVersion, tsLocal: string | undefined): string {
if (version === "local") {
return tsLocal! + "/typescript.js";
}
return path.join(installDir(version), "node_modules", "typescript");
}
function installDir(version: TsVersion | "next" | "rc"): string {
assert(version !== "local");
if (version === "next") version = TypeScriptVersion.latest;
if (version === "rc") version = TypeScriptVersion.supported[TypeScriptVersion.supported.length - 2];
return path.join(installsDir, version);
}
/** Run a command and return the stdout, or if there was an error, throw. */
async function execAndThrowErrors(cmd: string, cwd?: string): Promise<void> {
// tslint:disable-next-line:promise-must-complete
return new Promise<void>((resolve, reject) => {
const env = { ...process.env };
if (env.NODE_OPTIONS && env.NODE_OPTIONS.includes("--require")) {
delete env.NODE_OPTIONS;
}
exec(cmd, { encoding: "utf8", cwd, env }, (err, _stdout, stderr) => {
if (stderr) {
console.error(stderr);
}
if (err) {
reject(err);
} else {
resolve();
}
});
});
}