File tree Expand file tree Collapse file tree 2 files changed +44
-2
lines changed
Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -211,11 +211,27 @@ module.exports = function (opts = {}) {
211211 }
212212
213213 // Handle default as functions, augment with entire options
214- const __default = typeof defaults [ key ] !== 'undefined' ? defaults [ key ] : prompt . default
214+ const defaultFromRoot = typeof prompt . default === 'undefined'
215+ const __default = ! defaultFromRoot ? prompt . default : defaults [ key ]
215216 let _default = __default
216217 if ( typeof _default === 'function' ) {
217218 _default = ( ans ) => {
218- return __default ( ans , values ( ans ) )
219+ let ret = __default ( ans , values ( ans ) )
220+ if ( ret && typeof ret . then === 'function' ) {
221+ // Resolve promise
222+ ret = ret . then ( ( val ) => {
223+ // If the default came from the root of the options object,
224+ // ensure we assign it so undefined's are preserved
225+ if ( val === undefined && defaultFromRoot ) {
226+ defaults [ key ] = val
227+ }
228+ return val
229+ } )
230+ } else if ( ret === undefined && defaultFromRoot ) {
231+ // See above on undefineds from the root options object
232+ defaults [ key ] = ret
233+ }
234+ return ret
219235 }
220236 }
221237
Original file line number Diff line number Diff line change @@ -289,4 +289,30 @@ suite(pkg.name, () => {
289289 baz : 'baz'
290290 } )
291291 } )
292+
293+ test ( 'default functions which return undefined' , async ( ) => {
294+ const opts = opta ( {
295+ options : {
296+ foo : {
297+ default : ( ) => undefined
298+ } ,
299+ bar : {
300+ default : async ( ) => undefined
301+ }
302+ } ,
303+ promptModule : ( ) => {
304+ return async ( prompts ) => {
305+ return {
306+ foo : prompts [ 0 ] . default ( ) ,
307+ bar : await prompts [ 1 ] . default ( )
308+ }
309+ }
310+ }
311+ } )
312+
313+ await opts . prompt ( ) ( )
314+ const o = opts . values ( )
315+ assert . strictEqual ( o . foo , undefined )
316+ assert . strictEqual ( o . bar , undefined )
317+ } )
292318} )
You can’t perform that action at this time.
0 commit comments