Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-assistant",
"version": "0.52.1",
"version": "0.52.2-tsc-build-attempt-2.3",
"description": "Combines and integrates professional Typescript tools into your project",
"main": "dist/index.js",
"bin": {
Expand Down Expand Up @@ -79,6 +79,7 @@
"@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",
Expand Down
15 changes: 12 additions & 3 deletions src/commands/assist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export interface AssistOptions {
statusServerPort?: number;
format?: boolean;
coverage?: boolean;
projects?: string[];
testConfig?: string;
testsGlob?: string;
}

export function createAssistCommand(
Expand All @@ -14,7 +17,13 @@ export function createAssistCommand(

return {
execute(options: AssistOptions = {}) {
const { format = true, coverage = true } = options;
const {
format = true,
coverage = true,
projects,
testConfig,
testsGlob,
} = options;

watcher.watchSourceFileChanged();
if (options.statusServerPort) {
Expand All @@ -26,8 +35,8 @@ export function createAssistCommand(
} else {
linter.start("source-files-changed", true);
}
nyc.start(["source-files-changed"], coverage);
compiler.start();
nyc.start(["source-files-changed"], coverage, testConfig, testsGlob);
compiler.start(projects);

return Promise.resolve(true);
},
Expand Down
10 changes: 7 additions & 3 deletions src/commands/pre-push.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Dependencies } from "../dependencies";
import { AssistOptions } from "./assist";
import { Command } from "./command";

export interface PrePushCommandOptions {
export interface PrePushCommandOptions
extends Pick<AssistOptions, "testConfig" | "testsGlob"> {
disabledProjects?: string[];
}

Expand All @@ -12,6 +14,8 @@ export function createPrePushCommand(

return {
async execute(options: PrePushCommandOptions = {}): Promise<boolean> {
const { disabledProjects, testConfig, testsGlob } = options;

let timestamp = new Date().getTime();
let pristine = await git.isPristine();
if (!pristine) {
Expand All @@ -27,8 +31,8 @@ export function createPrePushCommand(
return false;
}
let results = await Promise.all([
compiler.runOnce([], options.disabledProjects),
nyc.run(),
compiler.runOnce([], disabledProjects),
nyc.run(true, testConfig, testsGlob),
]);
let toolErrors = results.filter((result) => result === false).length;
logger.log(
Expand Down
88 changes: 52 additions & 36 deletions src/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "fs";

import { parallelLimit } from "async";
import * as glob from "glob";

import { Bus } from "./bus";
Expand All @@ -8,11 +9,14 @@ import { Task, TaskRunner } from "./taskrunner";
import { absolutePath } from "./util";

export interface Compiler {
start(): void;
start(configs?: string[]): void;
stop(): void;
runOnce(tscArgs: string[], disabledProjects?: string[]): Promise<boolean>;
}

type TaskFunctionCallback = () => void;
type TaskFunction = (callback: TaskFunctionCallback) => void;

let runningTasks: Task[] = [];

export function createCompiler(dependencies: {
Expand Down Expand Up @@ -69,6 +73,8 @@ export function createCompiler(dependencies: {
return true;
}

let taskFunctions: TaskFunction[] = [];

return {
runOnce(tscArgs, disabledProjects = []) {
return new Promise((resolve, reject) => {
Expand All @@ -79,43 +85,58 @@ export function createCompiler(dependencies: {
if (error) {
reject(error);
}
let files = tsConfigFiles.filter(
(file) => !disabledProjects.includes(file.split("/")[0])
);

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);
tsConfigFiles
.filter((file) => {
return !disabledProjects.includes(file.split("/")[0]);
})
.catch((err) => {
logger.error("compiler", err.message);
process.exit(1);
.forEach((file) => {
let args = ["--build", file];
let taskFunction = (callback: TaskFunctionCallback) => {
let task = taskRunner.runTask(
"./node_modules/.bin/tsc",
args,
{
name: `tsc --build ${file}`,
logger,
handleOutput,
}
);
runningTasks.push(task);
task.result
.then(() => {
runningTasks.splice(runningTasks.indexOf(task), 1);
})
.then(callback)
.catch(reject);
};

taskFunctions.push(taskFunction);
});

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)
/**
* Watching all tsconfig.json files has proven to cost too much CPU.
*/
start(tsConfigFiles = ["./tsconfig.json", "./src/tsconfig.json"]) {
tsConfigFiles = tsConfigFiles.map((config) =>
config.replace(/\\\\/g, "/")
);

tsConfigFiles.forEach((tsconfigFile) => {
if (!fs.existsSync(tsconfigFile)) {
throw new Error(`File does not exist: ${tsconfigFile}`);
}
});

let task = taskRunner.runTask(
"./node_modules/.bin/tsc",
["--build", ...tsConfigFiles, "--watch", "--preserveWatchOutput"],
["-b", ...tsConfigFiles, "--watch", "--preserveWatchOutput"],
{
name: `tsc --build ${tsConfigFiles.join(" ")} --watch`,
logger,
Expand All @@ -124,15 +145,10 @@ export function createCompiler(dependencies: {
);
runningTasks.push(task);
busyCompilers++;
task.result
.then(() => {
busyCompilers--;
runningTasks.splice(runningTasks.indexOf(task), 1);
})
.catch((err) => {
logger.error("compiler", err.message);
process.exit(1);
});
task.result.catch((err) => {
logger.error("compiler", err.message);
process.exit(1);
});
},
stop() {
runningTasks.forEach((task) => {
Expand Down
35 changes: 33 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ yargsModule.command(
boolean: true,
default: true,
},
project: {
describe: "provide tsconfigs to watch",
string: true,
type: "array",
},
testConfig: {
describe: "path to nyc config file",
string: true,
},
testsGlob: {
describe: "glob for test files",
string: true,
},
},
(yargs) => {
if (
Expand All @@ -68,6 +81,9 @@ yargsModule.command(
statusServerPort: parseInt(yargs.port as string, 10) || 0,
format: yargs.format,
coverage: yargs.coverage,
projects: yargs.project,
testConfig: yargs.testConfig,
testsGlob: yargs.testsGlob,
});
} else {
console.error("Unknown command");
Expand Down Expand Up @@ -212,11 +228,26 @@ yargsModule.command(
yargsModule.command(
"pre-push",
"Pre-push git hook for husky",
(yargs) => yargs.array("disable"),
{
disable: {
string: true,
type: "array",
},
testConfig: {
describe: "path to nyc config file",
string: true,
},
testsGlob: {
describe: "glob for test files",
string: true,
},
},
(yargs) => {
inject(createPrePushCommand)
.execute({
disabledProjects: yargs.disable as string[],
disabledProjects: yargs.disable,
testConfig: yargs.testConfig,
testsGlob: yargs.testsGlob,
})
.then(failIfUnsuccessful, onFailure);
}
Expand Down
7 changes: 3 additions & 4 deletions src/taskrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ 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}`,
""
);
let readableCommand = command
.replace(`.${path.sep}node_modules${path.sep}.bin${path.sep}`, "")
.replace(".cmd", "");

logger.log(
loggerCategory,
Expand Down
Loading