Skip to content

Commit

Permalink
feat: add KillSignal.prototype.abortedExitCode (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 26, 2024
1 parent 1bcd684 commit f222694
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ function validateCommandName(command: string) {
const SHELL_SIGNAL_CTOR_SYMBOL = Symbol();

interface KillSignalState {
abortedCode: number;
abortedCode: number | undefined;
listeners: ((signal: Deno.Signal) => void)[];
}

Expand All @@ -1086,7 +1086,7 @@ export class KillSignalController {

constructor() {
this.#state = {
abortedCode: 0,
abortedCode: undefined,
listeners: [],
};
this.#killSignal = new KillSignal(SHELL_SIGNAL_CTOR_SYMBOL, this.#state);
Expand Down Expand Up @@ -1131,7 +1131,12 @@ export class KillSignal {
* SIGKILL, SIGABRT, SIGQUIT, SIGINT, or SIGSTOP
*/
get aborted(): boolean {
return this.#state.abortedCode !== 0;
return this.#state.abortedCode !== undefined;
}

/** Gets the exit code to use if aborted. */
get abortedExitCode(): number | undefined {
return this.#state.abortedCode;
}

/**
Expand Down Expand Up @@ -1160,11 +1165,6 @@ export class KillSignal {
this.#state.listeners.splice(index, 1);
}
}

/** @internal - DO NOT USE. Very unstable. Not sure about this. */
get _abortedExitCode() {
return this.#state.abortedCode;
}
}

function sendSignalToState(state: KillSignalState, signal: Deno.Signal) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function executeCat(context: CommandContext) {
if (!size || size === 0) break;
else context.stdout.writeSync(buf.slice(0, size));
}
exitCode = context.signal._abortedExitCode;
exitCode = context.signal.abortedExitCode ?? 0;
} else {
const _assertValue: "null" | "inherit" = context.stdin;
throw new Error(`not supported. stdin was '${context.stdin}'`);
Expand All @@ -47,7 +47,7 @@ async function executeCat(context: CommandContext) {
if (!size || size === 0) break;
else context.stdout.writeSync(buf.slice(0, size));
}
exitCode = context.signal._abortedExitCode;
exitCode = context.signal.abortedExitCode ?? 0;
} catch (err) {
context.stderr.writeLine(`cat ${path}: ${err}`);
exitCode = 1;
Expand Down

0 comments on commit f222694

Please sign in to comment.