From c85bea90c46aa5566e5107fa9e1625b5e6b47625 Mon Sep 17 00:00:00 2001 From: Joeri van Oostveen Date: Wed, 1 Sep 2021 16:17:11 +0200 Subject: [PATCH 1/4] Switching to tsc --build instead of --project. --- src/compiler.ts | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index c44f157..3c252d2 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -76,7 +76,7 @@ export function createCompiler(dependencies: { let taskFunctions: TaskFunction[] = []; return { - runOnce: (tscArgs, disabledProjects = []) => { + runOnce(tscArgs, disabledProjects = []) { return new Promise((resolve, reject) => { glob( "**/tsconfig.json", @@ -119,35 +119,24 @@ export function createCompiler(dependencies: { ); }); }, - start: () => { - const tsConfigFiles = ["./tsconfig.json", "./src/tsconfig.json"]; // Watching all **/tsconfig.json files has proven to cost too much CPU - tsConfigFiles.forEach((tsconfigFile) => { - if (fs.existsSync(tsconfigFile)) { - let task = taskRunner.runTask( - "./node_modules/.bin/tsc", - [ - "-p", - tsconfigFile, - "--watch", - "--noEmit", - "--preserveWatchOutput", - ], - { - name: `tsc -p ${tsconfigFile} --watch`, - logger, - handleOutput, - } - ); - runningTasks.push(task); - busyCompilers++; - task.result.catch((err) => { - logger.error("compiler", err.message); - process.exit(1); - }); - } + start() { + let tsConfigFiles = ['./tsconfig.json', './src/tsconfig.json']; // Watching all **/tsconfig.json files has proven to cost too much CPU + tsConfigFiles = tsConfigFiles.filter(tsconfigFile => fs.existsSync(tsconfigFile)).map(file => file.replace('tsconfig.json', '')); + + let task = taskRunner.runTask('./node_modules/.bin/tsc', ['--build', ...tsConfigFiles, '--watch', '--preserveWatchOutput'], { + name: `tsc --build ${tsConfigFiles.join(' ')} --watch`, + logger, + handleOutput }); + runningTasks.push(task); + busyCompilers++; + task.result.catch(err => { + logger.error('compiler', err.message); + process.exit(1); + }); + }, - stop: () => { + stop() { runningTasks.forEach((task) => { task.kill(); }); From a55f635e283f60f7d31174d9f27bc0fc13f88522 Mon Sep 17 00:00:00 2001 From: Joeri van Oostveen Date: Tue, 14 Sep 2021 14:28:43 +0200 Subject: [PATCH 2/4] Now using `tsc --build` supporting project references and such. --- package-lock.json | 12 +----- package.json | 3 +- src/compiler.ts | 94 +++++++++++++++++++++++------------------------ src/taskrunner.ts | 8 +++- 4 files changed, 55 insertions(+), 62 deletions(-) diff --git a/package-lock.json b/package-lock.json index ab8a81f..54ac695 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "typescript-assistant", "version": "0.52.0", "license": "MIT", "dependencies": { @@ -13,7 +14,6 @@ "@types/prettier": "2.3.2", "@typescript-eslint/eslint-plugin": "4.30.0", "@typescript-eslint/parser": "4.30.0", - "async": "3.2.1", "chai": "4.3.4", "chokidar": "3.5.2", "eslint": "7.32.0", @@ -993,11 +993,6 @@ "node": ">=8" } }, - "node_modules/async": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/async/-/async-3.2.1.tgz", - "integrity": "sha512-XdD5lRO/87udXCMC9meWdYiR+Nq6ZjUfXidViUZGu2F1MO4T3XwZ1et0hb2++BgLfhyJwy44BGB/yx80ABx8hg==" - }, "node_modules/balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -5270,11 +5265,6 @@ "resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-2.0.0.tgz", "integrity": "sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==" }, - "async": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/async/-/async-3.2.1.tgz", - "integrity": "sha512-XdD5lRO/87udXCMC9meWdYiR+Nq6ZjUfXidViUZGu2F1MO4T3XwZ1et0hb2++BgLfhyJwy44BGB/yx80ABx8hg==" - }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", diff --git a/package.json b/package.json index 00357b9..b11e334 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "scripts": { "prepare": "npm run fix:hooks", - "prepublishOnly": "tsc -p ./src/tsconfig.json", + "prepublishOnly": "tsc -b ./src/tsconfig.json", "assist": "ts-node --transpile-only src/index.ts", "prerelease": "ts-node --transpile-only src/index ci --no-format", "release": "ts-node --transpile-only src/index.ts release", @@ -79,7 +79,6 @@ "@types/prettier": "2.3.2", "@typescript-eslint/eslint-plugin": "4.30.0", "@typescript-eslint/parser": "4.30.0", - "async": "3.2.1", "chai": "4.3.4", "chokidar": "3.5.2", "eslint": "7.32.0", diff --git a/src/compiler.ts b/src/compiler.ts index 3c252d2..049df52 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -1,6 +1,5 @@ import * as fs from "fs"; -import { parallelLimit } from "async"; import * as glob from "glob"; import { Bus } from "./bus"; @@ -14,9 +13,6 @@ export interface Compiler { runOnce(tscArgs: string[], disabledProjects?: string[]): Promise; } -type TaskFunctionCallback = () => void; -type TaskFunction = (callback: TaskFunctionCallback) => void; - let runningTasks: Task[] = []; export function createCompiler(dependencies: { @@ -29,7 +25,7 @@ export function createCompiler(dependencies: { let busyCompilers = 0; let errors: string[] = []; - let handleOutput = (line: string) => { + function handleOutput(line: string) { if (/Starting incremental compilation...$/.test(line)) { if (busyCompilers === 0) { bus.report({ tool: "compiler", status: "busy" }); @@ -71,9 +67,7 @@ export function createCompiler(dependencies: { } } return true; - }; - - let taskFunctions: TaskFunction[] = []; + } return { runOnce(tscArgs, disabledProjects = []) { @@ -85,56 +79,60 @@ export function createCompiler(dependencies: { if (error) { reject(error); } - tsConfigFiles - .filter((file) => { - return !disabledProjects.includes(file.split("/")[0]); - }) - .forEach((file) => { - let args = ["-p", file]; - let taskFunction = (callback: TaskFunctionCallback) => { - let task = taskRunner.runTask( - "./node_modules/.bin/tsc", - args, - { - name: `tsc -p ${file}`, - logger, - handleOutput, - } - ); - runningTasks.push(task); - task.result - .then(() => { - runningTasks.splice(runningTasks.indexOf(task), 1); - }) - .then(callback) - .catch(reject); - }; + let files = tsConfigFiles.filter( + (file) => !disabledProjects.includes(file.split("/")[0]) + ); - taskFunctions.push(taskFunction); + let task = taskRunner.runTask( + "./node_modules/.bin/tsc", + ["--build", ...files], + { + name: `tsc --build ${files.join(" ")}`, + logger, + handleOutput, + } + ); + runningTasks.push(task); + busyCompilers++; + task.result + .then(() => { + busyCompilers--; + runningTasks.splice(runningTasks.indexOf(task), 1); + }) + .catch((err) => { + logger.error("compiler", err.message); + process.exit(1); }); - - let limit = 2; - parallelLimit(taskFunctions, limit, resolve); } ); }); }, start() { - let tsConfigFiles = ['./tsconfig.json', './src/tsconfig.json']; // Watching all **/tsconfig.json files has proven to cost too much CPU - tsConfigFiles = tsConfigFiles.filter(tsconfigFile => fs.existsSync(tsconfigFile)).map(file => file.replace('tsconfig.json', '')); + let tsConfigFiles = ["./tsconfig.json", "./src/tsconfig.json"]; // Watching all **/tsconfig.json files has proven to cost too much CPU + tsConfigFiles = tsConfigFiles.filter((tsconfigFile) => + fs.existsSync(tsconfigFile) + ); - let task = taskRunner.runTask('./node_modules/.bin/tsc', ['--build', ...tsConfigFiles, '--watch', '--preserveWatchOutput'], { - name: `tsc --build ${tsConfigFiles.join(' ')} --watch`, - logger, - handleOutput - }); + let task = taskRunner.runTask( + "./node_modules/.bin/tsc", + ["--build", ...tsConfigFiles, "--watch", "--preserveWatchOutput"], + { + name: `tsc --build ${tsConfigFiles.join(" ")} --watch`, + logger, + handleOutput, + } + ); runningTasks.push(task); busyCompilers++; - task.result.catch(err => { - logger.error('compiler', err.message); - process.exit(1); - }); - + task.result + .then(() => { + busyCompilers--; + runningTasks.splice(runningTasks.indexOf(task), 1); + }) + .catch((err) => { + logger.error("compiler", err.message); + process.exit(1); + }); }, stop() { runningTasks.forEach((task) => { diff --git a/src/taskrunner.ts b/src/taskrunner.ts index a6924cb..f6c6958 100644 --- a/src/taskrunner.ts +++ b/src/taskrunner.ts @@ -1,4 +1,5 @@ import { spawn } from "child_process"; +import * as path from "path"; import { createInterface } from "readline"; import * as kill from "tree-kill"; @@ -32,9 +33,14 @@ export let createDefaultTaskRunner = (): TaskRunner => { let loggerCategory = config.name; let logger = config.logger; + let readableCommand = command.replace( + `.${path.sep}node_modules${path.sep}.bin${path.sep}`, + "" + ); + logger.log( loggerCategory, - `running command ${command} ${args.join(" ")}` + `running command ${readableCommand} ${args.join(" ")}` ); let task = spawn(command, args); From aa3a908b83e0854dd0d637eded4c585216fd754b Mon Sep 17 00:00:00 2001 From: Joeri van Oostveen Date: Tue, 14 Sep 2021 14:38:31 +0200 Subject: [PATCH 3/4] Doing a prerelease now tags it with the branchname, allowing for multiple different prereleases to exist. --- src/commands/release.ts | 13 +++++++++---- src/git.ts | 37 ++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/commands/release.ts b/src/commands/release.ts index 9774f56..a7558c9 100644 --- a/src/commands/release.ts +++ b/src/commands/release.ts @@ -27,10 +27,15 @@ export function createReleaseCommand(deps: Dependencies): Command { if (!answers["confirm"]) { return true; } - await taskRunner.runTask(npm, ["version", "prerelease"], { - name: "npm", - logger, - }).result; + let tag = await git.getBranchName(); + await taskRunner.runTask( + npm, + ["version", "prerelease", "--preid", tag], + { + name: "npm", + logger, + } + ).result; } else { let answers: Answers = await prompt({ type: "list", diff --git a/src/git.ts b/src/git.ts index 1c8d62a..c912c6e 100644 --- a/src/git.ts +++ b/src/git.ts @@ -15,15 +15,16 @@ export interface Git { isPristine(): Promise; execute(args: string[]): Promise; isOnBranch(): Promise; + getBranchName(): Promise; } -export let createGit = (dependencies: { +export function createGit(dependencies: { taskRunner: TaskRunner; logger: Logger; -}): Git => { - let { taskRunner, logger } = dependencies; +}): Git { + const { taskRunner, logger } = dependencies; - let findAllTypescriptFiles = (): Promise => { + function findAllTypescriptFiles(): Promise { // Not using this mechanism anymore but a blacklist to allow multiple tsconfig files. // let tsConfig = JSON.parse(readFileSync(join(process.cwd(), 'tsconfig.json'), 'UTF-8')); // let globs: string[] = tsConfig && tsConfig.include ? tsConfig.include : ['src/**/*.ts', 'test/**/*.ts']; @@ -44,10 +45,10 @@ export let createGit = (dependencies: { } }); }); - }; + } let git: Git = { - findChangedFiles: (sinceLastPush) => { + findChangedFiles(sinceLastPush) { let args = sinceLastPush ? ["diff", "--name-only", "--diff-filter=ACMR", "origin/HEAD", "HEAD"] : ["diff", "--name-only", "--diff-filter=ACMR", "HEAD"]; @@ -55,14 +56,14 @@ export let createGit = (dependencies: { return files; }); }, - isPristine: () => { + isPristine() { return git.execute(["status", "--porcelain"]).then((modifiedFiles) => { return modifiedFiles.length === 0; }); }, - findChangedFilesOrAllTypescriptFiles: async ( + async findChangedFilesOrAllTypescriptFiles( sinceLastPush - ): Promise => { + ): Promise { return git .findChangedFiles(sinceLastPush) .then((files) => @@ -70,21 +71,19 @@ export let createGit = (dependencies: { ) // no changed files found => All TS Files .catch(findAllTypescriptFiles); // If not inside a git repository }, - findAllTypescriptFiles, - - isOnBranch: async () => { + async isOnBranch() { + let currentBranchName = await git.getBranchName(); + return currentBranchName !== "master"; + }, + async getBranchName() { let [stdout] = await git.execute(["rev-parse", "--abbrev-ref", "HEAD"]); let currentBranchName = stdout.toString().trim(); // When in detached HEAD, assume it's master. - if (currentBranchName === "HEAD") { - return false; - } - return currentBranchName !== "master"; + return currentBranchName === "HEAD" ? "master" : currentBranchName; }, - - execute: (args: string[]) => { + execute(args: string[]) { let lines: string[] = []; return taskRunner .runTask("git", args, { @@ -101,4 +100,4 @@ export let createGit = (dependencies: { }, }; return git; -}; +} From ea88ef588a96497bab6533c66fcaa4aea8b59bd8 Mon Sep 17 00:00:00 2001 From: Joeri van Oostveen Date: Tue, 14 Sep 2021 14:38:55 +0200 Subject: [PATCH 4/4] 0.52.1-tsc-build.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 54ac695..b949036 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "typescript-assistant", - "version": "0.52.0", + "version": "0.52.1-tsc-build.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "typescript-assistant", - "version": "0.52.0", + "version": "0.52.1-tsc-build.0", "license": "MIT", "dependencies": { "@types/chai": "4.2.21", diff --git a/package.json b/package.json index b11e334..382d4fd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typescript-assistant", - "version": "0.52.0", + "version": "0.52.1-tsc-build.0", "description": "Combines and integrates professional Typescript tools into your project", "main": "dist/index.js", "bin": {