Skip to content
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

Merged
merged 8 commits into from
Mar 4, 2018
Merged

Refactor Prompts #43

merged 8 commits into from
Mar 4, 2018

Conversation

lukeed
Copy link
Collaborator

@lukeed lukeed commented Mar 4, 2018

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 the args is accessory anyway -- it's pretty much just the onState key that gets discarded.

Moving forward, we can even consolidate some of the Elements' construction, but definitely not a priority.

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 the onCancel handler, immediately skipping the prompt instead. I also converted the throw for ensuring choices 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:

  • Merge all prompts types to build from single function
  • Throw Error from main loop if no message instead of silent skip
  • Ensure choices is always an Array instead of silent skip
  • Moderate rewrites, nothing major

@lukeed lukeed requested a review from terkelg March 4, 2018 03:24

if (!prompts.hasOwnProperty(question.type)) {
throw new Error(`prompt type ${question.type} not defined`);
if (prompts[type] === void 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this faster?

Copy link
Collaborator Author

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty clever 馃憤

Copy link
Collaborator Author

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

@terkelg
Copy link
Owner

terkelg commented Mar 4, 2018

This is so good and much needed! Nothing is better than removing code

@terkelg terkelg merged commit 855a873 into master Mar 4, 2018
@lukeed lukeed deleted the refactor branch March 4, 2018 04:02
const onAbort = opts.onAbort || noop;
const onSubmit = opts.onSubmit || noop;
p.on('state', args.onState || noop);
p.on('submit', x => res(onSubmit(x)));
Copy link
Owner

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

Copy link
Collaborator Author

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 }) {

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.

Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants