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

watch task #575

Closed
jamiebuilds opened this issue Feb 2, 2017 · 20 comments
Closed

watch task #575

jamiebuilds opened this issue Feb 2, 2017 · 20 comments
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:enhancement A potential new feature to be added, or an improvement to how we print something

Comments

@jamiebuilds
Copy link

jamiebuilds commented Feb 2, 2017

It would nice to be able to use this as just:

yarn add --dev prettier
yarn prettier -- --write src/**/*.js --watch/-w

And only update the files that are changed as they are changed.

@vjeux
Copy link
Contributor

vjeux commented Feb 2, 2017

I wish there was a unix utility for that instead of each project having to reimplement it in a crappy way :(

@vjeux vjeux added the type:enhancement A potential new feature to be added, or an improvement to how we print something label Feb 2, 2017
@xtuc
Copy link

xtuc commented Feb 2, 2017

@vjeux there is actually unix inotify but you will still need some sort of Node binding.

@vjeux
Copy link
Contributor

vjeux commented Feb 2, 2017

@xtuc I was more thinking about

watch src/**/*.js | xargs prettier -- --write

@jamiebuilds
Copy link
Author

I am using https://github.com/mikeal/watch to do effectively that:

watch 'prettier --write src/**/*.js' src

The problem is that it will just spawn up a new prettier process and go through every single file again

@xtuc
Copy link

xtuc commented Feb 2, 2017

Yeah that would be awesome.

@lydell
Copy link
Member

lydell commented Feb 2, 2017

There's also cli-watch and chokidar-cli. Example:

chokidar 'src/**/*.js' -c "test '{event}' = 'change' && prettier --write '{path}'"

@jlongster
Copy link
Member

Do you have your editor instantly show changes from the filesystem? If not, how would save a file a file twice, since the second time there will have been changes to the file and you'll overwrite them? (My editor warns me about this and asks to confirm)

@jamiebuilds
Copy link
Author

My editor automatically shows changes. It's fast enough that it doesn't cause me any issues for now. If the watch task only rebuilds the file I'm working on it should stay that way

@jlongster
Copy link
Member

Interesting, that's the opposite workflow from usual, where we have editor plugins that will automatically reformat on save. The editor invokes prettier before saving, formats it, and then it's saved.

My experience with watchers has always been painful trying to get them working across operating systems. Is there any reason you don't want to have the editor invoke prettier, @thejameskyle ? Which editor are you using?

@jamiebuilds
Copy link
Author

Because I want it to run out of the box for everyone working on the project

@xtuc
Copy link

xtuc commented Feb 2, 2017

With Vim, i'm not notified that the file has changed unless I try to save. If I really save I will just override my version with the formated one.

Prettify before save is actually a better solution in my case.

I know many other editors will just load the new version of the file you're currently on.

@jlongster
Copy link
Member

@thejameskyle fwiw, many of the editor integrations also invoke the script from scratch. I think atom is the only one that doesn't because it's written in JS and already supports JS APIs first-class.

A more generic solution might be to introduce a "server" mode that just spawns it in the background and anything can run the CLI as many times as it wants. This would benefit all editors as well as your watch mode. Not sure how much more complex that would be, or if there's already a node module to help with that though.

@jamiebuilds
Copy link
Author

You might find this useful: http://langserver.org/

@baransu
Copy link

baransu commented Feb 14, 2017

If you want quick unix tool without implementing it into package I recommend entr.

@olpeh
Copy link

olpeh commented Mar 10, 2017

FYI: It tried using prettier together with npm-watch but it wrote the files as empty.

Tried using https://github.com/mikeal/watch instead as @thejameskyle suggested and it seems to work.

This is good enough at least for me.

@vjeux
Copy link
Contributor

vjeux commented Mar 10, 2017

@olpeh: could you create a test plan that reproduces the issue? This way we can look into it.

@olpeh
Copy link

olpeh commented Mar 10, 2017

@vjeux Sure. Should I create a new issue of that or add it here?
Is there a template I should follow?

@vjeux
Copy link
Contributor

vjeux commented Mar 10, 2017

A new issue would be great. No template, just make sure there's enough information so that we can investigate :)

josephfrazier added a commit to josephfrazier/prettier that referenced this issue Apr 18, 2017
`prettier_d` is like [eslint_d], but it runs `prettier` instead of
`eslint`. This eliminates the Node.js startup delay from all but the
first run of `prettier_d`, making it a snappier experience.

[eslint_d]: https://github.com/mantoni/eslint_d.js

Related discussion:
* prettier#575 (comment)
* prettier#918 (comment)
* prettier#753 (comment)
vjeux pushed a commit that referenced this issue Apr 18, 2017
`prettier_d` is like [eslint_d], but it runs `prettier` instead of
`eslint`. This eliminates the Node.js startup delay from all but the
first run of `prettier_d`, making it a snappier experience.

[eslint_d]: https://github.com/mantoni/eslint_d.js

Related discussion:
* #575 (comment)
* #918 (comment)
* #753 (comment)
@vjeux
Copy link
Contributor

vjeux commented May 21, 2017

I'm doing a spring cleaning of the issues and we're unlikely going to implement it, so closing. If someone is interested in building this, there is no opposition.

@eedrah
Copy link
Contributor

eedrah commented Oct 31, 2017

@vjeux - I thought perhaps it might be nice to put in the README how to use this with an [external] watcher - that way it hopefully will please everyone wanting a watcher, without having requests to reimplement this functionality in the code of Prettier.

Thoughts? I made pull request #3126 if you want it.

For those who are coming here from Google wanting this functionality, the short version is:

npx onchange '**/*.js' -- npx prettier --write {{changed}}

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:enhancement A potential new feature to be added, or an improvement to how we print something
Projects
None yet
Development

No branches or pull requests

8 participants