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 documentation of global script options #870

Merged
merged 1 commit into from
Mar 2, 2024

Conversation

ehmicky
Copy link
Collaborator

@ehmicky ehmicky commented Feb 29, 2024

This improves the documentation related to sharing options with $.

Unfortunately, users cannot just do:

import {$} from 'execa';
$ = $(options);

Because imports are const.

Doing the following:

import {$} from 'execa';
const $$ = $(options);

Requires renaming all $ to $$ in the file.

The following does not require any renaming:

import {$ as $$} from 'execa';
const $ = $$(options);

I've tried thinking of a better pattern, but could not come up with one. :/

@sindresorhus
Copy link
Owner

It may be less confusing to do this, since we already use $$ for a different purpose.

import {$ as $_} from 'execa';
const $ = $_(options);

@ehmicky
Copy link
Collaborator Author

ehmicky commented Feb 29, 2024

$_ sounds good to me since it makes it clearer that is no different than $. 👍

For the other places where we are using $$, are those purposes different? 🤔 We currently document creating $$ as a new $ with bound options. We could draw a distinction between whether this new $ will be used in the whole script ("global" options) or only a few commands ("shared" options), but in the end, it is quite similar and uses the same method.

In other words, maybe we should replace all $$ by $_ in the documentation?

@tommy-mitchell
Copy link

I like the distinction between a global $ and a local $$. I use them separately here: https://github.com/tommy-mitchell/listr-cli/blob/main/src/tasks.ts

@ehmicky
Copy link
Collaborator Author

ehmicky commented Mar 1, 2024

Thanks for your input @tommy-mitchell and @aaronccasanova, let's go with what you suggest:

  • global options: import {$ as $_} + const $ = $_(options)
  • shared options: const $$ = $(options)

In the end, those are just variable names, and users could use different ones, but it's nice to have a convention.

I have updated the PR accordingly.
@tommy-mitchell and @aaronccasanova please feel free to review this PR if you want, thanks!

//=> 'unicorns'

await $$`echo rainbows`;
await $`echo rainbows`;
//=> 'rainbows'
```
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This file also includes an example of const $$ = $(options) for shared options. It shows the following text, which did not need to be changed:

This can be used to either:
  - Set options for a specific command: `$(options)`command` `
  - Share options for multiple commands: `const $$ = $(options); $$`command`; $$`otherCommand`;`

@sindresorhus sindresorhus merged commit 146fa07 into main Mar 2, 2024
14 checks passed
@sindresorhus sindresorhus deleted the shared-options-doc branch March 2, 2024 06:16
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