|
| 1 | +/// <reference types="node"/> |
| 2 | +import { normalize, dirname, join } from "path"; |
| 3 | +import { readFileSync, writeFileSync, unlinkSync, existsSync } from "fs"; |
| 4 | +import assert = require("assert"); |
| 5 | +import { execSync } from "child_process"; |
| 6 | +const args = process.argv.slice(2); |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * A minimal description for a parsed package.json object. |
| 12 | + */ |
| 13 | +interface PackageJson { |
| 14 | + name: string; |
| 15 | + bin: {}; |
| 16 | + main: string; |
| 17 | +} |
| 18 | + |
| 19 | +// function exec(path: string, args: string[] = []) { |
| 20 | +// const cmdLine = ["node", path, ...args].join(" "); |
| 21 | +// console.log(cmdLine); |
| 22 | +// execSync(cmdLine); |
| 23 | +// } |
| 24 | + |
| 25 | +function main(): void { |
| 26 | + if (args.length < 1) { |
| 27 | + console.log("Usage:"); |
| 28 | + console.log("\tnode configureTSCBuild.js <package.json location>"); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // Acquire the version from the package.json file and modify it appropriately. |
| 33 | + const packageJsonFilePath = normalize(args[0]); |
| 34 | + const packageJsonValue: PackageJson = JSON.parse(readFileSync(packageJsonFilePath).toString()); |
| 35 | + |
| 36 | + // Remove the bin section from the current package |
| 37 | + delete packageJsonValue.bin; |
| 38 | + |
| 39 | + // Set the new name |
| 40 | + packageJsonValue.name = "@orta/tsc"; |
| 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, then remove the sibling JS files referenced above |
| 58 | + const lib = join(dirname(packageJsonFilePath), packageJsonValue.main); |
| 59 | + const libPath = dirname(lib); |
| 60 | + toRemove.forEach(file => { |
| 61 | + const path = join(libPath, file); |
| 62 | + if (existsSync(path)) unlinkSync(path); |
| 63 | + }); |
| 64 | + |
| 65 | + /////////////////////////////////// |
| 66 | + |
| 67 | + // This section verifies that the build of TypeScript compiles and emits |
| 68 | + |
| 69 | + const ts = require("../" + lib); |
| 70 | + const source = "let x: string = 'string'"; |
| 71 | + |
| 72 | + const results =ts.transpileModule(source, { |
| 73 | + compilerOptions: { module: ts.ModuleKind.CommonJS } |
| 74 | + }); |
| 75 | + console.log(Object.keys(results)); |
| 76 | + |
| 77 | + assert(results.outputText.trim() === "var x = 'string';", `Running typescript with ${packageJsonValue.name} did not return the expected results, got: ${results.outputText}`); |
| 78 | + |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +main(); |
0 commit comments