Skip to content

Commit bea43c5

Browse files
fix: Ctrl-C cancels login process (#305)
1 parent cdd74e6 commit bea43c5

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/login.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ export class Login {
7777
process.stdin.setRawMode(false)
7878
rl.close()
7979
ux.stdout('')
80-
if (key.toLowerCase() === 'q') {
81-
ux.error('Login cancelled by user')
82-
}
83-
84-
input = 'browser'
80+
input = this.getLoginMethodFromPromptKey(key)
8581
}
8682
}
8783

@@ -365,4 +361,16 @@ machine, please manually open the URL above in your browser.\n`,
365361
ux.warn('If browser does not open, visit:')
366362
ux.stderr(ansis.greenBright(url))
367363
}
364+
365+
private getLoginMethodFromPromptKey(key: string): 'browser' {
366+
if (key === '\u0003') {
367+
ux.error('Login cancelled by user', {exit: 130})
368+
}
369+
370+
if (key.toLowerCase() === 'q') {
371+
ux.error('Login cancelled by user')
372+
}
373+
374+
return 'browser'
375+
}
368376
}

test/login.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,14 @@ describe('login with browser', () => {
143143
expect(stderrStub.calledWithExactly(ansis.greenBright(url))).to.equal(true)
144144
sinon.assert.callOrder(warnStub, stderrStub)
145145
})
146+
147+
test
148+
.it('treats ctrl-c keypress as cancel', async ctx => {
149+
const cmd = new Command([], ctx.config)
150+
const login = new Login(ctx.config, cmd.heroku)
151+
const errorStub = sinon.stub(ux, 'error').throws(new Error('cancelled'))
152+
153+
expect(() => (login as any).getLoginMethodFromPromptKey('\u0003')).to.throw('cancelled')
154+
expect(errorStub.calledWithExactly('Login cancelled by user', {exit: 130})).to.equal(true)
155+
})
146156
})

0 commit comments

Comments
 (0)