Skip to content

Commit 4cf0d21

Browse files
committed
fix(shortcuts): give the dev server the terminal unless the CLI's shortcuts take it
Vite binds its own h/r/u/o/c/q shortcuts only when its stdin is a terminal, so keeping vite serve off stdin unconditionally removed them from the default session, where the CLI's shortcuts are off and nothing else reads the keys. Hand it no stdin only when the CLI will read the terminal itself: command shortcuts on, a raw-mode capable TTY, and key shortcuts not switched off. The same conditions KeyShortcutService.attach applies now live in commandShortcutsOwnTerminal(), decided at spawn time since the dev server starts before the attach.
1 parent c2d9915 commit 4cf0d21

4 files changed

Lines changed: 150 additions & 18 deletions

File tree

‎lib/services/bundler/bundler-compiler-service.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
resolvePackagePath,
4040
resolvePackageJSONPath,
4141
} from "../../helpers/package-path-helper";
42+
import { commandShortcutsOwnTerminal } from "../key-shortcuts";
4243

4344
// todo: move out of here
4445
interface IBundlerMessage<T = any> {
@@ -739,9 +740,14 @@ export class BundlerCompilerService
739740
// Output is inherited so the dev server's URLs/logs stream to the
740741
// user. No IPC needed here — the build watcher provides the
741742
// bundle-complete IPC; the dev server is fetched over HTTP/ws.
742-
// stdin must not be the terminal: Vite reads it for shortcuts of
743-
// its own and would take the keys meant for the CLI's.
744-
stdio: ["ignore", "inherit", "inherit"],
743+
// Vite reads a terminal stdin for shortcuts of its own, which would
744+
// take the keys meant for the CLI's; otherwise its shortcuts are the
745+
// only ones the terminal has.
746+
stdio: [
747+
commandShortcutsOwnTerminal() ? "ignore" : "inherit",
748+
"inherit",
749+
"inherit",
750+
],
745751
env: {
746752
...process.env,
747753
NATIVESCRIPT_BUNDLER_ENV: JSON.stringify(envData),

‎lib/services/key-shortcuts.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RunOnDeviceEvents } from "../constants";
55
import { IChildProcess } from "../common/declarations";
66
import { Injector } from "../common/di/injector";
77
import {
8+
commandShortcutsEnabled,
89
envSwitchIsOn,
910
IKeyShortcutService,
1011
KeyContextBase,
@@ -125,6 +126,23 @@ export function keyShortcutsEnabled(): boolean {
125126
return !!process.stdin.isTTY;
126127
}
127128

129+
function isRawModeTerminal(stdin: NodeJS.ReadStream): boolean {
130+
return !!stdin.isTTY && typeof stdin.setRawMode === "function";
131+
}
132+
133+
/**
134+
* Whether a command's shortcuts, once attached, read the keys straight off
135+
* the terminal. Answerable before the attach, so a child spawned earlier can
136+
* be kept off a terminal the CLI is about to read.
137+
*/
138+
export function commandShortcutsOwnTerminal(): boolean {
139+
return (
140+
commandShortcutsEnabled() &&
141+
isRawModeTerminal(process.stdin) &&
142+
keyShortcutsEnabled()
143+
);
144+
}
145+
128146
/**
129147
* The names `devicePlatformsConstants` hands out, read off the constants
130148
* rather than spelled again here.
@@ -420,7 +438,7 @@ export class KeyShortcutService implements IKeyShortcutService {
420438
);
421439

422440
const stdin = process.stdin;
423-
if (!stdin.isTTY || typeof stdin.setRawMode !== "function") {
441+
if (!isRawModeTerminal(stdin)) {
424442
if (typeof process.send !== "function") {
425443
this.releaseShortcuts();
426444
return false;

‎test/services/bundler/bundler-compiler-service.ts‎

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,41 @@ describe("BundlerCompilerService", () => {
374374
});
375375

376376
describe("startViteDevServer", () => {
377-
it("does not hand the dev server the terminal's stdin", async () => {
378-
const spawned: { [key: string]: any }[] = [];
377+
const env = ["NS_COMMAND_SHORTCUTS", "NS_KEY_SHORTCUTS"];
378+
let savedEnv: { [key: string]: string };
379+
let originalStdin: PropertyDescriptor;
380+
let spawned: { [key: string]: any }[];
381+
382+
const useStdin = (isTTY: boolean): void => {
383+
Object.defineProperty(process, "stdin", {
384+
value: { isTTY, setRawMode: (): void => undefined },
385+
configurable: true,
386+
});
387+
};
388+
389+
const devServerStdin = async (): Promise<string> => {
390+
await (<any>bundlerCompilerService).startViteDevServer(
391+
{ platformNameLowerCase: "ios" },
392+
{
393+
projectDir: "/project",
394+
bundlerConfigPath: "/project/vite.config.ts",
395+
},
396+
{ watch: true, hmr: true },
397+
);
398+
assert.lengthOf(spawned, 1);
399+
400+
return spawned[0].stdio[0];
401+
};
402+
403+
beforeEach(() => {
404+
savedEnv = {};
405+
for (const name of env) {
406+
savedEnv[name] = process.env[name];
407+
delete process.env[name];
408+
}
409+
originalStdin = Object.getOwnPropertyDescriptor(process, "stdin");
410+
411+
spawned = [];
379412
testInjector.resolve("childProcess").spawn = (
380413
_command: string,
381414
_args: string[],
@@ -390,20 +423,39 @@ describe("BundlerCompilerService", () => {
390423
service.buildEnvCommandLineParams = async (): Promise<string[]> => [];
391424
service.getBundlerExecutablePath = () => "/project/vite.js";
392425
service.waitForPort = async () => true;
426+
});
393427

394-
await service.startViteDevServer(
395-
{ platformNameLowerCase: "ios" },
396-
{
397-
projectDir: "/project",
398-
bundlerConfigPath: "/project/vite.config.ts",
399-
},
400-
{ watch: true, hmr: true },
401-
);
428+
afterEach(() => {
429+
Object.defineProperty(process, "stdin", originalStdin);
430+
for (const name of env) {
431+
if (savedEnv[name] === undefined) {
432+
delete process.env[name];
433+
} else {
434+
process.env[name] = savedEnv[name];
435+
}
436+
}
437+
});
402438

403-
assert.lengthOf(spawned, 1);
404-
const { stdio } = spawned[0];
405-
assert.isArray(stdio);
406-
assert.equal(stdio[0], "ignore");
439+
it("keeps the dev server off the terminal the CLI's shortcuts read", async () => {
440+
process.env.NS_COMMAND_SHORTCUTS = "1";
441+
process.env.NS_KEY_SHORTCUTS = "true";
442+
useStdin(true);
443+
444+
assert.equal(await devServerStdin(), "ignore");
445+
});
446+
447+
it("hands the dev server the terminal while the CLI's shortcuts are off", async () => {
448+
useStdin(true);
449+
450+
assert.equal(await devServerStdin(), "inherit");
451+
});
452+
453+
it("hands the dev server its stdin when the CLI has no terminal to read", async () => {
454+
process.env.NS_COMMAND_SHORTCUTS = "1";
455+
process.env.NS_KEY_SHORTCUTS = "true";
456+
useStdin(false);
457+
458+
assert.equal(await devServerStdin(), "inherit");
407459
});
408460
});
409461

‎test/services/key-shortcuts.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getContractName } from "../../lib/common/di/contract";
55
import { Injector } from "../../lib/common/di/injector";
66
import { KeyShortcutRegistryService } from "../../lib/services/key-shortcut-registry";
77
import {
8+
commandShortcutsOwnTerminal,
89
findShortcut,
910
KeyContextBase,
1011
KeyShortcut,
@@ -473,6 +474,61 @@ describe("key shortcuts", () => {
473474
});
474475
});
475476

477+
describe("commandShortcutsOwnTerminal", () => {
478+
const env = [
479+
"NS_COMMAND_SHORTCUTS",
480+
"NS_KEY_SHORTCUTS",
481+
"CI",
482+
"JENKINS_HOME",
483+
];
484+
let saved: { [key: string]: string };
485+
let stdin: FakeStdin;
486+
let restoreStdin: () => void;
487+
488+
beforeEach(() => {
489+
saved = {};
490+
for (const name of env) {
491+
saved[name] = process.env[name];
492+
delete process.env[name];
493+
}
494+
process.env.NS_COMMAND_SHORTCUTS = "1";
495+
stdin = new FakeStdin();
496+
restoreStdin = swapStdin(stdin);
497+
});
498+
499+
afterEach(() => {
500+
restoreStdin();
501+
for (const name of env) {
502+
if (saved[name] === undefined) {
503+
delete process.env[name];
504+
} else {
505+
process.env[name] = saved[name];
506+
}
507+
}
508+
});
509+
510+
it("holds for a command's shortcuts on a terminal", () => {
511+
assert.isTrue(commandShortcutsOwnTerminal());
512+
});
513+
514+
it("does not hold while command shortcuts are off", () => {
515+
delete process.env.NS_COMMAND_SHORTCUTS;
516+
assert.isFalse(commandShortcutsOwnTerminal());
517+
});
518+
519+
it("does not hold when key shortcuts are switched off", () => {
520+
process.env.NS_KEY_SHORTCUTS = "false";
521+
assert.isFalse(commandShortcutsOwnTerminal());
522+
});
523+
524+
it("does not hold for a stdin that is not a terminal, even when forced on", () => {
525+
// A `ns run` spawned by `ns start`: its keys come over IPC.
526+
process.env.NS_KEY_SHORTCUTS = "true";
527+
stdin.isTTY = false;
528+
assert.isFalse(commandShortcutsOwnTerminal());
529+
});
530+
});
531+
476532
describe("KeyShortcutService", () => {
477533
let stdin: FakeStdin;
478534
let restoreStdin: () => void;

0 commit comments

Comments
 (0)