Skip to content

Commit 893f3d7

Browse files
committedJan 7, 2022
fix: always run non-git tasks in the current working directory
1 parent c7ea359 commit 893f3d7

5 files changed

+19
-26
lines changed
 

‎lib/makeCmdTasks.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ const getTitleLength = (renderer, columns = process.stdout.columns) => {
2828
*
2929
* @param {object} options
3030
* @param {Array<string|Function>|string|Function} options.commands
31+
* @param {string} options.cwd
3132
* @param {Array<string>} options.files
3233
* @param {string} options.gitDir
3334
* @param {string} options.renderer
3435
* @param {Boolean} shell
3536
* @param {Boolean} verbose
3637
*/
37-
export const makeCmdTasks = async ({ commands, files, gitDir, renderer, shell, verbose }) => {
38+
export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, shell, verbose }) => {
3839
debugLog('Creating listr tasks for commands %o', commands)
3940
const commandArray = Array.isArray(commands) ? commands : [commands]
4041
const cmdTasks = []
@@ -61,7 +62,7 @@ export const makeCmdTasks = async ({ commands, files, gitDir, renderer, shell, v
6162

6263
// Truncate title to single line based on renderer
6364
const title = cliTruncate(command, getTitleLength(renderer))
64-
const task = resolveTaskFn({ command, files, gitDir, isFn, shell, verbose })
65+
const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose })
6566
cmdTasks.push({ title, command, task })
6667
}
6768
}

‎lib/resolveTaskFn.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,36 @@ const makeErr = (command, result, ctx) => {
6868
*
6969
* @param {Object} options
7070
* @param {string} options.command — Linter task
71+
* @param {string} [options.cwd]
7172
* @param {String} options.gitDir - Current git repo path
7273
* @param {Boolean} options.isFn - Whether the linter task is a function
7374
* @param {Array<string>} options.files — Filepaths to run the linter task against
74-
* @param {Boolean} [options.relative] — Whether the filepaths should be relative
7575
* @param {Boolean} [options.shell] — Whether to skip parsing linter task for better shell support
7676
* @param {Boolean} [options.verbose] — Always show task verbose
7777
* @returns {function(): Promise<Array<string>>}
7878
*/
7979
export const resolveTaskFn = ({
8080
command,
81+
cwd = process.cwd(),
8182
files,
8283
gitDir,
8384
isFn,
84-
relative,
8585
shell = false,
8686
verbose = false,
8787
}) => {
8888
const [cmd, ...args] = parseArgsStringToArgv(command)
8989
debugLog('cmd:', cmd)
9090
debugLog('args:', args)
9191

92-
const execaOptions = { preferLocal: true, reject: false, shell }
93-
if (relative) {
94-
execaOptions.cwd = process.cwd()
95-
} else if (/^git(\.exe)?/i.test(cmd) && gitDir !== process.cwd()) {
92+
const execaOptions = {
9693
// Only use gitDir as CWD if we are using the git binary
9794
// e.g `npm` should run tasks in the actual CWD
98-
execaOptions.cwd = gitDir
95+
cwd: /^git(\.exe)?/i.test(cmd) ? gitDir : cwd,
96+
preferLocal: true,
97+
reject: false,
98+
shell,
9999
}
100+
100101
debugLog('execaOptions:', execaOptions)
101102

102103
return async (ctx = getInitialState()) => {

‎lib/runAll.js

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export const runAll = async (
135135
for (const task of chunkTasks) {
136136
const subTasks = await makeCmdTasks({
137137
commands: task.commands,
138+
cwd,
138139
files: task.fileList,
139140
gitDir,
140141
renderer: listrOptions.renderer,

‎test/makeCmdTasks.spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('makeCmdTasks', () => {
4343
await taskPromise
4444
expect(execa).toHaveBeenCalledTimes(1)
4545
expect(execa).lastCalledWith('test', ['test.js'], {
46+
cwd: process.cwd(),
4647
preferLocal: true,
4748
reject: false,
4849
shell: false,
@@ -52,6 +53,7 @@ describe('makeCmdTasks', () => {
5253
await taskPromise
5354
expect(execa).toHaveBeenCalledTimes(2)
5455
expect(execa).lastCalledWith('test2', ['test.js'], {
56+
cwd: process.cwd(),
5557
preferLocal: true,
5658
reject: false,
5759
shell: false,

‎test/resolveTaskFn.spec.js

+5-17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('resolveTaskFn', () => {
2121
await taskFn()
2222
expect(execa).toHaveBeenCalledTimes(1)
2323
expect(execa).lastCalledWith('node', ['--arg=true', './myscript.js', 'test.js'], {
24+
cwd: process.cwd(),
2425
preferLocal: true,
2526
reject: false,
2627
shell: false,
@@ -38,6 +39,7 @@ describe('resolveTaskFn', () => {
3839
await taskFn()
3940
expect(execa).toHaveBeenCalledTimes(1)
4041
expect(execa).lastCalledWith('node', ['--arg=true', './myscript.js', 'test.js'], {
42+
cwd: process.cwd(),
4143
preferLocal: true,
4244
reject: false,
4345
shell: false,
@@ -56,6 +58,7 @@ describe('resolveTaskFn', () => {
5658
await taskFn()
5759
expect(execa).toHaveBeenCalledTimes(1)
5860
expect(execa).lastCalledWith('node --arg=true ./myscript.js test.js', {
61+
cwd: process.cwd(),
5962
preferLocal: true,
6063
reject: false,
6164
shell: true,
@@ -73,6 +76,7 @@ describe('resolveTaskFn', () => {
7376
await taskFn()
7477
expect(execa).toHaveBeenCalledTimes(1)
7578
expect(execa).lastCalledWith('node --arg=true ./myscript.js test.js', {
79+
cwd: process.cwd(),
7680
preferLocal: true,
7781
reject: false,
7882
shell: true,
@@ -90,6 +94,7 @@ describe('resolveTaskFn', () => {
9094
await taskFn()
9195
expect(execa).toHaveBeenCalledTimes(1)
9296
expect(execa).lastCalledWith('node --arg=true ./myscript.js test.js', {
97+
cwd: process.cwd(),
9398
preferLocal: true,
9499
reject: false,
95100
shell: '/bin/bash',
@@ -121,23 +126,6 @@ describe('resolveTaskFn', () => {
121126
await taskFn()
122127
expect(execa).toHaveBeenCalledTimes(1)
123128
expect(execa).lastCalledWith('jest', ['test.js'], {
124-
preferLocal: true,
125-
reject: false,
126-
shell: false,
127-
})
128-
})
129-
130-
it('should always pass `process.cwd()` as `cwd` to `execa()` when relative = true', async () => {
131-
expect.assertions(2)
132-
const taskFn = resolveTaskFn({
133-
...defaultOpts,
134-
command: 'git diff',
135-
relative: true,
136-
})
137-
138-
await taskFn()
139-
expect(execa).toHaveBeenCalledTimes(1)
140-
expect(execa).lastCalledWith('git', ['diff', 'test.js'], {
141129
cwd: process.cwd(),
142130
preferLocal: true,
143131
reject: false,

0 commit comments

Comments
 (0)
Please sign in to comment.