Skip to content

Commit bd50ff9

Browse files
committed
refactor(utils): pull out helper function for stripping arguments
1 parent 7bec6c5 commit bd50ff9

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/scripts/pre-commit.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ const os = require('os')
44
const spawn = require('cross-spawn')
55
const yargsParser = require('yargs-parser')
66
const {
7-
hasPkgProp,
7+
getPkgName,
88
hasFile,
9+
hasPkgProp,
910
ifScript,
1011
relative,
1112
resolveBin,
12-
getPkgName,
13+
stripArgument,
1314
} = require('../utils')
1415
const {buildConfig} = require('../config/helpers/build-lint-staged')
1516

1617
const hereRelative = relative(__dirname)
1718

1819
const args = process.argv.slice(2)
19-
const {argv: parsedArgs, aliases} = yargsParser.detailed(args)
20+
const {argv: parsedArgs} = yargsParser.detailed(args)
2021

2122
/**
2223
* Generate a temporary copy of the built-in lint-staged
@@ -47,15 +48,11 @@ if (parsedArgs.config && parsedArgs.testCommand) {
4748

4849
// Don't forward `--testCommand` or `--test-command`
4950
// flags through to `lint-staged` (yes, this is gross)
50-
const testCommandIndex = args.findIndex(
51-
a =>
52-
a === '--testCommand' ||
53-
(aliases.testCommand && aliases.testCommand.includes(a.replace(/^--/, ''))),
51+
const argsToForward = stripArgument(
52+
args,
53+
['--test-command', '--testCommand'],
54+
2,
5455
)
55-
const argsToForward = [...args]
56-
if (testCommandIndex >= 0) {
57-
argsToForward.splice(testCommandIndex, 2)
58-
}
5956

6057
const useCustomBuiltInConfig = !!parsedArgs.testCommand
6158
const customBuiltInConfig = useCustomBuiltInConfig

src/utils.js

+20
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ function hasLocalConfig(moduleName, searchOptions = {}) {
162162
return result !== null
163163
}
164164

165+
/**
166+
* Strip an argument and it's values from arguments forwarded to lint-staged
167+
*
168+
* @param {any[]} from arguments to strip argument(s) from, e.g: `process.argv`
169+
* @param {string[]} argument array of argument aliases, e.g: `['--foo-bar', '--fooBar']
170+
* @param {number} length number arguments to strip, i.e: `--arg value` = 2
171+
*/
172+
const stripArgument = (from, argument, length = 1) => {
173+
const index = from.findIndex(a => argument.includes(a))
174+
175+
if (index < 0) return from
176+
177+
const stripped = [...from]
178+
179+
stripped.splice(index, length)
180+
181+
return stripped
182+
}
183+
165184
/**
166185
* Get function that converts relative paths to absolute
167186
*
@@ -197,6 +216,7 @@ module.exports = {
197216
relative,
198217
resolveBin,
199218
resolveHoverScripts,
219+
stripArgument,
200220
uniq,
201221
writeExtraEntry,
202222
}

0 commit comments

Comments
 (0)