Skip to content

Commit 77c8494

Browse files
committed
feat: add commit-msg script
1 parent 2df3f35 commit 77c8494

File tree

6 files changed

+513
-37
lines changed

6 files changed

+513
-37
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
},
2121
"husky": {
2222
"hooks": {
23-
"pre-commit": "node src pre-commit"
23+
"pre-commit": "node src pre-commit",
24+
"commit-msg": "node src commit-msg"
2425
}
2526
},
2627
"files": [
@@ -47,6 +48,7 @@
4748
"@babel/preset-flow": "^7.8.3",
4849
"@babel/preset-react": "^7.8.3",
4950
"@babel/runtime": "^7.8.3",
51+
"@commitlint/cli": "^8.3.5",
5052
"@commitlint/config-conventional": "^8.3.4",
5153
"@commitlint/prompt": "^8.3.5",
5254
"@typescript-eslint/eslint-plugin": "^2.16.0",

src/__tests__/__snapshots__/index.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Usage: ../ [script] [--flags]
2929
Available Scripts:
3030
build
3131
ci-after-success
32+
commit-msg
3233
commit
3334
format
3435
lint
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`commit-msg adds env flag with HUSKY_GIT_PARAMS when available 1`] = `commitlint --env HUSKY_GIT_PARAMS --config ./src/config/commitlint.config.js`;
4+
5+
exports[`commit-msg calls @commitlint/cli with default args 1`] = `commitlint --config ./src/config/commitlint.config.js`;
6+
7+
exports[`commit-msg does not use built-in config with --config 1`] = `commitlint`;
8+
9+
exports[`commit-msg does not use built-in config with commitlint.config.js file 1`] = `commitlint`;
10+
11+
exports[`commit-msg forwards args 1`] = `commitlint --config ./src/config/commitlint.config.js`;

src/scripts/__tests__/commit-msg.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import cases from 'jest-in-case'
2+
import {unquoteSerializer, winPathSerializer} from './helpers/serializers'
3+
4+
expect.addSnapshotSerializer(unquoteSerializer)
5+
expect.addSnapshotSerializer(winPathSerializer)
6+
7+
cases(
8+
'commit-msg',
9+
({
10+
args = [],
11+
utils = require('../../utils'),
12+
hasFile = () => false,
13+
env = {},
14+
}) => {
15+
// beforeEach
16+
const {sync: crossSpawnSyncMock} = require('cross-spawn')
17+
const originalArgv = process.argv
18+
const originalExit = process.exit
19+
const originalEnv = process.env
20+
21+
Object.assign(utils, {
22+
hasFile,
23+
resolveBin: (modName, {executable = modName} = {}) => executable,
24+
})
25+
26+
process.exit = jest.fn()
27+
process.argv = ['node', '../commit-msg', ...args]
28+
process.env = env
29+
30+
try {
31+
// tests
32+
require('../commit-msg')
33+
34+
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
35+
36+
const [call] = crossSpawnSyncMock.mock.calls
37+
const [script, calledArgs] = call
38+
39+
expect([script, ...calledArgs].join(' ')).toMatchSnapshot()
40+
} catch (error) {
41+
throw error
42+
} finally {
43+
// afterEach
44+
process.exit = originalExit
45+
process.argv = originalArgv
46+
process.env = originalEnv
47+
48+
jest.resetModules()
49+
}
50+
},
51+
{
52+
'calls @commitlint/cli with default args': {},
53+
'does not use built-in config with --config': {
54+
args: ['--config', './custom-config.js'],
55+
},
56+
'does not use built-in config with commitlint.config.js file': {
57+
hasFile: filename => filename === 'commitlint.config.js',
58+
},
59+
'forwards args': {
60+
args: ['--verbose'],
61+
},
62+
'adds env flag with HUSKY_GIT_PARAMS when available': {
63+
env: {HUSKY_GIT_PARAMS: 'husky-git-params'},
64+
},
65+
},
66+
)

src/scripts/commit-msg.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const path = require('path')
2+
const spawn = require('cross-spawn')
3+
const {hasFile, resolveBin} = require('../utils')
4+
5+
const args = process.argv.slice(2)
6+
const huskyGitParams = process.env.HUSKY_GIT_PARAMS
7+
8+
const here = p => path.join(__dirname, p)
9+
const hereRelative = p => here(p).replace(process.cwd(), '.')
10+
11+
const useBuiltinConfig =
12+
!args.includes('--config') &&
13+
!args.includes('-g') &&
14+
!hasFile('commitlint.config.js')
15+
16+
const env = huskyGitParams ? ['--env', 'HUSKY_GIT_PARAMS'] : []
17+
18+
const config = useBuiltinConfig
19+
? ['--config', hereRelative('../config/commitlint.config.js')]
20+
: []
21+
22+
const result = spawn.sync(
23+
resolveBin('@commitlint/cli', {executable: 'commitlint'}),
24+
[...env, ...config],
25+
{
26+
stdio: 'inherit',
27+
},
28+
)
29+
30+
process.exit(result.status)

0 commit comments

Comments
 (0)