-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·95 lines (75 loc) · 2.07 KB
/
index.js
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
89
90
91
92
93
94
95
#!/usr/bin/env node
const fs = require("fs");
const { ncp } = require("ncp");
const path = require("path");
const yargs = require("yargs");
const rimraf = require("rimraf");
if (!fs.existsSync(path.join("node_modules", "typescript"))) {
console.error("typescript not installed in node_modules");
process.exit(1);
}
const argv = yargs
.option("memory", {
alias: "m",
describe: "Memory limit for tsserver in kb"
})
.option("destination", {
alias: "d",
describe: "Destination directory (default .vscode)"
})
.demandOption(["memory"])
.help().argv;
const memory = argv.memory;
if (!Number.isFinite(memory)) {
console.error("Memory value must be a number");
process.exit(1);
}
if (memory <= 0) {
console.error("Memory value must a positive number");
process.exit(1);
}
const destination = argv.destination || ".vscode";
const BRIDGE_TEMPLATE = `
const { spawn } = require("child_process");
const path = require("path");
const nextArgv = process.argv.slice(2);
nextArgv.unshift(path.resolve(__dirname, "./_tsserver.js"));
nextArgv.unshift("--max_old_space_size=${memory}");
spawn("node", nextArgv, {
cwd: process.cwd(),
env: process.env,
stdio: "inherit"
});
`;
function copyTypescript() {
return new Promise((resolve, reject) => {
const source = path.join("node_modules", "typescript");
const dest = path.join(destination, "typescript");
if (fs.existsSync(dest)) {
rimraf.sync(dest);
}
ncp(source, dest, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
function renameTsserver() {
const oldPath = path.join(destination, "typescript", "lib", "tsserver.js");
const newPath = path.join(destination, "typescript", "lib", "_tsserver.js");
fs.renameSync(oldPath, newPath);
}
function writeBridgeFile() {
const filePath = path.join(destination, "typescript", "lib", "tsserver.js");
fs.writeFileSync(filePath, BRIDGE_TEMPLATE);
}
async function run() {
await copyTypescript();
renameTsserver();
writeBridgeFile();
console.info("Created typescript bridge");
}
run();