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

Move to ESM #26

Merged
merged 11 commits into from
Sep 14, 2021
Merged

Move to ESM #26

merged 11 commits into from
Sep 14, 2021

Conversation

msabramo
Copy link
Contributor

@msabramo msabramo commented Sep 13, 2021

@sindresorhus
Copy link
Member

I think it's better to do this after #16. Then you don't have a chance of conflicting and we can also upgrade dependencies and not need to ignore XO rules.

@msabramo msabramo marked this pull request as draft September 14, 2021 04:53
@msabramo
Copy link
Contributor Author

I changed this to a draft PR, so it doesn't accidentally get merged. I can resume this after #16 is done, like you said.

@sindresorhus sindresorhus marked this pull request as ready for review September 14, 2021 14:36
@sindresorhus
Copy link
Member

Can you rebase from main and fix the merge conflict?

@Qix-
Copy link
Member

Qix- commented Sep 14, 2021

Since we only seem to do squashes I went ahead and used the github resolver. It was just a small YAML change.

@@ -2,7 +2,6 @@ import test from 'ava';
import chalk from 'chalk';
import execa from 'execa';

process.env.FORCE_COLOR = true;
Copy link
Member

Choose a reason for hiding this comment

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

Curious, what's the reason for removing this?

Copy link
Contributor Author

@msabramo msabramo Sep 14, 2021

Choose a reason for hiding this comment

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

Oh good question. I think it's not necessary anymore, after adding 6458c59:

--- a/package.json
+++ b/package.json
@@ -16,7 +16,7 @@
                "node": ">=6"
        },
        "scripts": {
-               "test": "xo && ava"
+               "test": "xo && FORCE_COLOR=1 ava"
        },
        "files": [
                "cli.js"

Basically setting process.env.FORCE_COLOR wasn't really doing anything anymore, because it happens after the import of chalk and that's too late to affect the determination of whether stdout supports color or not. For that reason, I moved it to package.json.

package.json Outdated
"rules": {
"comma-dangle": ["warn", "only-multiline"],
"promise/prefer-await-to-then": "warn",
"arrow-body-style": "warn"
Copy link
Member

Choose a reason for hiding this comment

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

Not sure what @sindresorhus's stance is but I don't like warnings these days, I generally prefer errors or nothing at all.

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

The rules should be followed instead of ignored.

Copy link
Member

@Qix- Qix- Sep 14, 2021

Choose a reason for hiding this comment

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

So then these should be "error", 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.

Ah okay! Yeah I'd rather follow the rules than use warnings as well. The warnings were because I started working on this before #16 was merged and I was trying not to make too many changes that could cause conflicts. Now that #16 is merged, that is no longer a concern and I can change this to a more pure approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Changed the warnings to errors. Now there are 2 errors. Before fixing the errors, I'll quickly point out that xo and GitHub Actions `CI are doing their jobs to give us feedback to improve the code.

Screen Shot 2021-09-14 at 8 43 03 AM

Screen Shot 2021-09-14 at 8 43 29 AM

We need node >= 12 for ESM support.
Requires ESM.
I made a bunch `"warn"` instead of `"error"`, so I don't have to make so
many code changes right now, because I don't want to risk creating
conflicts with any ongoing work, such as chalkGH-16.
by:

1. using `await` on `getStdin()` instead of `then`.
2. wrapping top-level code in `async function main()`

   The top-level `await` seemed to *work* okay (at least for me in Node
   14.17.5). However, I see 2 problems with using it.

   1. According to [Top-level await is available in Node.js
      modules][top-level-await-in-node]:

      > Starting with **Node.js v14.8**, top-level await is available
      > (without the use of the `--harmony-top-level-await` command line flag).

      So it seems like this will be  a pain for folks using older Node
      versions.

    2. It seems xo doesn't like this:

       ```
       $ npm run test

       > chalk-cli@4.1.0 test
       > xo && FORCE_COLOR=1 ava

         cli.js:140:15
         ✖  140:15  Parsing error: Cannot use keyword await outside an async function

         1 error
       ```

In light of these 2 things, I decided to just wrap all the top level
code in an `async` function and call it a day, but let me know if you
have other ideas on how to solve these issues.

[top-level-await-in-node]: https://www.stefanjudis.com/today-i-learned/top-level-await-is-available-in-node-js-modules/#top-level-%60await%60-is-available-%22unflagged%22-in-node.js-since-%60v14.8%60
Some tooling would prefer this to be a URI with a scheme and it's
rendering with a squiggly in VS code and I see no reason why not to have
a URL with a scheme.
Update `"repository"` field in `package.json` to one of the recommended
forms described at
https://docs.npmjs.com/cli/v7/configuring-npm/package-json#repository
@msabramo
Copy link
Contributor Author

msabramo commented Sep 14, 2021

OK, I made the following changes:

  1. Changed xo warnings to errors.
  2. Fixed errors reported by xo.
  3. Cherry-picked minor package.json metadata changes from package.json: Make author.url a URL #24 (closed).

Tests are passing!

Screen Shot 2021-09-14 at 9 32 39 AM

Let me know if other changes are desired!

Extract `processDataFromArgs` & `processDataFromStdin` funcs.

This avoids top-level `await`, which seems problematic. It also breaks
up a large piece of code with lots of nesting into two simpler functions
and at least partially addresses
chalk#31
Copy link
Member

@Qix- Qix- left a comment

Choose a reason for hiding this comment

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

Github doesn't allow me to make comments on "unrelated" parts of a diff, frustratingly.

@sindresorhus I noticed you don't move files to .mjs. This would remove the need to have async function main() would it not? Since .mjs allows root level awaits. I noticed you don't do this in your other packages, though.

cli.js Outdated Show resolved Hide resolved
Copy link
Member

@Qix- Qix- left a comment

Choose a reason for hiding this comment

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

Ugh github is annoying me so much today.

cli.js Outdated
Comment on lines 142 to 143
const dataFromStdin = await getStdin();
init(dataFromStdin);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const dataFromStdin = await getStdin();
init(dataFromStdin);
init(await getStdin());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Fixed in f31fe1b.

@Qix-
Copy link
Member

Qix- commented Sep 14, 2021

This avoids top-level await, which seems problematic.

Why?

@msabramo
Copy link
Contributor Author

msabramo commented Sep 14, 2021

@qix:

This avoids top-level await, which seems problematic.

Why?

See 398bd39.

TL;DR:

  1. Top-level await isn't supported without passing ugly flags to node until Node 14.8+.
  2. xo emits warnings about the top-level await. Maybe eslint hasn't caught up with the latest Node.js gizmos?

Though I don't think these two points matter a ton now, after I added 4533221. That commit made it so instead of having a perhaps arguably gratuitous main function, we have two smaller functions that not only avoid the top-level await but they also modularize the code in a nice way.

@msabramo
Copy link
Contributor Author

msabramo commented Sep 14, 2021

@qix:

This would remove the need to have async function main()

Note that I removed the async function main() in 4533221.

@Qix-
Copy link
Member

Qix- commented Sep 14, 2021

The naked calls to async functions will result in unhandled promise rejection errors if they fail for some reason. Unless node has changed something and I haven't caught up, those calls will need to have .catch() clauses on them to properly show stack traces.

@sindresorhus
Copy link
Member

xo emits warnings about the top-level await. Maybe eslint hasn't caught up with the latest Node.js gizmos?

Correct

@sindresorhus
Copy link
Member

The naked calls to async functions will result in unhandled promise rejection errors if they fail for some reason. Unless node has changed something and I haven't caught up, those calls will need to have .catch() clauses on them to properly show stack traces.

meow handles that for us.

@sindresorhus sindresorhus merged commit a3efcb5 into chalk:main Sep 14, 2021
@msabramo
Copy link
Contributor Author

🎉

@msabramo
Copy link
Contributor Author

Is there a CHANGELOG.md or such where we should be adding these big new features like -n, the move to ESM, etc.?

@sindresorhus
Copy link
Member

Thanks for your help getting this done :)

https://github.com/chalk/chalk-cli/releases/tag/v5.0.0

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.

Convert to ESM
3 participants