From 3fe7aafb5bbb14da9ec20c7e5c01686e6f972eed Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Tue, 24 Sep 2019 13:11:08 -0400 Subject: [PATCH] Add a command for stripping the dependency down --- Gulpfile.js | 4 ++ package.json | 1 + scripts/configureTSCBuild.js | 57 +++++++++++++++++++++++++ scripts/configureTSCBuild.ts | 82 ++++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 scripts/configureTSCBuild.js create mode 100644 scripts/configureTSCBuild.ts diff --git a/Gulpfile.js b/Gulpfile.js index 1f930e3b019..7ed50e25beb 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -588,6 +588,10 @@ const configureExperimental = () => exec(process.execPath, ["scripts/configurePr task("configure-experimental", series(buildScripts, configureExperimental)); task("configure-experimental").description = "Runs scripts/configurePrerelease.ts to prepare a build for experimental publishing"; +const configureTSCOnly = () => exec(process.execPath, ["scripts/configureTSCBuild.js", "package.json", "src/compiler/core.ts"]); +task("configure-tsc-only", series(buildScripts, configureNightly)); +task("configure-tsc-only").description = "Runs scripts/configureTSCOnly.ts to prepare a build for build which only has tsc "; + const publishNightly = () => exec("npm", ["publish", "--tag", "next"]); task("publish-nightly", series(task("clean"), task("LKG"), task("clean"), task("runtests-parallel"), publishNightly)); task("publish-nightly").description = "Runs `npm publish --tag next` to create a new nightly build on npm"; diff --git a/package.json b/package.json index 7a59b95a130..a61588734db 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,7 @@ "scripts": { "prepare": "gulp build-eslint-rules", "pretest": "gulp tests", + "postpublish": "gulp configure-tsc-only", "test": "gulp runtests-parallel --light=false", "test:eslint-rules": "gulp run-eslint-rules-tests", "build": "npm run build:compiler && npm run build:tests", diff --git a/scripts/configureTSCBuild.js b/scripts/configureTSCBuild.js new file mode 100644 index 00000000000..3eb7e9ee935 --- /dev/null +++ b/scripts/configureTSCBuild.js @@ -0,0 +1,57 @@ +"use strict"; +exports.__esModule = true; +/// +var path_1 = require("path"); +var fs_1 = require("fs"); +var assert = require("assert"); +var args = process.argv.slice(2); +// function exec(path: string, args: string[] = []) { +// const cmdLine = ["node", path, ...args].join(" "); +// console.log(cmdLine); +// execSync(cmdLine); +// } +function main() { + if (args.length < 1) { + console.log("Usage:"); + console.log("\tnode configureTSCBuild.js "); + return; + } + // Acquire the version from the package.json file and modify it appropriately. + var packageJsonFilePath = path_1.normalize(args[0]); + var packageJsonValue = JSON.parse(fs_1.readFileSync(packageJsonFilePath).toString()); + // Remove the bin section from the current package + delete packageJsonValue.bin; + // Set the new name + packageJsonValue.name = "@orta/tsc"; + fs_1.writeFileSync(packageJsonFilePath, JSON.stringify(packageJsonValue, /*replacer:*/ undefined, /*space:*/ 4)); + // Remove the files which aren't use when just using the API + var toRemove = [ + // JS Files + "tsserver.js", + "tsserverlibrary.js", + "typescriptServices.js", + "typingsInstaller.js", + "tsc.js", + // DTS files + "typescriptServices.d.ts", + "tsserverlibrary.d.ts" + ]; + // Get a link to the main dependency JS file, then remove the sibling JS files referenced above + var lib = path_1.join(path_1.dirname(packageJsonFilePath), packageJsonValue.main); + var libPath = path_1.dirname(lib); + toRemove.forEach(function (file) { + var path = path_1.join(libPath, file); + if (fs_1.existsSync(path)) + fs_1.unlinkSync(path); + }); + /////////////////////////////////// + // This section verifies that the build of TypeScript compiles and emits + var ts = require("../" + lib); + var source = "let x: string = 'string'"; + var results = ts.transpileModule(source, { + compilerOptions: { module: ts.ModuleKind.CommonJS } + }); + console.log(Object.keys(results)); + assert(results.outputText.trim() === "var x = 'string';", "Running typescript with " + packageJsonValue.name + " did not return the expected results, got: " + results.outputText); +} +main(); diff --git a/scripts/configureTSCBuild.ts b/scripts/configureTSCBuild.ts new file mode 100644 index 00000000000..8fd76eab1bb --- /dev/null +++ b/scripts/configureTSCBuild.ts @@ -0,0 +1,82 @@ +/// +import { normalize, dirname, join } from "path"; +import { readFileSync, writeFileSync, unlinkSync, existsSync } from "fs"; +import assert = require("assert"); +import { execSync } from "child_process"; +const args = process.argv.slice(2); + + + +/** + * A minimal description for a parsed package.json object. + */ +interface PackageJson { + name: string; + bin: {}; + main: string; +} + +// function exec(path: string, args: string[] = []) { +// const cmdLine = ["node", path, ...args].join(" "); +// console.log(cmdLine); +// execSync(cmdLine); +// } + +function main(): void { + if (args.length < 1) { + console.log("Usage:"); + console.log("\tnode configureTSCBuild.js "); + return; + } + + // Acquire the version from the package.json file and modify it appropriately. + const packageJsonFilePath = normalize(args[0]); + const packageJsonValue: PackageJson = JSON.parse(readFileSync(packageJsonFilePath).toString()); + + // Remove the bin section from the current package + delete packageJsonValue.bin; + + // Set the new name + packageJsonValue.name = "@orta/tsc"; + + writeFileSync(packageJsonFilePath, JSON.stringify(packageJsonValue, /*replacer:*/ undefined, /*space:*/ 4)); + + // Remove the files which aren't use when just using the API + const toRemove = [ + // JS Files + "tsserver.js", + "tsserverlibrary.js", + "typescriptServices.js", + "typingsInstaller.js", + "tsc.js", + // DTS files + "typescriptServices.d.ts", + "tsserverlibrary.d.ts" + ]; + + // Get a link to the main dependency JS file, then remove the sibling JS files referenced above + const lib = join(dirname(packageJsonFilePath), packageJsonValue.main); + const libPath = dirname(lib); + toRemove.forEach(file => { + const path = join(libPath, file); + if (existsSync(path)) unlinkSync(path); + }); + + /////////////////////////////////// + + // This section verifies that the build of TypeScript compiles and emits + + const ts = require("../" + lib); + const source = "let x: string = 'string'"; + + const results =ts.transpileModule(source, { + compilerOptions: { module: ts.ModuleKind.CommonJS } + }); + console.log(Object.keys(results)); + + assert(results.outputText.trim() === "var x = 'string';", `Running typescript with ${packageJsonValue.name} did not return the expected results, got: ${results.outputText}`); + + +} + +main();