-
-
Notifications
You must be signed in to change notification settings - Fork 302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Prompts #43
Refactor Prompts #43
Conversation
|
||
if (!prompts.hasOwnProperty(question.type)) { | ||
throw new Error(`prompt type ${question.type} not defined`); | ||
if (prompts[type] === void 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this faster?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep and effectively does same thing.
let MAP = prompt._map || {}; | ||
|
||
for (question of questions) { | ||
name = question.name; | ||
({ name, type } = question); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty clever 馃憤
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, little out of ordinary but hey
This is so good and much needed! Nothing is better than removing code |
const onAbort = opts.onAbort || noop; | ||
const onSubmit = opts.onSubmit || noop; | ||
p.on('state', args.onState || noop); | ||
p.on('submit', x => res(onSubmit(x))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now all prompts also have individual onAbort
and onSubmit
. That's cool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Individual prompt functions, yeah. But not the user-defined prompt objects.
lib/prompts.js
Outdated
* @param {Function} [args.suggest] Function to filter results based on user input. Defaults to sort by `title` | ||
* @param {number} [args.limit=10] Max number of results to show | ||
* @param {string} [args.style="default"] Render style ('default', 'password', 'invisible') | ||
* @param {function} [args.onState] On state change callback | ||
* @returns {Promise} Promise with user input | ||
*/ | ||
function autocomplete({ message, choices, suggest, limit, style, onState = noop }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is still destructed object, but not args
as expected below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old commit. Fixed in 6457a15
We were destructuring all prompt-objects, only to reassemble them in nearly the same way. While this made it a little more legible in some ways, it also added a lot of noise.
Looking at the underling
elements
, they were also picking out the keys that they cared about, which meant that we could safely pass everything down to them. In most cases, hardly anything from theargs
is accessory anyway -- it's pretty much just theonState
key that gets discarded.Anyway, after clearing up a bunch of the pattern matching, it was pretty clear that our
prompts.js
was doing the same thing for every type, with a tiny exception here n' there.The entire file is now 44 lines of code, plus 102 lines of whitespace and docs 馃槅
Also, I moved the
message
checking into the main loop. The errors were being swallowed by theonCancel
handler, immediately skipping theprompt
instead. I also converted thethrow
for ensuringchoices
was an Array into the action of just ensuring it's an Array. It'll be pretty obvious that something's wrong when there are 0 choices 馃槈TLDR:
prompts
types to build from single functionmessage
instead of silent skipchoices
is always an Array instead of silent skip