Skip to content

Commit

Permalink
use isInteractive() instead of !isNonIteractiveOrCI()
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Jul 2, 2024
1 parent 9b7e52f commit 4b4d56a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
7 changes: 6 additions & 1 deletion packages/wrangler/src/cli-hotkeys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import readline from "readline";
import { Log } from "miniflare";
import { unwrapHook } from "./api/startDevWorker/utils";
import isInteractive from "./is-interactive";
import { Logger, logger } from "./logger";
import { onKeyPress } from "./utils/onKeyPress";
import type { Hook } from "./api";
Expand All @@ -13,6 +14,10 @@ export default function (
handler: () => void | Promise<void>;
}>
) {
if (!isInteractive()) {
return;
}

/**
* Formats all options, comma-separated, prefixed by the first key in square brackets.
*
Expand Down Expand Up @@ -87,7 +92,7 @@ export default function (
printInstructions();

return () => {
unregisterKeyPress();
unregisterKeyPress?.();
clearPreviousInstructions();
Logger.registerBeforeLogHook(undefined);
Logger.registerAfterLogHook(undefined);
Expand Down
5 changes: 2 additions & 3 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { maybeRegisterLocalWorker } from "./dev/local";
import { startDevServer } from "./dev/start-server";
import { UserError } from "./errors";
import { run } from "./experimental-flags";
import { isNonInteractiveOrCI } from "./is-interactive";
import { logger } from "./logger";
import * as metrics from "./metrics";
import { getAssetPaths, getSiteAssetPaths } from "./sites";
Expand Down Expand Up @@ -584,8 +583,8 @@ export async function startDev(args: StartDevOptions) {
});
}

let unregisterHotKeys: () => void;
if (!isNonInteractiveOrCI() && args.showInteractiveDevSession !== false) {
let unregisterHotKeys: (() => void) | undefined = undefined;
if (args.showInteractiveDevSession !== false) {
unregisterHotKeys = registerDevHotKeys(devEnv, args);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/dev/hotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function registerDevHotKeys(
keys: ["x", "q", "ctrl+c"],
label: "to exit",
handler: async () => {
unregisterHotKeys();
unregisterHotKeys?.();
await devEnv.teardown();
},
},
Expand Down
16 changes: 8 additions & 8 deletions packages/wrangler/src/utils/onKeyPress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import readline from "readline";
import { PassThrough } from "stream";
import { isNonInteractiveOrCI } from "../is-interactive";
import isInteractive from "../is-interactive";

export type KeypressEvent = {
name: string;
Expand All @@ -11,6 +11,10 @@ export type KeypressEvent = {
};

export function onKeyPress(callback: (key: KeypressEvent) => void) {
if (!isInteractive()) {
return;
}

// Listening for events on process.stdin (eg .on('keypress')) causes it to go into 'old mode'
// which keeps this nodejs process alive even after calling .off('keypress')
// WORKAROUND: piping stdin via a transform stream allows us to call stream.destroy()
Expand All @@ -19,10 +23,8 @@ export function onKeyPress(callback: (key: KeypressEvent) => void) {
const stream = new PassThrough();
process.stdin.pipe(stream);

if (!isNonInteractiveOrCI()) {
readline.emitKeypressEvents(stream);
process.stdin.setRawMode(true);
}
readline.emitKeypressEvents(stream);
process.stdin.setRawMode(true);

const handler = async (_char: string, key: KeypressEvent) => {
if (key) {
Expand All @@ -33,9 +35,7 @@ export function onKeyPress(callback: (key: KeypressEvent) => void) {
stream.on("keypress", handler);

return () => {
if (!isNonInteractiveOrCI()) {
process.stdin.setRawMode(false);
}
process.stdin.setRawMode(false);
stream.off("keypress", handler);
stream.destroy();
};
Expand Down

0 comments on commit 4b4d56a

Please sign in to comment.