-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Move to ESM #26
Conversation
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. |
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. |
Can you rebase from main and fix the merge conflict? |
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; |
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.
Curious, what's the reason for removing this?
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.
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" |
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.
Not sure what @sindresorhus's stance is but I don't like warnings these days, I generally prefer errors or nothing at all.
Thoughts?
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.
The rules should be followed instead of ignored.
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.
So then these should be "error"
, right?
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.
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.
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.
as described at https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c which was referenced at: chalk#20 (comment) Fixes: chalkGH-25
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
OK, I made the following changes:
Tests are passing! 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
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.
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 await
s. I noticed you don't do this in your other packages, though.
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.
Ugh github is annoying me so much today.
cli.js
Outdated
const dataFromStdin = await getStdin(); | ||
init(dataFromStdin); |
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.
const dataFromStdin = await getStdin(); | |
init(dataFromStdin); | |
init(await getStdin()); |
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.
Thanks! Fixed in f31fe1b.
Why? |
@qix:
See 398bd39. TL;DR:
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 |
as suggested by @qix in chalk#26 (comment)
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 |
Correct |
|
🎉 |
Is there a |
Thanks for your help getting this done :) |
as described at https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
which was referenced at: #20 (comment)
Fixes: GH-25
🎉