prettier icon indicating copy to clipboard operation
prettier copied to clipboard

feat(stdin): added new cli for accepting stdin

Open kkyusufk opened this issue 3 years ago • 4 comments

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 devDependencies object "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.md file following changelog_unreleased/TEMPLATE.md.
  • [x] I’ve read the contributing guidelines.

Try the playground for this PR

kkyusufk avatar Sep 05 '22 10:09 kkyusufk

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?

kkyusufk avatar Sep 05 '22 11:09 kkyusufk

@fisker Please take a look again whenever you get the time Only one comment is left to address which I do not understand 😅

kkyusufk avatar Sep 17 '22 14:09 kkyusufk

https://github.com/prettier/prettier/pull/13428#discussion_r973593191

fisker avatar Sep 17 '22 14:09 fisker

@fisker ready for another round. Should be close to approval by now 🤞🏻

kkyusufk avatar Sep 17 '22 18:09 kkyusufk

Hey @fisker , apologies for neglecting this PR for a long time. Please review, I fixed the issue on windows.

kkyusufk avatar Oct 13 '22 18:10 kkyusufk

@fisker pinging for another review.

kkyusufk avatar Oct 17 '22 15:10 kkyusufk

Sorry for missing your comment. Will try again soon.

fisker avatar Oct 17 '22 15:10 fisker

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 avatar Oct 20 '22 06:10 fisker

@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.

kkyusufk avatar Oct 20 '22 19:10 kkyusufk