Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tty-qrcode-shortcuts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/qrcode-rsbuild-plugin": patch
---

Only register console shortcuts when running in TTY environments
4 changes: 3 additions & 1 deletion packages/rspeedy/plugin-qrcode/src/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion packages/rspeedy/plugin-qrcode/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
22 changes: 19 additions & 3 deletions packages/rspeedy/plugin-qrcode/test/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -228,9 +247,6 @@ describe('Preview', () => {
pluginStubRspeedyAPI(),
pluginQRCode(),
],
environments: {
lynx: {},
},
source: {
entry: {},
},
Expand Down
24 changes: 23 additions & 1 deletion packages/rspeedy/plugin-qrcode/test/shortcuts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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()
Expand Down
Loading