From 4a4c47a20b16653d151db9b55f2f674e8242a1fd Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Wed, 13 Jun 2018 10:43:37 -0700 Subject: [PATCH] Add produceLKG script --- scripts/produceLKG.ts | 92 ++++++++++++++++++++++++++++++++++++++++++- scripts/tsconfig.json | 19 +++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 scripts/tsconfig.json diff --git a/scripts/produceLKG.ts b/scripts/produceLKG.ts index 476187bfee8..5bf3d3a5785 100644 --- a/scripts/produceLKG.ts +++ b/scripts/produceLKG.ts @@ -1,4 +1,94 @@ /// -const fs = require('fs'); +import childProcess = require('child_process'); +import fs = require('fs-extra'); +import path = require('path'); +import removeInternal = require('remove-internal'); +import glob = require('glob'); +const source = path.join(__dirname, "../built/local"); +const dest = path.join(__dirname, "../lib"); +const copyright = fs.readFileSync(path.join(__dirname, "../CopyrightNotice.txt"), "utf-8"); + +async function produceLKG() { + console.log(`Building LKG from ${source} to ${dest}`); + await copyLibFiles(); + await copyLocalizedDiagnostics(); + await buildProtocol(); + await copyScriptOutputs(); + await copyDeclarationOutputs(); + await writeGitAttributes(); +} + +async function copyLibFiles() { + await copyFilesWithGlob("lib?(.*).d.ts"); +} + +async function copyLocalizedDiagnostics() { + const dir = await fs.readdir(source); + for (const d of dir) { + const fileName = path.join(source, d); + if (fs.statSync(fileName).isDirectory()) { + if (d === 'tslint') continue; + await fs.copy(fileName, path.join(dest, d)); + } + } +} + +async function buildProtocol() { + const protocolScript = path.join(__dirname, "buildProtocol.js"); + if (!fs.existsSync(protocolScript)) { + throw new Error(`Expected protocol script ${protocolScript} to exist`); + } + + const protocolInput = path.join(__dirname, "../src/server/protocol.ts"); + const protocolServices = path.join(source, "typescriptServices.d.ts"); + const protocolOutput = path.join(dest, "protocol.d.ts"); + + console.log(`Building ${protocolOutput}...`); + await exec(protocolScript, [protocolInput, protocolServices, protocolOutput]); +} + +async function copyScriptOutputs() { + await copyWithCopyright("tsserver.js"); + await copyWithCopyright("tsc.js"); + await copyWithCopyright("watchGuard.js"); + await copyWithCopyright("cancellationToken.js"); + await copyWithCopyright("typingsInstaller.js"); +} + +async function copyDeclarationOutputs() { + await copyWithCopyright("typescript.d.ts"); + await copyWithCopyright("typescriptServices.d.ts"); + await copyWithCopyright("tsserverlibrary.d.ts"); +} + +async function writeGitAttributes() { + await fs.writeFile(path.join(dest, ".gitattributes"), `* text eol=lf`, "utf-8"); +} + +async function copyWithCopyright(fileName: string) { + const content = await fs.readFile(path.join(source, fileName), "utf-8"); + await fs.writeFile(path.join(dest, fileName), copyright + "\r\n" + content); +} + +async function copyFromBuiltLocal(fileName: string) { + await fs.copy(path.join(source, fileName), path.join(dest, fileName)); +} + +async function copyFilesWithGlob(pattern: string) { + const files = glob.sync(path.join(source, pattern)).map(f => path.basename(f)); + for (const f of files) { + await copyFromBuiltLocal(f); + } + console.log(`Copied ${files.length} files matching pattern ${pattern}`); +} + +async function exec(path: string, args: string[] = []) { + const cmdLine = ["node", path, ...args].join(" "); + console.log(cmdLine); + childProcess.execSync(cmdLine); +} + +process.on("unhandledRejection", err => { throw err; }); +produceLKG().then(() => console.log("Done"), err => { throw err; }); diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json new file mode 100644 index 00000000000..f45261895f8 --- /dev/null +++ b/scripts/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "removeComments": false, + "target": "es6", + "module": "commonjs", + "declaration": false, + "lib": [ + "es6", + "scripthost" + ], + "types": ["node"] + }, + "files": [ + "produceLKG.ts", + "buildProtocol.ts", + "processDiagnosticMessages.ts", + "generateLocalizedDiagnosticMessages.ts" + ] +} \ No newline at end of file