Skip to content

Commit 734f9ba

Browse files
committed
feat(scripts/pre-commit): add support for --no-toc argument
Add support to `hover-scripts pre-commit` exclude the DocToc generation via `--no-toc`
1 parent d4cfa79 commit 734f9ba

File tree

8 files changed

+72
-32
lines changed

8 files changed

+72
-32
lines changed

lint-staged.js

-1
This file was deleted.

lint-staged/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../dist/config/lintstagedrc')

lint-staged/no-toc.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../dist/config/lintstagedrc-no-toc')

src/config/helpers/build-lint-staged.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const formatGlob =
1818
const lintGlob = `*.+(${sourceExtensions.join('|')})`
1919
const testGlob = `*.+(${sourceExtensions.reverse().join('|')})`
2020

21-
const buildConfig = (testCommand = defaultTestCommand) => ({
22-
[readmeGlob]: [`${doctoc} --maxlevel 4 --notitle`],
21+
const buildConfig = (toc = true, testCommand = defaultTestCommand) => ({
22+
...(toc ? {[readmeGlob]: [`${doctoc} --maxlevel 4 --notitle`]} : {}),
2323
[formatGlob]: [`${hoverScripts} format`],
2424
[lintGlob]: [`${hoverScripts} lint`],
2525
[testGlob]: [testCommand],

src/config/lintstagedrc-no-toc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const {buildConfig} = require('./helpers/build-lint-staged')
2+
3+
module.exports = buildConfig(false)

src/scripts/__tests__/__snapshots__/pre-commit.js.snap

+28-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ Array [
77
]
88
`;
99

10+
exports[`pre-commit disables DocToc and forwards args 1`] = `
11+
Array [
12+
lint-staged --config .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json --verbose --some-other-arg,
13+
npm run validate,
14+
]
15+
`;
16+
17+
exports[`pre-commit disables DocToc and forwards args 2`] = `
18+
Array [
19+
.test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json,
20+
{"*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx|vue)":["./src/index.js format"],"*.+(js|jsx|ts|tsx)":["./src/index.js lint"],"*.+(tsx|ts|jsx|js)":["./src/index.js test --findRelatedTests"]},
21+
]
22+
`;
23+
24+
exports[`pre-commit disables DocToc, overrides built-in test command, and forwards args 1`] = `
25+
Array [
26+
lint-staged --config .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json --verbose --some-other-arg,
27+
npm run validate,
28+
]
29+
`;
30+
31+
exports[`pre-commit disables DocToc, overrides built-in test command, and forwards args 2`] = `
32+
Array [
33+
.test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json,
34+
{"*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx|vue)":["./src/index.js format"],"*.+(js|jsx|ts|tsx)":["./src/index.js lint"],"*.+(tsx|ts|jsx|js)":["yarn test:custom --findRelatedTests foo.js"]},
35+
]
36+
`;
37+
1038
exports[`pre-commit does not run validate script if it's not defined 1`] = `
1139
Array [
1240
lint-staged --config ./src/config/lintstagedrc.js,
@@ -83,27 +111,13 @@ Array [
83111
]
84112
`;
85113

86-
exports[`pre-commit overrides built-in test command with --testCommand 2`] = `
87-
Array [
88-
.test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json,
89-
{"README.md":["doctoc --maxlevel 4 --notitle"],"*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx|vue)":["./src/index.js format"],"*.+(js|jsx|ts|tsx)":["./src/index.js lint"],"*.+(tsx|ts|jsx|js)":["yarn test:custom --findRelatedTests foo.js"]},
90-
]
91-
`;
92-
93114
exports[`pre-commit overrides built-in test command with --testCommand and forwards args 1`] = `
94115
Array [
95116
lint-staged --config .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json --verbose,
96117
npm run validate,
97118
]
98119
`;
99120

100-
exports[`pre-commit overrides built-in test command with --testCommand and forwards args 2`] = `
101-
Array [
102-
.test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json,
103-
{"README.md":["doctoc --maxlevel 4 --notitle"],"*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx|vue)":["./src/index.js format"],"*.+(js|jsx|ts|tsx)":["./src/index.js lint"],"*.+(tsx|ts|jsx|js)":["yarn test:custom --findRelatedTests foo.js"]},
104-
]
105-
`;
106-
107121
exports[`pre-commit throws an error when \`--config\` and \`--testCommand\` are used together 1`] = `[Error: @hover/javascript/pre-commit: --config and --testCommand cannot be used together (--testCommand only works with built-in lint-staged configuration)]`;
108122

109123
exports[`pre-commit throws an error when invalid \`--testCommand\` is provided 1`] = `[Error: @hover/javascript/pre-commit: invalid --testCommand]`;

src/scripts/__tests__/pre-commit.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from 'path'
22
import cases from 'jest-in-case'
3-
import yargsParser from 'yargs-parser'
43
import {unquoteSerializer, winPathSerializer} from './helpers/serializers'
54

65
expect.addSnapshotSerializer(unquoteSerializer)
@@ -55,13 +54,19 @@ cases(
5554
)
5655
expect(commands).toMatchSnapshot()
5756

58-
// Specific tests for when a custom test command is supplied
59-
if (!!yargsParser(args).testCommand) {
60-
// ensure we don't pass `--testCommand` through to `lint-staged`
57+
// ↓ ⚠️ Specific tests for when a custom test ↓
58+
// ↓ command is supplied or DocToc is disabled ↓
59+
60+
if (args.includes('--no-toc') || args.includes('--test-command')) {
61+
// ensure we don't pass invalid arguments through to `lint-staged`
6162
expect(
6263
crossSpawnSyncMock.mock.calls.some(([_command, commandArgs]) =>
6364
commandArgs.some(
64-
a => a === '--testCommand' || a === '--test-command',
65+
a =>
66+
a === '--testCommand' ||
67+
a === '--test-command' ||
68+
a === '--no-toc' ||
69+
a === '--noToc',
6570
),
6671
),
6772
).not.toBeTruthy()
@@ -77,6 +82,9 @@ cases(
7782
{recursive: true},
7883
)
7984
}
85+
86+
// ↑ ⚠️ Specific tests for when a custom test ↑
87+
// ↑ command is supplied or DocToc is disabled ↑
8088
} catch (error) {
8189
if (expectError) {
8290
expect(error).toMatchSnapshot()
@@ -146,5 +154,17 @@ cases(
146154
'"yarn test:custom --findRelatedTests foo.js"',
147155
],
148156
},
157+
'disables DocToc, overrides built-in test command, and forwards args': {
158+
args: [
159+
'--verbose',
160+
'--no-toc',
161+
'--test-command',
162+
'"yarn test:custom --findRelatedTests foo.js"',
163+
'--some-other-arg',
164+
],
165+
},
166+
'disables DocToc and forwards args': {
167+
args: ['--verbose', '--no-toc', '--some-other-arg'],
168+
},
149169
},
150170
)

src/scripts/pre-commit.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ const {argv: parsedArgs} = yargsParser.detailed(args)
2323
* Generate a temporary copy of the built-in lint-staged
2424
* configuration with a custom test command
2525
*
26-
* @param {string} command
26+
* @param {boolean} toc include DocToc
27+
* @param {string} command custom test command
2728
*
2829
* @returns {string} filename of generated config file
2930
*/
30-
const generateConfigWithTestCommand = command => {
31+
const generateConfigWithTestCommand = (toc, command) => {
3132
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'hover-javascript'))
3233
const tmpConfigFile = path.join(tmpDir, '.lintstaged.json')
3334

34-
fs.writeFileSync(tmpConfigFile, JSON.stringify(buildConfig(command)))
35+
fs.writeFileSync(tmpConfigFile, JSON.stringify(buildConfig(toc, command)))
3536

3637
return tmpConfigFile
3738
}
@@ -46,17 +47,18 @@ if (parsedArgs.config && parsedArgs.testCommand) {
4647
)
4748
}
4849

49-
// Don't forward `--testCommand` or `--test-command`
50-
// flags through to `lint-staged` (yes, this is gross)
50+
// Don't pass `--no-toc` or `--test-command [command]` to lint-staged
5151
const argsToForward = stripArgument(
52-
args,
53-
['--test-command', '--testCommand'],
54-
2,
52+
stripArgument(args, ['--test-command', '--testCommand'], 2),
53+
['--no-toc', '--noToc'],
5554
)
5655

57-
const useCustomBuiltInConfig = !!parsedArgs.testCommand
56+
// Use temporary configuration if `--no-toc` or `--test-command [command]` is passed
57+
const useCustomBuiltInConfig =
58+
!!parsedArgs.testCommand || parsedArgs.toc === false
59+
5860
const customBuiltInConfig = useCustomBuiltInConfig
59-
? generateConfigWithTestCommand(parsedArgs.testCommand)
61+
? generateConfigWithTestCommand(parsedArgs.toc, parsedArgs.testCommand)
6062
: null
6163

6264
const useBuiltInConfig =

0 commit comments

Comments
 (0)