diff --git a/.changeset/tty-qrcode-shortcuts.md b/.changeset/tty-qrcode-shortcuts.md new file mode 100644 index 0000000000..aa56836794 --- /dev/null +++ b/.changeset/tty-qrcode-shortcuts.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/qrcode-rsbuild-plugin": patch +--- + +Only register console shortcuts when running in TTY environments diff --git a/packages/rspeedy/plugin-qrcode/src/shortcuts.ts b/packages/rspeedy/plugin-qrcode/src/shortcuts.ts index 34e708f4a9..e71295efa8 100644 --- a/packages/rspeedy/plugin-qrcode/src/shortcuts.ts +++ b/packages/rspeedy/plugin-qrcode/src/shortcuts.ts @@ -47,7 +47,9 @@ export async function registerConsoleShortcuts( gExistingShortcuts.add(options) // We should not `await` on this since it would block the NodeJS main thread. - void loop(options, value, devUrls) + if (process.stdin.isTTY && process.stdout.isTTY) { + void loop(options, value, devUrls) + } function off() { gExistingShortcuts.delete(options) diff --git a/packages/rspeedy/plugin-qrcode/test/index.test.ts b/packages/rspeedy/plugin-qrcode/test/index.test.ts index 951ccf8b6a..677302c6af 100644 --- a/packages/rspeedy/plugin-qrcode/test/index.test.ts +++ b/packages/rspeedy/plugin-qrcode/test/index.test.ts @@ -37,8 +37,26 @@ describe('Plugins - Terminal', () => { vi.stubEnv('NODE_ENV', 'production') vi.restoreAllMocks() vi.mocked(isCancel).mockReturnValue(true) + Object.defineProperty(process.stdin, 'isTTY', { + value: true, + configurable: true, + }) + Object.defineProperty(process.stdout, 'isTTY', { + value: true, + configurable: true, + }) - return () => vi.unstubAllEnvs() + return () => { + vi.unstubAllEnvs() + Object.defineProperty(process.stdin, 'isTTY', { + value: undefined, + configurable: true, + }) + Object.defineProperty(process.stdout, 'isTTY', { + value: undefined, + configurable: true, + }) + } }) describe('schema', () => { diff --git a/packages/rspeedy/plugin-qrcode/test/preview.test.ts b/packages/rspeedy/plugin-qrcode/test/preview.test.ts index d4b8638c88..7fc56b84af 100644 --- a/packages/rspeedy/plugin-qrcode/test/preview.test.ts +++ b/packages/rspeedy/plugin-qrcode/test/preview.test.ts @@ -30,6 +30,25 @@ const pluginStubRspeedyAPI = (config: Config = {}): RsbuildPlugin => ({ describe('Preview', () => { beforeEach(() => { vi.restoreAllMocks() + Object.defineProperty(process.stdin, 'isTTY', { + value: true, + configurable: true, + }) + Object.defineProperty(process.stdout, 'isTTY', { + value: true, + configurable: true, + }) + + return () => { + Object.defineProperty(process.stdin, 'isTTY', { + value: undefined, + configurable: true, + }) + Object.defineProperty(process.stdout, 'isTTY', { + value: undefined, + configurable: true, + }) + } }) test('preview with NODE_ENV=development', async () => { @@ -228,9 +247,6 @@ describe('Preview', () => { pluginStubRspeedyAPI(), pluginQRCode(), ], - environments: { - lynx: {}, - }, source: { entry: {}, }, diff --git a/packages/rspeedy/plugin-qrcode/test/shortcuts.test.ts b/packages/rspeedy/plugin-qrcode/test/shortcuts.test.ts index 567d2df274..a29938a043 100644 --- a/packages/rspeedy/plugin-qrcode/test/shortcuts.test.ts +++ b/packages/rspeedy/plugin-qrcode/test/shortcuts.test.ts @@ -2,7 +2,7 @@ // Licensed under the Apache License Version 2.0 that can be found in the // LICENSE file in the root directory of this source tree. import type { RsbuildPluginAPI } from '@rsbuild/core' -import { describe, expect, test, vi } from 'vitest' +import { beforeEach, describe, expect, test, vi } from 'vitest' import { registerConsoleShortcuts } from '../src/shortcuts.js' @@ -18,6 +18,28 @@ describe('PluginQRCode - CLI Shortcuts', () => { }), } as unknown as RsbuildPluginAPI + beforeEach(() => { + Object.defineProperty(process.stdin, 'isTTY', { + value: true, + configurable: true, + }) + Object.defineProperty(process.stdout, 'isTTY', { + value: true, + configurable: true, + }) + + return () => { + Object.defineProperty(process.stdin, 'isTTY', { + value: undefined, + configurable: true, + }) + Object.defineProperty(process.stdout, 'isTTY', { + value: undefined, + configurable: true, + }) + } + }) + test('open page', async () => { vi.stubEnv('NODE_ENV', 'development') const onPrint = vi.fn()