Skip to content

Commit 4456a5a

Browse files
authored
Merge pull request #2462 from alphagov/use-inquirer-prompts
Migrate to use `@inquirer/confirm`
2 parents 7debdfa + 9d0fb73 commit 4456a5a

File tree

6 files changed

+104
-477
lines changed

6 files changed

+104
-477
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- [#2462: Update from inquirer 8.2.6 to @inquirer/confirm 5.1.15 to address a vulnerability in tmp](https://github.com/alphagov/govuk-prototype-kit/pull/2462)
8+
59
## 13.18.0
610

711
### New features

lib/usage-data.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const os = require('os')
44
const path = require('path')
55

66
// npm dependencies
7-
const inquirer = require('inquirer')
7+
const { default: confirm } = require('@inquirer/confirm')
88
const universalAnalytics = require('universal-analytics')
99
const { v4: uuidv4 } = require('uuid')
1010
const ansiColors = require('ansi-colors')
@@ -50,14 +50,15 @@ function setUsageDataConfig (usageDataConfig) {
5050

5151
// Ask for permission to track data
5252
// returns a Promise with the user's answer
53-
function askForUsageDataPermission () {
54-
return inquirer.prompt([{
55-
name: 'usageData',
56-
message: USAGE_DATA_PROMPT,
57-
type: 'confirm',
58-
when: () => process.stdout.isTTY,
59-
default: false
60-
}]).then(answers => answers.usageData)
53+
async function askForUsageDataPermission () {
54+
if (process.stdout.isTTY) {
55+
return confirm({
56+
message: USAGE_DATA_PROMPT,
57+
default: false
58+
})
59+
}
60+
61+
return false
6162
}
6263

6364
function startTracking (usageDataConfig) {

lib/usage-data.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const { askForUsageDataPermission } = require('./usage-data')
2+
3+
jest.mock('@inquirer/confirm')
4+
5+
const { default: confirm } = require('@inquirer/confirm')
6+
7+
describe('askForUsageDataPermission', () => {
8+
afterEach(() => {
9+
jest.resetAllMocks()
10+
})
11+
12+
describe('when in an interactive shell', () => {
13+
// We can't use jest.replaceProperty here because in a non-TTY environment
14+
// the isTTY property doesn't exist on process.stdout, and Jest will only
15+
// replace existing properties.
16+
let originalTTY
17+
18+
beforeAll(() => {
19+
originalTTY = process.stdout.isTTY
20+
process.stdout.isTTY = true
21+
})
22+
23+
afterAll(() => {
24+
process.stdout.isTTY = originalTTY
25+
})
26+
27+
it('asks the user for confirmation', () => {
28+
askForUsageDataPermission()
29+
30+
expect(confirm).toHaveBeenCalledTimes(1)
31+
expect(confirm).toHaveBeenCalledWith({
32+
default: false,
33+
message: expect.stringContaining(
34+
'Do you give permission for the kit to send anonymous usage data?'
35+
)
36+
})
37+
})
38+
39+
it('returns true if the user accepts tracking', async () => {
40+
confirm.mockResolvedValue(true)
41+
42+
expect(await askForUsageDataPermission()).toBe(true)
43+
})
44+
45+
it('returns false if the user does not accept tracking', async () => {
46+
confirm.mockResolvedValue(false)
47+
48+
expect(await askForUsageDataPermission()).toBe(false)
49+
})
50+
})
51+
52+
describe('when in a non-interactive shell', () => {
53+
// We can't use jest.replaceProperty here because in a non-TTY environment
54+
// the isTTY property doesn't exist on process.stdout, and Jest will only
55+
// replace existing properties.
56+
let originalTTY
57+
58+
beforeAll(() => {
59+
originalTTY = process.stdout.isTTY
60+
process.stdout.isTTY = undefined
61+
})
62+
63+
afterAll(() => {
64+
process.stdout.isTTY = originalTTY
65+
})
66+
67+
it('does not ask the user for confirmation', () => {
68+
askForUsageDataPermission()
69+
70+
expect(confirm).not.toHaveBeenCalled()
71+
})
72+
73+
it('returns false', async () => {
74+
expect(await askForUsageDataPermission()).toBe(false)
75+
})
76+
})
77+
})

lib/utils/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const semver = require('semver')
77
const { existsSync } = require('fs')
88

99
// npm dependencies
10-
const inquirer = require('inquirer')
10+
const { default: confirm } = require('@inquirer/confirm')
1111
const portScanner = require('portscanner')
1212
const { marked } = require('marked')
1313

@@ -108,12 +108,10 @@ function findAvailablePort (callback) {
108108
console.error('ERROR: Port ' + port + ' in use - you may have another prototype running.\n')
109109

110110
// Ask user if they want to change port
111-
inquirer.prompt([{
112-
name: 'changePort',
113-
message: 'Change to an available port?',
114-
type: 'confirm'
115-
}]).then(answers => {
116-
if (answers.changePort) {
111+
confirm({
112+
message: 'Change to an available port?'
113+
}).then(changePort => {
114+
if (changePort) {
117115
// User answers yes
118116
port = availablePort
119117

0 commit comments

Comments
 (0)