From 89cd82db18655bbb085eb72c339402f412dd3996 Mon Sep 17 00:00:00 2001 From: Ankit Tiwari Date: Fri, 15 Nov 2019 18:12:06 +0530 Subject: [PATCH] :bug: Execute default function to get default answer when -y flag is passed (#185) --- src/ask-questions.js | 2 +- src/utils.js | 19 +++++++++++-------- src/utils.spec.js | 28 ++++++++++++++-------------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/ask-questions.js b/src/ask-questions.js index b72a24e..5f0afa6 100644 --- a/src/ask-questions.js +++ b/src/ask-questions.js @@ -16,7 +16,7 @@ module.exports = async (projectInfos, useDefaultAnswers) => { ) const answersContext = useDefaultAnswers - ? utils.getDefaultAnswers(questions) + ? await utils.getDefaultAnswers(questions) : await inquirer.prompt(questions) return { diff --git a/src/utils.js b/src/utils.js index 68cf0ab..6b733b2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -74,12 +74,14 @@ const getPackageJson = async () => { * * @param {Object} question */ -const getDefaultAnswer = (question, answersContext) => { +const getDefaultAnswer = async (question, answersContext) => { if (question.when && !question.when(answersContext)) return undefined switch (question.type) { case 'input': - return question.default || '' + return typeof question.default === 'function' + ? question.default(answersContext) + : question.default || '' case 'checkbox': return question.choices .filter(choice => choice.checked) @@ -110,13 +112,14 @@ const isProjectAvailableOnNpm = projectName => { * @param {Array} questions */ const getDefaultAnswers = questions => - questions.reduce( - (answersContext, question) => ({ + questions.reduce(async (answersContextProm, question) => { + const answersContext = await answersContextProm + + return { ...answersContext, - [question.name]: getDefaultAnswer(question, answersContext) - }), - {} - ) + [question.name]: await getDefaultAnswer(question, answersContext) + } + }, Promise.resolve({})) /** * Clean social network username by removing the @ prefix and diff --git a/src/utils.spec.js b/src/utils.spec.js index e72c89f..1e5e9bd 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.js @@ -120,45 +120,45 @@ describe('utils', () => { }) describe('getDefaultAnswer', () => { - it('should handle input prompts correctly', () => { + it('should handle input prompts correctly', async () => { const question = { type: 'input', default: 'default' } - const result = getDefaultAnswer(question) + const result = await getDefaultAnswer(question) expect(result).toEqual(question.default) }) - it('should handle choices prompts correctly', () => { + it('should handle choices prompts correctly', async () => { const value = { name: 'name', value: 'value' } const question = { type: 'checkbox', choices: [{ value, checked: true }, { checked: false }] } - const result = getDefaultAnswer(question) + const result = await getDefaultAnswer(question) expect(result).toEqual([value]) }) - it('should return empty string for non-defaulted fields', () => { + it('should return empty string for non-defaulted fields', async () => { const question = { type: 'input' } - const result = getDefaultAnswer(question) + const result = await getDefaultAnswer(question) expect(result).toEqual('') }) - it('should return undefined for invalid types', () => { + it('should return undefined for invalid types', async () => { const question = { type: 'invalid' } - const result = getDefaultAnswer(question) + const result = await getDefaultAnswer(question) expect(result).toEqual(undefined) }) - it('should return undefined if when function is defined and return false', () => { + it('should return undefined if when function is defined and return false', async () => { const answersContext = {} const question = { type: 'input', when: ansewersContext => !isNil(ansewersContext.licenseUrl) } - const result = getDefaultAnswer(question, answersContext) + const result = await getDefaultAnswer(question, answersContext) expect(result).toEqual(undefined) }) @@ -177,7 +177,7 @@ describe('utils', () => { }) }) - it('should return correct value if when function is defined and return true', () => { + it('should return correct value if when function is defined and return true', async () => { const answersContext = { licenseUrl: 'licenseUrl' } const question = { type: 'input', @@ -185,14 +185,14 @@ describe('utils', () => { when: ansewersContext => !isNil(ansewersContext.licenseUrl) } - const result = getDefaultAnswer(question, answersContext) + const result = await getDefaultAnswer(question, answersContext) expect(result).toEqual('default') }) }) describe('getDefaultAnswers', () => { - it('should return default answers from questions', () => { + it('should return default answers from questions', async () => { const questions = [ { type: 'input', @@ -206,7 +206,7 @@ describe('utils', () => { } ] - const result = getDefaultAnswers(questions) + const result = await getDefaultAnswers(questions) expect(result).toEqual({ questionOne: 'answer 1',