Skip to content

Commit

Permalink
Update askForUsageDataPermission to use inquirer
Browse files Browse the repository at this point in the history
Prompt's dependency winston is causing a warning in the console:

> (node:15613) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency

This has been raised as an issue on prompt's GitHub repo [1] but given the last release of prompt was 4 years ago, I think it makes sense to move away to an alternative that's actively maintained.

[1]: flatiron/prompt#199
  • Loading branch information
36degrees committed May 26, 2020
1 parent 1426740 commit f69d62b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/usage-data-prompt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ With your permission, the kit can send useful anonymous usage data
for analysis to help the team improve the service. Read more here:
https://govuk-prototype-kit.herokuapp.com/docs/usage-data

Do you give permission for the kit to send anonymous usage data? (y/n)
Do you give permission for the kit to send anonymous usage data?
37 changes: 9 additions & 28 deletions lib/usage_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs')
const os = require('os')

// NPM dependencies
const prompt = require('prompt')
const inquirer = require('inquirer')
const universalAnalytics = require('universal-analytics')
const uuidv4 = require('uuid/v4')

Expand Down Expand Up @@ -37,34 +37,15 @@ exports.setUsageDataConfig = function (usageDataConfig) {
// returns a Promise with the user's answer
exports.askForUsageDataPermission = function () {
return new Promise(function (resolve, reject) {
// Set up prompt settings
prompt.colors = false
prompt.start()
prompt.message = ''
prompt.delimiter = ''
const description = fs.readFileSync(path.join(__dirname, 'usage-data-prompt.txt'), 'utf8').trim()

const description = fs.readFileSync(path.join(__dirname, 'usage-data-prompt.txt'), 'utf8')

prompt.get([{
name: 'answer',
description: description,
required: true,
type: 'string',
pattern: /y(es)?|no?/i,
message: 'Please enter y or n',
ask: function () {
return process.stdout.isTTY
}
}], function (err, result) {
if (err) {
reject(err)
}
if (result.answer.match(/y(es)?/i)) {
resolve('yes')
} else {
resolve('no')
}
})
inquirer.prompt([{
name: 'usageData',
message: description,
type: 'confirm',
when: () => process.stdout.isTTY,
default: false
}]).then(answers => resolve(answers.usageData))
})
}

Expand Down
15 changes: 6 additions & 9 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ const usageDataConfig = usageData.getUsageDataConfig()
if (usageDataConfig.collectUsageData === undefined) {
// No recorded answer, so ask for permission
const promptPromise = usageData.askForUsageDataPermission()
promptPromise.then(function (answer) {
if (answer === 'yes') {
usageDataConfig.collectUsageData = true
usageData.setUsageDataConfig(usageDataConfig)
promptPromise.then(function (permissionGranted) {
usageDataConfig.collectUsageData = permissionGranted
usageData.setUsageDataConfig(usageDataConfig)

if (permissionGranted) {
usageData.startTracking(usageDataConfig)
} else if (answer === 'no') {
usageDataConfig.collectUsageData = false
usageData.setUsageDataConfig(usageDataConfig)
} else {
console.error(answer)
}

runGulp()
})
} else if (usageDataConfig.collectUsageData === true) {
Expand Down

0 comments on commit f69d62b

Please sign in to comment.