1 | module.exports = inputAnswers => async prompts => {
|
2 | inputAnswers = inputAnswers || {}
|
3 | const answers = {}
|
4 |
|
5 | for (const p of prompts) {
|
6 | const setValue = async val => {
|
7 | if (p.validate) {
|
8 | const res = await p.validate(val)
|
9 | if (res !== true) {
|
10 | throw new Error(`validation failed for prompt: ${p}`)
|
11 | }
|
12 | }
|
13 |
|
14 | answers[p.name] = p.filter ? await p.filter(val) : val
|
15 | }
|
16 |
|
17 | const when = typeof p.when === 'function' ? await p.when(answers) : p.when
|
18 | if (when === undefined || when === true) {
|
19 | const defaultValue =
|
20 | inputAnswers[p.name] === undefined ? p.default : inputAnswers[p.name]
|
21 | if (defaultValue !== undefined) {
|
22 | await setValue(
|
23 | typeof defaultValue === 'function'
|
24 | ? defaultValue(answers)
|
25 | : defaultValue
|
26 | )
|
27 | }
|
28 | }
|
29 |
|
30 | const a = answers[p.name]
|
31 | if (a === undefined && p.required) {
|
32 | throw new Error(`missing answer for required prompt: ${p}`)
|
33 | }
|
34 | }
|
35 |
|
36 | return answers
|
37 | }
|