Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.

Commit b392a80

Browse files
author
Anshul Guleria
committed
[FEATURE] Adds code for setting commit template
Adds configuration files for linting and git hooks required for package setup. Added code to set git template when the pre-commit-msg hook call this script. [TESTING] Tested by setting the same script locally for this package.
1 parent c8c3654 commit b392a80

File tree

13 files changed

+3494
-3
lines changed

13 files changed

+3494
-3
lines changed

.commit-msg

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[FEATURE | BUG | DOCS | CHORE] One liner
2+
# One liner describing the change(<=50 chars)
3+
4+
# FEATURE: When you are adding new feature then use this tag. Usually this
5+
# updates minor version for non-breaking change and major version for breaking
6+
# change. Command: `npm version minor` or `npm version major`
7+
#
8+
# BUG: When you are fixing an issue in the repository. Usually this changes the
9+
# patch version of the package. Command: `npm version patch`
10+
#
11+
# DOCS: When you are updating the documentation for the package. This is also a
12+
# patch version change as this does not breaks anything
13+
#
14+
# CHORE: When you are doing some minor code re-adjustments for re-factoring
15+
# which does not changes any interface or fixes any bug. This is also a patch
16+
# version change.
17+
18+
# Description (<= 72 chars)
19+
# Should reach max 72 characters before wrapping to new
20+
# line. Keep this section descriptive.
21+
Describe the issue you are facing and how you solved it with the
22+
proposed changes.
23+
24+
[TESTING]
25+
26+
# Explain the how you tested the change. If possible also provide links
27+
# where your working solution is hosted so that reviewer can test it.
28+
How you tested this change.
29+
30+
closes: #ISSUE_NUMBER
31+
32+
# If you want to close multiple issues you can do:
33+
34+
# closes: #ISSUE_NUMBER1
35+
36+
# closes: #ISSUE_NUMBER2
37+
38+
# Closes issue in another repository
39+
# closes: username/repository#ISSUE_NUMBER
40+
41+
# For full list of available syntax and options you can read github
42+
# documentation on
43+
# [closing-issues-using-keywords](https://help.github.com/en/articles/closing-issues-using-keywords).
44+
45+
# This is the default autogenerated template by git-commit-template
46+
# package. For more info read package documentation at
47+
# https://github.com/amzn/git-commit-template

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
9+
# 2 space indentation
10+
indent_style = space
11+
indent_size = 2
12+
13+
# Set default charset
14+
[*.{js,jsx,ts,tsx}]
15+
charset = utf-8

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore markdown files
2+
**/*.md

.eslintrc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
env: {
3+
commonjs: true,
4+
es6: true,
5+
node: true,
6+
},
7+
extends: "eslint:recommended",
8+
globals: {
9+
Atomics: "readonly",
10+
SharedArrayBuffer: "readonly",
11+
},
12+
parserOptions: {
13+
ecmaVersion: 2018,
14+
},
15+
plugins: ["json"],
16+
rules: {
17+
indent: ["error", 2, { SwitchCase: 1 }],
18+
"linebreak-style": ["error", "unix"],
19+
quotes: ["error", "double"],
20+
semi: ["error", "always"],
21+
"no-console": "off",
22+
curly: "warn",
23+
"arrow-parens": ["error", "always"],
24+
},
25+
};

.gitignore

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
# Created by https://www.gitignore.io/api/vim,node,macos
3+
# Edit at https://www.gitignore.io/?templates=vim,node,macos
4+
5+
### macOS ###
6+
# General
7+
.DS_Store
8+
.AppleDouble
9+
.LSOverride
10+
11+
# Icon must end with two \r
12+
Icon
13+
14+
# Thumbnails
15+
._*
16+
17+
# Files that might appear in the root of a volume
18+
.DocumentRevisions-V100
19+
.fseventsd
20+
.Spotlight-V100
21+
.TemporaryItems
22+
.Trashes
23+
.VolumeIcon.icns
24+
.com.apple.timemachine.donotpresent
25+
26+
# Directories potentially created on remote AFP share
27+
.AppleDB
28+
.AppleDesktop
29+
Network Trash Folder
30+
Temporary Items
31+
.apdisk
32+
33+
### Node ###
34+
# Logs
35+
logs
36+
*.log
37+
npm-debug.log*
38+
yarn-debug.log*
39+
yarn-error.log*
40+
lerna-debug.log*
41+
42+
# Diagnostic reports (https://nodejs.org/api/report.html)
43+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
44+
45+
# Runtime data
46+
pids
47+
*.pid
48+
*.seed
49+
*.pid.lock
50+
51+
# Directory for instrumented libs generated by jscoverage/JSCover
52+
lib-cov
53+
54+
# Coverage directory used by tools like istanbul
55+
coverage
56+
57+
# nyc test coverage
58+
.nyc_output
59+
60+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
61+
.grunt
62+
63+
# Bower dependency directory (https://bower.io/)
64+
bower_components
65+
66+
# node-waf configuration
67+
.lock-wscript
68+
69+
# Compiled binary addons (https://nodejs.org/api/addons.html)
70+
build/Release
71+
72+
# Dependency directories
73+
node_modules/
74+
jspm_packages/
75+
76+
# TypeScript v1 declaration files
77+
typings/
78+
79+
# Optional npm cache directory
80+
.npm
81+
82+
# Optional eslint cache
83+
.eslintcache
84+
85+
# Optional REPL history
86+
.node_repl_history
87+
88+
# Output of 'npm pack'
89+
*.tgz
90+
91+
# Yarn Integrity file
92+
.yarn-integrity
93+
94+
# dotenv environment variables file
95+
.env
96+
.env.test
97+
98+
# parcel-bundler cache (https://parceljs.org/)
99+
.cache
100+
101+
# next.js build output
102+
.next
103+
104+
# nuxt.js build output
105+
.nuxt
106+
107+
# vuepress build output
108+
.vuepress/dist
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
### Vim ###
120+
# Swap
121+
[._]*.s[a-v][a-z]
122+
[._]*.sw[a-p]
123+
[._]s[a-rt-v][a-z]
124+
[._]ss[a-gi-z]
125+
[._]sw[a-p]
126+
127+
# Session
128+
Session.vim
129+
130+
# Temporary
131+
.netrwhist
132+
*~
133+
# Auto-generated tag files
134+
tags
135+
# Persistent undo
136+
[._]*.un~
137+
138+
# End of https://www.gitignore.io/api/vim,node,macos
139+
140+
141+
# Our custom ignores
142+
build
143+
# From idea editor
144+
*.iml

.huskyrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
hooks: {
3+
"pre-commit": "lint-staged",
4+
"prepare-commit-msg": "./bin/git-commit-template.js",
5+
},
6+
};

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
1-
## Git Commit Template
1+
# git-commit-template
22

3-
Set commit templates for git
3+
Sets commit template for your git projects. This is a husky plugin which should
4+
be used on `prepare-commit-msg` hook exposed by git.
5+
6+
It exposes a cli command with name `git-commit-template` which you can use in
7+
your husky configuration. For example:
8+
9+
```javascript
10+
// .huskyrc.js
11+
module.exports = {
12+
hooks: {
13+
"prepare-commit-msg": "git-commit-template",
14+
},
15+
};
16+
```
17+
18+
## `git-commit-template` command
19+
20+
This command takes an optional parameter of your commit message file name. If
21+
provided, then it will read the file provided by you, otherwise uses its default
22+
configuration file. Read [`.commit-msg`](.commit-msg) file to see the default configuration
23+
added by us.
24+
25+
Sample for providing file name:
26+
27+
```javascript
28+
// .huskyrc.js
29+
module.exports = {
30+
hooks: {
31+
"prepare-commit-msg": "git-commit-template .my-custom-git-msg-file",
32+
},
33+
};
34+
```
35+
36+
> Note: This hooks provides you default commit message only if you haven't
37+
> provided any yet. This means that this plugin will not add message if you are
38+
> amending your commit(`git commit --amend`) or using interactive rebase
39+
> (`git rebase -i HEAD~3`).
440
541
## License
642

7-
This library is licensed under the Apache 2.0 License.
43+
This library is licensed under the Apache 2.0 License.

SETUP.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
For setting up this package you can follow the
2+
[Setup javascript package](https://w.amazon.com/bin/view/AmazonReactToolkit/Community/PackageSetup/#HSetupjavascriptpackage)
3+
documentation. Those are generic javascript package guidelines which we follow
4+
for all our javascript based packages.

bin/git-commit-template.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Script to set our template as commit message template. This depends on husky
5+
* parameters $HUSKY_GIT_PARAMS which gives me the git commit message template
6+
* being used for commit message. By default this is ./git/COMMIT_MSG file
7+
*/
8+
9+
/**
10+
* Here we are using only the first parameter of $HUSKY_GIT_PARAMS variable i.e.
11+
* the commit message file. We will update that file with our template file
12+
* content
13+
*/
14+
15+
/**
16+
* Usage examples:
17+
* Add this script in your hooks section of husky. We require 1.x version of
18+
* husky so this will not work for 0.x version of husky.
19+
*
20+
* Example:
21+
* ```javascript
22+
* // .huskyrc.js
23+
* module.exports = {
24+
* hooks: {
25+
* "prepare-commit-msg": "git-commit-template .gitmessage",
26+
* },
27+
* };
28+
*
29+
* ```
30+
*/
31+
32+
const fs = require("fs");
33+
const path = require("path");
34+
const COMMIT_MSG_FILE_NAME = ".commit-msg";
35+
36+
// Default template file but will be overridden with input argument if provided
37+
let templateFile = path.join(__dirname, `../${COMMIT_MSG_FILE_NAME}`);
38+
39+
/**
40+
* If we have 3rd argument i.e. file name then use that as template
41+
* e.g.
42+
* ./bin/setup-template.js .gitmessage
43+
* // Then argv will look like this:
44+
* argv = [ "node", "./bin/setup-template.js", ".gitmessage" ]
45+
*/
46+
if (process.argv[2]) {
47+
templateFile = path.join(process.cwd(), process.argv[2]);
48+
}
49+
50+
/**
51+
* Sample git params are [ "./git/COMMIT_MSG", "commit", "HEAD" ] in case of
52+
* --amend flag
53+
*/
54+
const gitParams = process.env["HUSKY_GIT_PARAMS"].split(" ");
55+
56+
/**
57+
* We are running our logic only in case of empty commit message. In other
58+
* scenarios commit message would already be present and no longer needs our
59+
* template thus skip it.
60+
*/
61+
if (gitParams.length === 1) {
62+
const commitMsgFile = gitParams[0];
63+
64+
let gitFileContent = fs.readFileSync(commitMsgFile, { encoding: "utf8" });
65+
const templateFileContent = fs.readFileSync(templateFile, {
66+
encoding: "utf8",
67+
});
68+
69+
gitFileContent = templateFileContent + gitFileContent;
70+
71+
fs.writeFileSync(commitMsgFile, gitFileContent, { encoding: "utf8" });
72+
}

lint-staged.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Since this module is set to use es6 thus module is giving error. Thus
2+
// defining module as global before hand
3+
module.exports = {
4+
linters: {
5+
"*.{js,json,md}": [
6+
"npm run lint:files",
7+
// Assuming prettier doesn't add any linting issues
8+
"prettier --write",
9+
// Checking if anything broke because of prettier
10+
"npm run lint:files",
11+
"git add",
12+
],
13+
},
14+
};

0 commit comments

Comments
 (0)