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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ora.promise() #181

Merged
merged 11 commits into from Aug 23, 2021
Merged

Improve ora.promise() #181

merged 11 commits into from Aug 23, 2021

Conversation

SrBrahma
Copy link
Contributor

@SrBrahma SrBrahma commented Jul 3, 2021

Implemented #169 (comment)

Improves ora.promise:

  1. The action can now be a function. It will automatically be executed. The corresponding spinner will be used as argument to that function.
  2. It will now return a promise, that resolves when ora.promise given promise/function is resolved, so the user can await ora.promise(..).
  3. Added successText and failText, that will be used in the spinner.succeed()/spinner.fail(). They can be either a string or a function. Being a function, successText will receive as argument the return of the action resolve, and failText will receive the action reject content.

Fixes #169

index.d.ts Outdated Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

The pull request needs a proper descriptive title.

package.json Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

Please always run npm test locally before pushing changes.

@SrBrahma
Copy link
Contributor Author

SrBrahma commented Jul 3, 2021

Please always run npm test locally before pushing changes.

I did and fixed my own errors, but there were other errors not related to my changes

@SrBrahma
Copy link
Contributor Author

SrBrahma commented Jul 3, 2021

I have no idea on how to fix this npm test issue:
image

@SrBrahma SrBrahma changed the title changed ora.promise Improves ora.promise - Action may be a function, returns a promise, added successText and failText Jul 3, 2021
@SrBrahma
Copy link
Contributor Author

SrBrahma commented Jul 25, 2021

I found out why the test error was happening. The new promise() was calling the promise that always rejects. I thought the promise was being rejected before entering the promise() call. Also, the PromiseOptions type definition was missing, looks like I only had added it to the README.

test.js Outdated Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
readme.md Outdated

If undefined, will keep the initial text. */
failText?: ((error: Error) => string) | string;
```
Copy link
Owner

Choose a reason for hiding this comment

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

The readme docs should use the same formatting as used here: https://github.com/sindresorhus/boxen#options (Not just a copy paste of the TS types).

All of the [options](#options) plus the following:

##### successText
Type: `string | ((result: T) => string)`
Copy link
Contributor Author

@SrBrahma SrBrahma Jul 25, 2021

Choose a reason for hiding this comment

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

For the functions, I used this as pattern: https://github.com/sindresorhus/got/blob/main/documentation/5-https.md#checkserveridentity. Please, change it further if wanted to fit your README styling.

index.d.ts Outdated
Comment on lines 136 to 149
interface PromiseOptions<T> extends Options {
/**
The new text of the spinner when the promise is resolved.

If undefined, will keep the initial text.
*/
successText?: string | ((result: T) => string);

/**
The new text of the spinner when the promise is rejected.

If undefined, will keep the initial text.
*/
failText?: string | ((error: Error) => string);
Copy link
Owner

Choose a reason for hiding this comment

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

Still messy formatting. Please look over the whole diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know why this is happening at all. In my VSCode the indentation is right

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't have EditorConfig extension installed here, I was using 2 spaces instead of tab (size 2). The code was looking the same in VSCode, but looks like the diff in GitHub renders tabs as 8 spaces, that's why it was visually strange.

I activated the whitespace rendering, and that's what was happening:

image

I found out that you are the XO dev. Maybe would be a good idea to have a pre-commit/GitHub action to lint this. Or even, to throw a warn/error on test if spaces were found instead of tabs. In eslint, this is doable.

I hope everything is fixed now.

Copy link
Contributor Author

@SrBrahma SrBrahma Aug 2, 2021

Choose a reason for hiding this comment

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

Just for your information, there is a cool eslint plugin for JSDoc: https://github.com/gajus/eslint-plugin-jsdoc

I will soon add it to my personal eslint-config, as I also have my personal taste for JSDoc comments. Maybe you could implement a similar thing for XO to avoid those issues among all your repos, in PRs like mine.

Copy link
Owner

Choose a reason for hiding this comment

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

Or even, to throw a warn/error on test if spaces were found instead of tabs. In eslint, this is doable.

That's already a thing. Ora just didn't have the latest XO version.

However, you are still expected to look over your own PR diff.

Copy link
Owner

Choose a reason for hiding this comment

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

Just for your information, there is a cool eslint plugin for JSDoc: gajus/eslint-plugin-jsdoc

I plan to add it at some point, but it's some work to evaluate all the rules, and I haven't had the time to do that yet: xojs/xo#378

@sindresorhus sindresorhus changed the title Improves ora.promise - Action may be a function, returns a promise, added successText and failText Improve Ora.promise() Aug 23, 2021
@sindresorhus sindresorhus changed the title Improve Ora.promise() Improve ora.promise() Aug 23, 2021
@sindresorhus sindresorhus merged commit 9c01990 into sindresorhus:main Aug 23, 2021
@sindresorhus
Copy link
Owner

Thanks :)

nbl7 pushed a commit to nbl7/ora that referenced this pull request Mar 15, 2022
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.

Suggestion: promise enhancements
2 participants