Skip to content

Commit 354d42b

Browse files
committed
fix(dev): suggest a route that exists, only where it can be read
1 parent 39420c9 commit 354d42b

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎packages/nuxt-cli/src/dev/shortcuts.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,16 @@ function printHelp(context: ActionContext): void {
106106
}
107107

108108
/**
109-
* Without a TTY there are no shortcuts to offer, so point at the way to talk to
110-
* the server instead: non-interactive callers (scripts, agents) otherwise have
111-
* no indication that one exists.
109+
* Without a readable stdin there are no shortcuts to offer, so point at the way
110+
* to talk to the server instead: a caller driving the CLI without a keyboard
111+
* (an agent, a wrapper script) otherwise has no indication that one exists.
112+
*
113+
* `/` is suggested rather than an API route because it is the one path every
114+
* project serves.
112115
*/
113116
functionprintRequestHint(): void{
114117
// eslint-disable-next-line no-console
115-
console.log(`\n ${styleText('dim','run')}${styleText('bold','nuxt curl /api/hello')}${styleText('dim','to send a request to this server')}\n`)
118+
console.log(`\n ${styleText('dim','run')}${styleText('bold','nuxt curl /')}${styleText('dim','to send a request to this server')}\n`)
116119
}
117120

118121
functionavailableShortcuts(context: ShortcutContext): Shortcut[]{
@@ -127,7 +130,9 @@ function availableShortcuts(context: ShortcutContext): Shortcut[] {
127130
*/
128131
exportfunctionsetupShortcuts(context: ShortcutContext): void{
129132
if(!process.stdin.isTTY||isCI||isTest){
130-
if(!isCI&&!isTest){
133+
// A hint written into a redirected log is read by nobody and answered by
134+
// nobody, so it is only offered while stdout is still a terminal.
135+
if(process.stdout.isTTY&&!isCI&&!isTest){
131136
context.onReady(()=>printRequestHint())
132137
}
133138
return

‎packages/nuxt-cli/test/unit/shortcuts.spec.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@ describe('setupShortcuts', () => {
3838
vi.clearAllMocks()
3939
})
4040

41-
functionsetup(context: Partial<ShortcutContext>={},{ isTTY =true, isRaw =false}={}){
41+
functionsetup(context: Partial<ShortcutContext>={},{ isTTY =true, isRaw =false, stdoutIsTTY =true}={}){
4242
conststdin=newPassThrough()asunknownastypeofprocess.stdin
4343
Object.assign(stdin,{ isTTY, isRaw,setRawMode: vi.fn((raw: boolean)=>Object.assign(stdin,{isRaw: raw}))})
4444

4545
constoriginal=process.stdin
4646
Object.defineProperty(process,'stdin',{value: stdin,configurable: true})
4747
restores.push(()=>Object.defineProperty(process,'stdin',{value: original,configurable: true}))
4848

49+
constoriginalStdoutIsTTY=process.stdout.isTTY
50+
Object.defineProperty(process.stdout,'isTTY',{value: stdoutIsTTY,configurable: true})
51+
restores.push(()=>Object.defineProperty(process.stdout,'isTTY',{value: originalStdoutIsTTY,configurable: true}))
52+
4953
constlog=vi.spyOn(console,'log').mockImplementation(()=>{})
5054

5155
constlistener={
@@ -89,10 +93,16 @@ describe('setupShortcuts', () => {
8993
expect(setup().stdin.listenerCount('data')).toBe(0)
9094
})
9195

92-
it('should suggest `nuxt curl` when there is no TTY',()=>{
96+
it('should suggest `nuxt curl` when stdin cannot be read',()=>{
9397
const{ log }=setup({},{isTTY: false})
9498

95-
expect(log.mock.calls.join('\n')).toContain('nuxt curl /api/hello')
99+
expect(log.mock.calls.join('\n')).toContain('nuxt curl /')
100+
})
101+
102+
it('should stay silent when the output is redirected',()=>{
103+
const{ log }=setup({},{isTTY: false,stdoutIsTTY: false})
104+
105+
expect(log.mock.calls.join('\n')).not.toContain('nuxt curl')
96106
})
97107

98108
it('should stay silent in CI',()=>{

0 commit comments

Comments
 (0)