Skip to content

Commit 4a4c47a

Browse files
committed
Add produceLKG script
1 parent 645f562 commit 4a4c47a

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

scripts/produceLKG.ts

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,94 @@
11
/// <reference types="node" />
22

3-
const fs = require('fs');
3+
import childProcess = require('child_process');
4+
import fs = require('fs-extra');
5+
import path = require('path');
6+
import removeInternal = require('remove-internal');
7+
import glob = require('glob');
48

9+
const source = path.join(__dirname, "../built/local");
10+
const dest = path.join(__dirname, "../lib");
11+
const copyright = fs.readFileSync(path.join(__dirname, "../CopyrightNotice.txt"), "utf-8");
12+
13+
async function produceLKG() {
14+
console.log(`Building LKG from ${source} to ${dest}`);
15+
await copyLibFiles();
16+
await copyLocalizedDiagnostics();
17+
await buildProtocol();
18+
await copyScriptOutputs();
19+
await copyDeclarationOutputs();
20+
await writeGitAttributes();
21+
}
22+
23+
async function copyLibFiles() {
24+
await copyFilesWithGlob("lib?(.*).d.ts");
25+
}
26+
27+
async function copyLocalizedDiagnostics() {
28+
const dir = await fs.readdir(source);
29+
for (const d of dir) {
30+
const fileName = path.join(source, d);
31+
if (fs.statSync(fileName).isDirectory()) {
32+
if (d === 'tslint') continue;
33+
await fs.copy(fileName, path.join(dest, d));
34+
}
35+
}
36+
}
37+
38+
async function buildProtocol() {
39+
const protocolScript = path.join(__dirname, "buildProtocol.js");
40+
if (!fs.existsSync(protocolScript)) {
41+
throw new Error(`Expected protocol script ${protocolScript} to exist`);
42+
}
43+
44+
const protocolInput = path.join(__dirname, "../src/server/protocol.ts");
45+
const protocolServices = path.join(source, "typescriptServices.d.ts");
46+
const protocolOutput = path.join(dest, "protocol.d.ts");
47+
48+
console.log(`Building ${protocolOutput}...`);
49+
await exec(protocolScript, [protocolInput, protocolServices, protocolOutput]);
50+
}
51+
52+
async function copyScriptOutputs() {
53+
await copyWithCopyright("tsserver.js");
54+
await copyWithCopyright("tsc.js");
55+
await copyWithCopyright("watchGuard.js");
56+
await copyWithCopyright("cancellationToken.js");
57+
await copyWithCopyright("typingsInstaller.js");
58+
}
59+
60+
async function copyDeclarationOutputs() {
61+
await copyWithCopyright("typescript.d.ts");
62+
await copyWithCopyright("typescriptServices.d.ts");
63+
await copyWithCopyright("tsserverlibrary.d.ts");
64+
}
65+
66+
async function writeGitAttributes() {
67+
await fs.writeFile(path.join(dest, ".gitattributes"), `* text eol=lf`, "utf-8");
68+
}
69+
70+
async function copyWithCopyright(fileName: string) {
71+
const content = await fs.readFile(path.join(source, fileName), "utf-8");
72+
await fs.writeFile(path.join(dest, fileName), copyright + "\r\n" + content);
73+
}
74+
75+
async function copyFromBuiltLocal(fileName: string) {
76+
await fs.copy(path.join(source, fileName), path.join(dest, fileName));
77+
}
78+
79+
async function copyFilesWithGlob(pattern: string) {
80+
const files = glob.sync(path.join(source, pattern)).map(f => path.basename(f));
81+
for (const f of files) {
82+
await copyFromBuiltLocal(f);
83+
}
84+
console.log(`Copied ${files.length} files matching pattern ${pattern}`);
85+
}
86+
87+
async function exec(path: string, args: string[] = []) {
88+
const cmdLine = ["node", path, ...args].join(" ");
89+
console.log(cmdLine);
90+
childProcess.execSync(cmdLine);
91+
}
92+
93+
process.on("unhandledRejection", err => { throw err; });
94+
produceLKG().then(() => console.log("Done"), err => { throw err; });

scripts/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"removeComments": false,
4+
"target": "es6",
5+
"module": "commonjs",
6+
"declaration": false,
7+
"lib": [
8+
"es6",
9+
"scripthost"
10+
],
11+
"types": ["node"]
12+
},
13+
"files": [
14+
"produceLKG.ts",
15+
"buildProtocol.ts",
16+
"processDiagnosticMessages.ts",
17+
"generateLocalizedDiagnosticMessages.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)