From 734f9ba516c36aa9b77594c73f4a4bab5ea3088e Mon Sep 17 00:00:00 2001 From: Jamie Rolfs Date: Sun, 18 Jul 2021 00:35:05 -0700 Subject: [PATCH] feat(scripts/pre-commit): add support for `--no-toc` argument Add support to `hover-scripts pre-commit` exclude the DocToc generation via `--no-toc` --- lint-staged.js | 1 - lint-staged/index.js | 1 + lint-staged/no-toc.js | 1 + src/config/helpers/build-lint-staged.js | 4 +- src/config/lintstagedrc-no-toc.js | 3 ++ .../__snapshots__/pre-commit.js.snap | 42 ++++++++++++------- src/scripts/__tests__/pre-commit.js | 30 ++++++++++--- src/scripts/pre-commit.js | 22 +++++----- 8 files changed, 72 insertions(+), 32 deletions(-) delete mode 100644 lint-staged.js create mode 100644 lint-staged/index.js create mode 100644 lint-staged/no-toc.js create mode 100644 src/config/lintstagedrc-no-toc.js diff --git a/lint-staged.js b/lint-staged.js deleted file mode 100644 index cbcef68d..00000000 --- a/lint-staged.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./dist/config/lintstagedrc') diff --git a/lint-staged/index.js b/lint-staged/index.js new file mode 100644 index 00000000..cd9bded7 --- /dev/null +++ b/lint-staged/index.js @@ -0,0 +1 @@ +module.exports = require('../dist/config/lintstagedrc') diff --git a/lint-staged/no-toc.js b/lint-staged/no-toc.js new file mode 100644 index 00000000..b69feaa5 --- /dev/null +++ b/lint-staged/no-toc.js @@ -0,0 +1 @@ +module.exports = require('../dist/config/lintstagedrc-no-toc') diff --git a/src/config/helpers/build-lint-staged.js b/src/config/helpers/build-lint-staged.js index 20009ece..30530060 100644 --- a/src/config/helpers/build-lint-staged.js +++ b/src/config/helpers/build-lint-staged.js @@ -18,8 +18,8 @@ const formatGlob = const lintGlob = `*.+(${sourceExtensions.join('|')})` const testGlob = `*.+(${sourceExtensions.reverse().join('|')})` -const buildConfig = (testCommand = defaultTestCommand) => ({ - [readmeGlob]: [`${doctoc} --maxlevel 4 --notitle`], +const buildConfig = (toc = true, testCommand = defaultTestCommand) => ({ + ...(toc ? {[readmeGlob]: [`${doctoc} --maxlevel 4 --notitle`]} : {}), [formatGlob]: [`${hoverScripts} format`], [lintGlob]: [`${hoverScripts} lint`], [testGlob]: [testCommand], diff --git a/src/config/lintstagedrc-no-toc.js b/src/config/lintstagedrc-no-toc.js new file mode 100644 index 00000000..60799687 --- /dev/null +++ b/src/config/lintstagedrc-no-toc.js @@ -0,0 +1,3 @@ +const {buildConfig} = require('./helpers/build-lint-staged') + +module.exports = buildConfig(false) diff --git a/src/scripts/__tests__/__snapshots__/pre-commit.js.snap b/src/scripts/__tests__/__snapshots__/pre-commit.js.snap index d5bf0898..ea85de97 100644 --- a/src/scripts/__tests__/__snapshots__/pre-commit.js.snap +++ b/src/scripts/__tests__/__snapshots__/pre-commit.js.snap @@ -7,6 +7,34 @@ Array [ ] `; +exports[`pre-commit disables DocToc and forwards args 1`] = ` +Array [ + lint-staged --config .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json --verbose --some-other-arg, + npm run validate, +] +`; + +exports[`pre-commit disables DocToc and forwards args 2`] = ` +Array [ + .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json, + {"*.+(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"]}, +] +`; + +exports[`pre-commit disables DocToc, overrides built-in test command, and forwards args 1`] = ` +Array [ + lint-staged --config .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json --verbose --some-other-arg, + npm run validate, +] +`; + +exports[`pre-commit disables DocToc, overrides built-in test command, and forwards args 2`] = ` +Array [ + .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json, + {"*.+(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"]}, +] +`; + exports[`pre-commit does not run validate script if it's not defined 1`] = ` Array [ lint-staged --config ./src/config/lintstagedrc.js, @@ -83,13 +111,6 @@ Array [ ] `; -exports[`pre-commit overrides built-in test command with --testCommand 2`] = ` -Array [ - .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json, - {"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"]}, -] -`; - exports[`pre-commit overrides built-in test command with --testCommand and forwards args 1`] = ` Array [ lint-staged --config .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json --verbose, @@ -97,13 +118,6 @@ Array [ ] `; -exports[`pre-commit overrides built-in test command with --testCommand and forwards args 2`] = ` -Array [ - .test-tmp/hover-javascriptTMPSUFFIX/.lintstaged.json, - {"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"]}, -] -`; - 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)]`; exports[`pre-commit throws an error when invalid \`--testCommand\` is provided 1`] = `[Error: @hover/javascript/pre-commit: invalid --testCommand]`; diff --git a/src/scripts/__tests__/pre-commit.js b/src/scripts/__tests__/pre-commit.js index b7fbb6e4..b2c40154 100644 --- a/src/scripts/__tests__/pre-commit.js +++ b/src/scripts/__tests__/pre-commit.js @@ -1,6 +1,5 @@ import path from 'path' import cases from 'jest-in-case' -import yargsParser from 'yargs-parser' import {unquoteSerializer, winPathSerializer} from './helpers/serializers' expect.addSnapshotSerializer(unquoteSerializer) @@ -55,13 +54,19 @@ cases( ) expect(commands).toMatchSnapshot() - // Specific tests for when a custom test command is supplied - if (!!yargsParser(args).testCommand) { - // ensure we don't pass `--testCommand` through to `lint-staged` + // ↓ ⚠️ Specific tests for when a custom test ↓ + // ↓ command is supplied or DocToc is disabled ↓ + + if (args.includes('--no-toc') || args.includes('--test-command')) { + // ensure we don't pass invalid arguments through to `lint-staged` expect( crossSpawnSyncMock.mock.calls.some(([_command, commandArgs]) => commandArgs.some( - a => a === '--testCommand' || a === '--test-command', + a => + a === '--testCommand' || + a === '--test-command' || + a === '--no-toc' || + a === '--noToc', ), ), ).not.toBeTruthy() @@ -77,6 +82,9 @@ cases( {recursive: true}, ) } + + // ↑ ⚠️ Specific tests for when a custom test ↑ + // ↑ command is supplied or DocToc is disabled ↑ } catch (error) { if (expectError) { expect(error).toMatchSnapshot() @@ -146,5 +154,17 @@ cases( '"yarn test:custom --findRelatedTests foo.js"', ], }, + 'disables DocToc, overrides built-in test command, and forwards args': { + args: [ + '--verbose', + '--no-toc', + '--test-command', + '"yarn test:custom --findRelatedTests foo.js"', + '--some-other-arg', + ], + }, + 'disables DocToc and forwards args': { + args: ['--verbose', '--no-toc', '--some-other-arg'], + }, }, ) diff --git a/src/scripts/pre-commit.js b/src/scripts/pre-commit.js index 50a59e6c..5ac99141 100644 --- a/src/scripts/pre-commit.js +++ b/src/scripts/pre-commit.js @@ -23,15 +23,16 @@ const {argv: parsedArgs} = yargsParser.detailed(args) * Generate a temporary copy of the built-in lint-staged * configuration with a custom test command * - * @param {string} command + * @param {boolean} toc include DocToc + * @param {string} command custom test command * * @returns {string} filename of generated config file */ -const generateConfigWithTestCommand = command => { +const generateConfigWithTestCommand = (toc, command) => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'hover-javascript')) const tmpConfigFile = path.join(tmpDir, '.lintstaged.json') - fs.writeFileSync(tmpConfigFile, JSON.stringify(buildConfig(command))) + fs.writeFileSync(tmpConfigFile, JSON.stringify(buildConfig(toc, command))) return tmpConfigFile } @@ -46,17 +47,18 @@ if (parsedArgs.config && parsedArgs.testCommand) { ) } -// Don't forward `--testCommand` or `--test-command` -// flags through to `lint-staged` (yes, this is gross) +// Don't pass `--no-toc` or `--test-command [command]` to lint-staged const argsToForward = stripArgument( - args, - ['--test-command', '--testCommand'], - 2, + stripArgument(args, ['--test-command', '--testCommand'], 2), + ['--no-toc', '--noToc'], ) -const useCustomBuiltInConfig = !!parsedArgs.testCommand +// Use temporary configuration if `--no-toc` or `--test-command [command]` is passed +const useCustomBuiltInConfig = + !!parsedArgs.testCommand || parsedArgs.toc === false + const customBuiltInConfig = useCustomBuiltInConfig - ? generateConfigWithTestCommand(parsedArgs.testCommand) + ? generateConfigWithTestCommand(parsedArgs.toc, parsedArgs.testCommand) : null const useBuiltInConfig =