Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/commands/config/clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ governing permissions and limitations under the License.

const { Flags } = require('@oclif/core')
const BaseCommand = require('../../base-command')
const { prompt } = require('../../prompt')
const { promptConfirm } = require('../../prompt')

class ClearCommand extends BaseCommand {
async run () {
const { flags } = await this.parse(ClearCommand)

if (!flags.force) {
const confirm = await prompt('are you sure? [yN]')
if (!confirm[0] || confirm[0].toLowerCase() !== 'y') {
const confirmed = await promptConfirm('are you sure?')
if (!confirmed) {
return
}
}
Expand Down
24 changes: 21 additions & 3 deletions src/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { input } = require('@inquirer/prompts')
const { input, confirm } = require('@inquirer/prompts')

// @inquirer/core pipes a MuteStream to process.stdout on every prompt call,
// adding an 'error' listener each time. oclif's plugin loading fills most of
// the default 10-slot budget before we even get here, so bump the limit to
// avoid the MaxListenersExceededWarning.
process.stdout.setMaxListeners(Math.max(process.stdout.getMaxListeners(), 20))
process.stderr.setMaxListeners(Math.max(process.stderr.getMaxListeners(), 20))

/**
* Prompts the user for input.
* Prompts the user for text input.
*
* @param {string} message - the prompt message to display
* @returns {Promise<string>} the user's input
Expand All @@ -22,4 +29,15 @@ async function prompt (message) {
return input({ message })
}

module.exports = { prompt }
/**
* Prompts the user for a yes/no confirmation.
*
* @param {string} message - the prompt message to display
* @param {boolean} defaultValue - the default value if the user just presses Enter (default: false)
* @returns {Promise<boolean>} true if the user confirmed
*/
async function promptConfirm (message, defaultValue = false) {
return confirm({ message, default: defaultValue })
}

module.exports = { prompt, promptConfirm }
9 changes: 5 additions & 4 deletions test/commands/config/clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const TheCommand = require('../../../src/commands/config/clear.js')
const { mockSet } = require('@adobe/aio-lib-core-config/src/Config')

jest.mock('../../../src/prompt', () => ({
prompt: jest.fn()
prompt: jest.fn(),
promptConfirm: jest.fn()
}))
const { prompt } = require('../../../src/prompt')
const { promptConfirm } = require('../../../src/prompt')

describe('clear', () => {
afterEach(() => {
Expand Down Expand Up @@ -46,14 +47,14 @@ describe('clear', () => {
})

test('prompt with yes', () => {
prompt.mockResolvedValue('y')
promptConfirm.mockResolvedValue(true)
return TheCommand.run([]).then(() => {
expect(mockSet).toHaveBeenCalledWith(null, null, false)
})
})

test('prompt with no', () => {
prompt.mockResolvedValue('n')
promptConfirm.mockResolvedValue(false)
return TheCommand.run([]).then(() => {
expect(mockSet).not.toHaveBeenCalled()
})
Expand Down
35 changes: 32 additions & 3 deletions test/commands/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { prompt } = require('../../src/prompt')
const { prompt, promptConfirm } = require('../../src/prompt')

jest.mock('@inquirer/prompts', () => ({
input: jest.fn()
input: jest.fn(),
confirm: jest.fn()
}))
const { input } = require('@inquirer/prompts')
const { input, confirm } = require('@inquirer/prompts')

describe('prompt', () => {
test('returns user input', async () => {
Expand All @@ -27,3 +28,31 @@ describe('prompt', () => {
expect(result).toEqual('user answer')
})
})

describe('promptConfirm', () => {
test('returns true when confirmed', async () => {
confirm.mockResolvedValue(true)

const result = await promptConfirm('are you sure?')

expect(confirm).toHaveBeenCalledWith({ message: 'are you sure?', default: false })
expect(result).toBe(true)
})

test('returns false when denied', async () => {
confirm.mockResolvedValue(false)

const result = await promptConfirm('are you sure?')

expect(confirm).toHaveBeenCalledWith({ message: 'are you sure?', default: false })
expect(result).toBe(false)
})

test('passes custom default value', async () => {
confirm.mockResolvedValue(true)

await promptConfirm('proceed?', true)

expect(confirm).toHaveBeenCalledWith({ message: 'proceed?', default: true })
})
})
Loading