feat(stdin): added new cli for accepting stdin
Description
Resolves #13271
New feature 🎉
Added a new cli option --stdin
Now we can provide prettier with code using std input and it will prettify the code and return the answer in the terminal itself.
How to use
- Run
prettier --stdin --stdin-filepath xyz.js. - Prettier will go into read input mode.
- Start writing your code which you want to be prettified.
- press ctrl+d to exit the read mode.
- Prettier will prettify the input code and print it to the terminal.
Testing done
- Made a test repository and added this line in the
devDependenciesobject"prettier": "[email protected]:prettier/prettier.git#2e95bf4beafe29360a20eae867e77fd208e1c29f" - ran
npm i - open terminal and run
npx prettier --stdin-filepath xyz.js
Checklist
- [x] I’ve added tests to confirm my change works.
- [x] (If changing the API or CLI) I’ve documented the changes I’ve made (in the
docs/directory). - [x] (If the change is user-facing) I’ve added my changes to
changelog_unreleased/*/XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md. - [x] I’ve read the contributing guidelines.
Open question:
With this feature added we can probably make the argument to remove the old way of passing stdin (using the cat command). Should we remove it or keep it around?
@fisker Please take a look again whenever you get the time Only one comment is left to address which I do not understand 😅
https://github.com/prettier/prettier/pull/13428#discussion_r973593191
@fisker ready for another round. Should be close to approval by now 🤞🏻
Hey @fisker , apologies for neglecting this PR for a long time. Please review, I fixed the issue on windows.
@fisker pinging for another review.
Sorry for missing your comment. Will try again soon.
It seems I have to press Ctrl + D on a new line to exit (can't exit if cursor in middle of text). And ↑ ↓ doesn't work as expected.
@fisker Readline only registers a line after we press enter. Seems like this will be a bad DX, I will try to iterate on this. Let's see if there is anything we can use.
If not, maybe we just add those instructions, Pressing Ctrl+D after pressing enter will format.