Skip to content

Commit cba9997

Browse files
committed
fix: pass correct arguments to sub-script
Update argument forwarding logic in run-script wrapper to dynamically find sub-script command and therefore pass correct arguments to script withou hard-coding the script names. I was not aware of this hard-coded bit and therefore some scripts were receiving the sub-script command erroneously.
1 parent df5f7d7 commit cba9997

File tree

4 files changed

+12
-50
lines changed

4 files changed

+12
-50
lines changed

src/run-script.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
const fs = require('fs')
12
const path = require('path')
2-
const spawn = require('cross-spawn')
33
const glob = require('glob')
4+
const spawn = require('cross-spawn')
45

56
const [executor, ignoredBin, script] = process.argv
67

@@ -55,15 +56,14 @@ function getEnv() {
5556
function spawnScript() {
5657
// get all the arguments of the script and find the position of our script commands
5758
const args = process.argv.slice(2)
58-
const scriptIndex = args.findIndex(
59-
x =>
60-
x === 'format' ||
61-
x === 'lint' ||
62-
x === 'pre-commit' ||
63-
x === 'test' ||
64-
x === 'validate' ||
65-
x === 'build',
66-
)
59+
const scriptsPath = path.join(__dirname, './scripts')
60+
61+
const scripts = fs
62+
.readdirSync(scriptsPath)
63+
.filter(f => fs.statSync(path.join(scriptsPath, f)).isFile())
64+
.map(f => f.replace(/\.js$/, ''))
65+
66+
const scriptIndex = args.findIndex(x => scripts.includes(x))
6767

6868
// Extract the node arguments so we can pass them to node later on
6969
const buildCommand = scriptIndex === -1 ? args[0] : args[scriptIndex]
@@ -73,7 +73,7 @@ function spawnScript() {
7373
throw new Error(`Unknown script "${script}".`)
7474
}
7575

76-
const relativeScriptPath = path.join(__dirname, './scripts', buildCommand)
76+
const relativeScriptPath = path.join(scriptsPath, buildCommand)
7777
const scriptPath = attemptResolve(relativeScriptPath)
7878
if (!scriptPath) {
7979
throw new Error(`Unknown script "${script}".`)

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

-22
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,3 @@ Object {
2121
`;
2222
2323
exports[`commit forwards arguments 2`] = `node ../commit --retry`;
24-
25-
exports[`commit strips errant "commit" argument 1`] = `
26-
Object {
27-
cliPath: <PROJECT_ROOT>/node_modules/commitizen,
28-
config: Object {
29-
path: @commitlint/prompt,
30-
},
31-
}
32-
`;
33-
34-
exports[`commit strips errant "commit" argument 2`] = `node ../commit`;
35-
36-
exports[`commit strips errant "commit" argument and forwards arguments 1`] = `
37-
Object {
38-
cliPath: <PROJECT_ROOT>/node_modules/commitizen,
39-
config: Object {
40-
path: @commitlint/prompt,
41-
},
42-
}
43-
`;
44-
45-
exports[`commit strips errant "commit" argument and forwards arguments 2`] = `node ../commit --retry`;

src/scripts/__tests__/commit.js

-6
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,8 @@ cases(
4949
},
5050
{
5151
'bootstraps @commitlint/prompt': {},
52-
'strips errant "commit" argument': {
53-
args: ['commit'],
54-
},
5552
'forwards arguments': {
5653
args: ['--retry'],
5754
},
58-
'strips errant "commit" argument and forwards arguments': {
59-
args: ['commit', '--retry'],
60-
},
6155
},
6256
)

src/scripts/commit.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@ const {bootstrap} = require('commitizen/dist/cli/git-cz')
33
const commitizenPaths = require.resolve('commitizen/package.json').split('/')
44
const cliPath = commitizenPaths.slice(0, -1).join('/')
55

6-
// eslint-disable-next-line no-warning-comments
7-
// FIXME: for some reason invoking this script with `yarn commit` now passes
8-
// through `'commit'` in the arguments that get parsed as "raw git arguments"
9-
// in the adapter which blows up said parsing behavior. I'm not sure if this
10-
// was related to a yarn change (`yarn [script] args` vs. `yarn [script] --
11-
// args`) or something else...
12-
//
13-
// Either way, this is a band-aid to filter out the rogue argument for now.
14-
const args = process.argv.filter(arg => arg !== 'commit')
15-
166
bootstrap(
177
{
188
cliPath,
199
config: {
2010
path: '@commitlint/prompt',
2111
},
2212
},
23-
args,
13+
process.argv,
2414
)

0 commit comments

Comments
 (0)