|
1 | 1 | /// <reference types="node" /> |
2 | 2 |
|
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'); |
4 | 8 |
|
| 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; }); |
0 commit comments