|
1 |
| -import assert = require("assert"); |
2 |
| -import { exec } from "child_process"; |
3 |
| -import * as fs from "fs"; |
4 |
| -import * as os from "os"; |
5 |
| -import * as path from "path"; |
6 |
| -import * as process from "process"; |
7 | 1 | import { TypeScriptVersion } from "@definitelytyped/typescript-versions";
|
| 2 | +import * as typeScriptPackages from "@definitelytyped/typescript-packages"; |
8 | 3 |
|
9 | 4 | export type TsVersion = TypeScriptVersion | "local";
|
10 | 5 |
|
11 |
| -const installsDir = path.join(os.homedir(), ".dts", "typescript-installs"); |
12 |
| - |
13 |
| -export async function installAllTypeScriptVersions() { |
14 |
| - for (const v of TypeScriptVersion.shipped) { |
15 |
| - await install(v); |
16 |
| - } |
17 |
| - // `shipped + [rc, next] == supported` during the RC period. During that time, typescript@rc needs to be installed too. |
18 |
| - if (TypeScriptVersion.shipped.length + 2 === TypeScriptVersion.supported.length) { |
19 |
| - await install("rc"); |
20 |
| - } |
21 |
| - await installTypeScriptNext(); |
22 |
| -} |
23 |
| - |
24 |
| -export async function installTypeScriptNext() { |
25 |
| - await install("next"); |
26 |
| -} |
27 |
| - |
28 |
| -export async function install(version: TsVersion | "next" | "rc"): Promise<void> { |
29 |
| - if (version === "local") { |
30 |
| - return; |
31 |
| - } |
32 |
| - const dir = installDir(version); |
33 |
| - if (!fs.existsSync(dir)) { |
34 |
| - console.log(`Installing to ${dir}...`); |
35 |
| - await fs.promises.mkdir(dir, { recursive: true }); |
36 |
| - await fs.promises.writeFile( |
37 |
| - path.join(dir, "package.json"), |
38 |
| - JSON.stringify({ |
39 |
| - description: `Installs typescript@${version}`, |
40 |
| - repository: "N/A", |
41 |
| - license: "MIT", |
42 |
| - dependencies: { |
43 |
| - typescript: version, |
44 |
| - }, |
45 |
| - }), |
46 |
| - ); |
47 |
| - await execAndThrowErrors("npm install --ignore-scripts --no-shrinkwrap --no-package-lock --no-bin-links", dir); |
48 |
| - console.log("Installed!"); |
49 |
| - console.log(""); |
50 |
| - } |
51 |
| -} |
52 |
| - |
53 |
| -export function cleanTypeScriptInstalls(): Promise<void> { |
54 |
| - return fs.promises.rm(installsDir, { recursive: true, force: true }); |
55 |
| -} |
56 |
| - |
57 |
| -export function typeScriptPath(version: TsVersion | "next" | "rc", tsLocal: string | undefined): string { |
| 6 | +export function typeScriptPath(version: TsVersion, tsLocal: string | undefined): string { |
58 | 7 | if (version === "local") {
|
59 | 8 | return tsLocal! + "/typescript.js";
|
60 | 9 | }
|
61 |
| - return path.join(installDir(version), "node_modules", "typescript"); |
62 |
| -} |
63 |
| - |
64 |
| -function installDir(version: TsVersion | "next" | "rc"): string { |
65 |
| - assert(version !== "local"); |
66 |
| - if (version === "next") version = TypeScriptVersion.latest; |
67 |
| - if (version === "rc") version = TypeScriptVersion.supported[TypeScriptVersion.supported.length - 2]; |
68 |
| - return path.join(installsDir, version); |
69 |
| -} |
70 |
| - |
71 |
| -/** Run a command and return the stdout, or if there was an error, throw. */ |
72 |
| -async function execAndThrowErrors(cmd: string, cwd?: string): Promise<void> { |
73 |
| - // tslint:disable-next-line:promise-must-complete |
74 |
| - return new Promise<void>((resolve, reject) => { |
75 |
| - const env = { ...process.env }; |
76 |
| - if (env.NODE_OPTIONS && env.NODE_OPTIONS.includes("--require")) { |
77 |
| - delete env.NODE_OPTIONS; |
78 |
| - } |
79 |
| - exec(cmd, { encoding: "utf8", cwd, env }, (err, _stdout, stderr) => { |
80 |
| - if (stderr) { |
81 |
| - console.error(stderr); |
82 |
| - } |
83 |
| - |
84 |
| - if (err) { |
85 |
| - reject(err); |
86 |
| - } else { |
87 |
| - resolve(); |
88 |
| - } |
89 |
| - }); |
90 |
| - }); |
| 10 | + return typeScriptPackages.resolve(version); |
91 | 11 | }
|
0 commit comments