Where you been?
If you haven’t heard of Prettier you are missing out on one of the best tools for the javascript community. I’ve been hanging around on the outskirts of adopting Prettier because I’m very focused on TypeScript projects and initially there was no support for TypeScript. That was until 12 days ago when 1.4.0 was published. So now I’m diving head in on all projects to default with Prettier support. I’ll keep this brief, so let’s dive in.
The Steps
First, we need to add a few packages to our project as dev dependencies. Run the following from your terminal:
npm install prettier lint-staged husky --save-dev
You can use yarn if you’d like instead of npm to install the dev dependencies.
Second, open your project’s package.json and add the following script
and object for lint-staged
"scripts": { "precommit": "lint-staged" }, "lint-staged": { "*.ts": [ "prettier --write", "git add" ] },
That’s basically all you need to do. Pretty simple, our precommit
script will run lint-staged
which will run the prettier
command on the .ts files being committed.
Thanks, Brad! How do I get git to use the precommit hook? Is it best to use a GUI like GitHub Desktop or GitKraken for this?
LikeLike
Hey Ken, that might be possible. I’ve not had to do anything extra other than setting up the script in package.json and using my IDE (VSCode currently). Once I stage then commit it should run the script and prettier kick in. Sorry I’m probably not much help here. I don’t do a lot of `git` besides the basics so I might just be ignorant on what you are asking really 🙂
LikeLike
Now that I read your comment again, are you a git terminal dev? If so, then I guess when you go to `push` it should execute but yea I use the git integration in VSCode 99% of the time and this simple modification works without any other steps 🙂
LikeLike
Thanks for the info, Brad! I recently stumbled about, working on your `nativescript-texttospeech` project, but thanks to this post I figured it out and added it to my current project as well. I spent some time upon investigating prettier, but it seems to be pretty (or should I say ‘prettie’? ;)) cool. 🙂
LikeLiked by 1 person
I don’t do a lot of `git` besides the basics so I might just be ignorant on what you are asking really 🙂 How do I get git to use the precommit hook?
LikeLike
Hi Charles, you should be able to follow this post and put the `precommit` inside your project’s package.json – once that directory is linked to a git repo it should execute correctly before committing.
LikeLike
I wear’t do a circle of `git` besides the basics so I might just be ignorant on what you are asking really 🙂 How do I become git to enjoyment the precommit hooking? How do I become git to enjoyment the precommit hooking?
LikeLike
I acquire into’t do a destiny of `git` besides the basics so I might just be ignorant on what you are asking really 🙂 How do I acquire git to use of goods and services the precommit hook shot? I vesture’t do a rope of `git` besides the basics so I might just be ignorant on what you are asking really 🙂 How do I become git to delectation the precommit hook shoting?
LikeLike
You have not configured the pretty in scripts, and it isn’t working.
LikeLike