1/// <reference types="node"/> 2import { normalize, dirname, join } from "path"; 3import { readFileSync, writeFileSync, unlinkSync, existsSync } from "fs"; 4import assert = require("assert"); 5import { execSync } from "child_process"; 6const args = process.argv.slice(2); 7 8/** 9 * A minimal description for a parsed package.json object. 10 */ 11interface PackageJson { 12 name: string; 13 bin: {}; 14 main: string; 15 scripts: { 16 prepare: string 17 postpublish: string 18 } 19} 20 21function main(): void { 22 if (args.length < 1) { 23 console.log("Usage:"); 24 console.log("\tnode configureTSCBuild.js <package.json location>"); 25 return; 26 } 27 28 // Acquire the version from the package.json file and modify it appropriately. 29 const packageJsonFilePath = normalize(args[0]); 30 const packageJsonValue: PackageJson = JSON.parse(readFileSync(packageJsonFilePath).toString()); 31 32 // Remove the bin section from the current package 33 delete packageJsonValue.bin; 34 // We won't be running eslint which would run before publishing 35 delete packageJsonValue.scripts.prepare; 36 // No infinite loops 37 delete packageJsonValue.scripts.postpublish; 38 39 // Set the new name 40 packageJsonValue.name = "@typescript/language-services"; 41 42 writeFileSync(packageJsonFilePath, JSON.stringify(packageJsonValue, /*replacer:*/ undefined, /*space:*/ 4)); 43 44 // Remove the files which aren't use when just using the API 45 const toRemove = [ 46 // JS Files 47 "tsserver.js", 48 "tsserverlibrary.js", 49 "typescriptServices.js", 50 "typingsInstaller.js", 51 "tsc.js", 52 // DTS files 53 "typescriptServices.d.ts", 54 "tsserverlibrary.d.ts" 55 ]; 56 57 // Get a link to the main dependency JS file 58 const lib = join(dirname(packageJsonFilePath), packageJsonValue.main); 59 const libPath = dirname(lib); 60 61 // Remove the sibling JS large files referenced above 62 toRemove.forEach(file => { 63 const path = join(libPath, file); 64 if (existsSync(path)) unlinkSync(path); 65 }); 66 67 // Remove VS-specific localization keys 68 execSync("rm -rf loc", { cwd: dirname(packageJsonFilePath) }); 69 70 // Remove runnable file reference 71 execSync("rm -rf bin", { cwd: dirname(packageJsonFilePath) }); 72 73 /////////////////////////////////// 74 75 // This section verifies that the build of TypeScript compiles and emits 76 77 const ts = require(lib); 78 const source = "let x: string = 'string'"; 79 80 const results = ts.transpileModule(source, { 81 compilerOptions: { module: ts.ModuleKind.CommonJS } 82 }); 83 84 assert(results.outputText.trim() === "var x = 'string';", `Running typescript with ${packageJsonValue.name} did not return the expected results, got: ${results.outputText}`); 85} 86 87main(); 88