<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Peace Chen</title>
    <description>The latest articles on DEV Community by Peace Chen (@peacechen).</description>
    <link>https://dev.to/peacechen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F434445%2Fb20e7e80-9d5f-4294-b366-c7df6037ce60.png</url>
      <title>DEV Community: Peace Chen</title>
      <link>https://dev.to/peacechen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/peacechen"/>
    <language>en</language>
    <item>
      <title>Automated Cross-Platform Git Hooks with npm</title>
      <dc:creator>Peace Chen</dc:creator>
      <pubDate>Fri, 17 Jul 2020 23:10:58 +0000</pubDate>
      <link>https://dev.to/peacechen/automated-cross-platform-git-hooks-with-npm-1iof</link>
      <guid>https://dev.to/peacechen/automated-cross-platform-git-hooks-with-npm-1iof</guid>
      <description>&lt;p&gt;&lt;a href="https://git-scm.com/docs/githooks"&gt;Git hooks&lt;/a&gt; are a convenient way to help developers sanitize code automatically. Git supports a hook for virtually every Git action. A popular use case is leveraging &lt;code&gt;pre-commit&lt;/code&gt; to lint code.&lt;/p&gt;

&lt;p&gt;For better or for worse, Git hooks are not committed as part of the repository. Every developer manually installs the team's Git hooks for that repo. There must be a better way.&lt;/p&gt;

&lt;p&gt;Not wanting to reinvent this wheel, I searched for &lt;a href="https://githooks.com/"&gt;pre-made solutions&lt;/a&gt; but they didn't meet my requirements: simple, cross-platform, no additional third-party tools. Their typical requirements or limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manual creation of YAML or &lt;code&gt;.rc&lt;/code&gt; configuration files, or scripts in a non-native hook format.&lt;/li&gt;
&lt;li&gt;Installation of heavy dependencies (Python, Ruby...). Remember, every developer who works on the repo has to install those dependencies.&lt;/li&gt;
&lt;li&gt;Platform-specific scripts (*NIX or Windows only, not both).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting out of the way
&lt;/h2&gt;

&lt;p&gt;Ramp up time is crucial. Don't burden new developers with dozens of manual setup steps. Let the computer do the grunt work so that humans can get stuff done.&lt;/p&gt;

&lt;p&gt;Projects that use npm or yarn are very likely to have Node installed, and I leveraged that for cross-platform support.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/peacechen/node-git-hooks"&gt;node-git-hooks&lt;/a&gt; is deployment- and CI-friendly. It won't install the hooks if your library is used by another project, avoiding the error &lt;code&gt;Appears to be a git repo or submodule&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;npm install node-git-hooks --save&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Writing Git hooks should be fast and straight-forward. I kept it as close to the metal as possible. The hooks files are written exactly the same as they exist in the &lt;code&gt;.git/hooks&lt;/code&gt; folder. Create a &lt;code&gt;.githooks&lt;/code&gt; folder and place hooks files inside. An example &lt;code&gt;pre-commit&lt;/code&gt; script that performs linting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/sh
# Lint all files under the `src` directory
./node_modules/.bin/eslint src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Perhaps surprisingly, the above hook works on both *NIX and Windows. I recommend using a current version of npm or yarn. Older versions may not be as cross-platform friendly.&lt;/p&gt;

&lt;p&gt;Finally, add the following &lt;code&gt;prepare&lt;/code&gt; script to package.json:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "prepare": "node-git-hooks"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above steps in place, running &lt;code&gt;npm install&lt;/code&gt; (or &lt;code&gt;npm run prepare&lt;/code&gt;) copies the hooks files from &lt;code&gt;.githooks&lt;/code&gt; to &lt;code&gt;.git/hooks&lt;/code&gt;.  The hooks files may be committed to the repo and will be automatically installed to other devs' repos when they run &lt;code&gt;npm install&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I hope that this helps more teams adopt Git hooks. Source code is available at &lt;a href="https://github.com/peacechen/node-git-hooks"&gt;node-git-hooks&lt;/a&gt;. Comments and PRs are welcome!&lt;/p&gt;

</description>
      <category>git</category>
      <category>hooks</category>
      <category>npm</category>
      <category>node</category>
    </item>
  </channel>
</rss>
