Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(async): add support for async check and coerce (#1872)
BREAKING CHANGE: yargs now returns a promise if async or check are asynchronous. refactor(coerce)!: coerce is now applied before validation. refactor(check): check callback is no longer passed an aliases object.
- Loading branch information
Showing
12 changed files
with
584 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// maybeAsyncResult() allows the same error/completion handler to be | ||
// applied to a value regardless of whether it is a concrete value or an | ||
// eventual value. | ||
// | ||
// As of yargs@v17, if no asynchronous steps are run, .e.g, a | ||
// check() script that resolves a promise, yargs will return a concrete | ||
// value. If any asynchronous steps are introduced, yargs resolves a promise. | ||
import {isPromise} from './is-promise.js'; | ||
export function maybeAsyncResult<T>( | ||
getResult: (() => T | Promise<T>) | T | Promise<T>, | ||
resultHandler: (result: T) => T | Promise<T>, | ||
errorHandler: (err: Error) => T = (err: Error) => { | ||
throw err; | ||
} | ||
): T | Promise<T> { | ||
try { | ||
const result = isFunction(getResult) ? getResult() : getResult; | ||
if (isPromise(result)) { | ||
return result.then((result: T) => { | ||
return resultHandler(result); | ||
}); | ||
} else { | ||
return resultHandler(result); | ||
} | ||
} catch (err) { | ||
return errorHandler(err); | ||
} | ||
} | ||
|
||
function isFunction(arg: (() => any) | any): arg is () => any { | ||
return typeof arg === 'function'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.