Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify cli options and API in README #445

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
575 changes: 345 additions & 230 deletions README.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -24,6 +24,7 @@
"scripts": {
"build": "tsc --build",
"postbuild": "chmod +x dist/bin/concurrently.js",
"generate:doc:api": "ts-readme --header-depth 3 src/concurrently.ts ",
"clean": "tsc --build --clean",
"format": "prettier --check '**/*.{json,y?(a)ml,md}'",
"format:fix": "pnpm run format --write",
Expand Down Expand Up @@ -88,6 +89,7 @@
"prettier": "^3.0.3",
"safe-publish-latest": "^2.0.0",
"string-argv": "^0.3.2",
"ts-readme": "^1.1.3",
"typescript": "~5.2.2"
},
"files": [
Expand Down
133 changes: 133 additions & 0 deletions pnpm-lock.yaml

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

64 changes: 16 additions & 48 deletions src/command.ts
Expand Up @@ -2,59 +2,39 @@ import { ChildProcess as BaseChildProcess, SpawnOptions } from 'child_process';
import * as Rx from 'rxjs';
import { EventEmitter, Writable } from 'stream';

/**
* Identifier for a command; if string, it's the command's name, if number, it's the index.
*/
/** Identifier for a command; if string, it's the command's name, if number, it's the index.*/
export type CommandIdentifier = string | number;

export interface CommandInfo {
/**
* Command's name.
*/
/** Command's name. */
name: string;

/**
* Which command line the command has.
*/
/** Which command line the command has. */
command: string;

/**
* Which environment variables should the spawned process have.
*/
/** Which environment variables should the spawned process have.*/
env?: Record<string, unknown>;

/**
* The current working directory of the process when spawned.
*/
/** The current working directory of the process when spawned.*/
cwd?: string;

/**
* Color to use on prefix of the command.
*/
/** Color to use on prefix of the command.*/
prefixColor?: string;

/**
* Output command in raw format.
*/
/** Output command in raw format.*/
raw?: boolean;
}

export interface CloseEvent {
command: CommandInfo;

/**
* The command's index among all commands ran.
*/
/** The command's index among all commands ran.*/
index: number;

/**
* Whether the command exited because it was killed.
*/
/** Whether the command exited because it was killed.*/
killed: boolean;

/**
* The exit code or signal for the command.
*/
/** The exit code or signal for the command.*/
exitCode: string | number;
timings: {
startDate: Date;
Expand All @@ -68,20 +48,14 @@ export interface TimerEvent {
endDate?: Date;
}

/**
* Subtype of NodeJS's child_process including only what's actually needed for a command to work.
*/
/** Subtype of NodeJS's child_process including only what's actually needed for a command to work. */
export type ChildProcess = EventEmitter &
Pick<BaseChildProcess, 'pid' | 'stdin' | 'stdout' | 'stderr'>;

/**
* Interface for a function that must kill the process with `pid`, optionally sending `signal` to it.
*/
/** Interface for a function that must kill the process with `pid`, optionally sending `signal` to it. */
export type KillProcess = (pid: number, signal?: string) => void;

/**
* Interface for a function that spawns a command and returns its child process instance.
*/
/** Interface for a function that spawns a command and returns its child process instance. */
export type SpawnCommand = (command: string, options: SpawnOptions) => ChildProcess;

export class Command implements CommandInfo {
Expand Down Expand Up @@ -139,9 +113,7 @@ export class Command implements CommandInfo {
this.spawnOpts = spawnOpts;
}

/**
* Starts this command, piping output, error and close events onto the corresponding observables.
*/
/** Starts this command, piping output, error and close events onto the corresponding observables. */
start() {
const child = this.spawn(this.command, this.spawnOpts);
this.process = child;
Expand Down Expand Up @@ -190,9 +162,7 @@ export class Command implements CommandInfo {
this.stdin = child.stdin || undefined;
}

/**
* Kills this command, optionally specifying a signal to send to it.
*/
/** Kills this command, optionally specifying a signal to send to it. */
kill(code?: string) {
if (Command.canKill(this)) {
this.killed = true;
Expand All @@ -210,9 +180,7 @@ export class Command implements CommandInfo {
}
}

/**
* Pipes all events emitted by `stream` into `subject`.
*/
/** Pipes all events emitted by `stream` into `subject`.s */
function pipeTo<T>(stream: Rx.Observable<T>, subject: Rx.Subject<T>) {
stream.subscribe((event) => subject.next(event));
}