From 893f3d7825f73115a41ddb3be34af15f4c207315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Thu, 6 Jan 2022 15:57:13 +0200 Subject: [PATCH] fix: always run non-git tasks in the current working directory --- lib/makeCmdTasks.js | 5 +++-- lib/resolveTaskFn.js | 15 ++++++++------- lib/runAll.js | 1 + test/makeCmdTasks.spec.js | 2 ++ test/resolveTaskFn.spec.js | 22 +++++----------------- 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/makeCmdTasks.js b/lib/makeCmdTasks.js index 8bac8f7a6..1de2b9251 100644 --- a/lib/makeCmdTasks.js +++ b/lib/makeCmdTasks.js @@ -28,13 +28,14 @@ const getTitleLength = (renderer, columns = process.stdout.columns) => { * * @param {object} options * @param {Array|string|Function} options.commands + * @param {string} options.cwd * @param {Array} options.files * @param {string} options.gitDir * @param {string} options.renderer * @param {Boolean} shell * @param {Boolean} verbose */ -export const makeCmdTasks = async ({ commands, files, gitDir, renderer, shell, verbose }) => { +export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, shell, verbose }) => { debugLog('Creating listr tasks for commands %o', commands) const commandArray = Array.isArray(commands) ? commands : [commands] const cmdTasks = [] @@ -61,7 +62,7 @@ export const makeCmdTasks = async ({ commands, files, gitDir, renderer, shell, v // Truncate title to single line based on renderer const title = cliTruncate(command, getTitleLength(renderer)) - const task = resolveTaskFn({ command, files, gitDir, isFn, shell, verbose }) + const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose }) cmdTasks.push({ title, command, task }) } } diff --git a/lib/resolveTaskFn.js b/lib/resolveTaskFn.js index a0af0262f..f39e6017a 100644 --- a/lib/resolveTaskFn.js +++ b/lib/resolveTaskFn.js @@ -68,20 +68,20 @@ const makeErr = (command, result, ctx) => { * * @param {Object} options * @param {string} options.command — Linter task + * @param {string} [options.cwd] * @param {String} options.gitDir - Current git repo path * @param {Boolean} options.isFn - Whether the linter task is a function * @param {Array} options.files — Filepaths to run the linter task against - * @param {Boolean} [options.relative] — Whether the filepaths should be relative * @param {Boolean} [options.shell] — Whether to skip parsing linter task for better shell support * @param {Boolean} [options.verbose] — Always show task verbose * @returns {function(): Promise>} */ export const resolveTaskFn = ({ command, + cwd = process.cwd(), files, gitDir, isFn, - relative, shell = false, verbose = false, }) => { @@ -89,14 +89,15 @@ export const resolveTaskFn = ({ debugLog('cmd:', cmd) debugLog('args:', args) - const execaOptions = { preferLocal: true, reject: false, shell } - if (relative) { - execaOptions.cwd = process.cwd() - } else if (/^git(\.exe)?/i.test(cmd) && gitDir !== process.cwd()) { + const execaOptions = { // Only use gitDir as CWD if we are using the git binary // e.g `npm` should run tasks in the actual CWD - execaOptions.cwd = gitDir + cwd: /^git(\.exe)?/i.test(cmd) ? gitDir : cwd, + preferLocal: true, + reject: false, + shell, } + debugLog('execaOptions:', execaOptions) return async (ctx = getInitialState()) => { diff --git a/lib/runAll.js b/lib/runAll.js index 08b9d6f55..1ba726004 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -135,6 +135,7 @@ export const runAll = async ( for (const task of chunkTasks) { const subTasks = await makeCmdTasks({ commands: task.commands, + cwd, files: task.fileList, gitDir, renderer: listrOptions.renderer, diff --git a/test/makeCmdTasks.spec.js b/test/makeCmdTasks.spec.js index c591bd5f7..5d5e3164c 100644 --- a/test/makeCmdTasks.spec.js +++ b/test/makeCmdTasks.spec.js @@ -43,6 +43,7 @@ describe('makeCmdTasks', () => { await taskPromise expect(execa).toHaveBeenCalledTimes(1) expect(execa).lastCalledWith('test', ['test.js'], { + cwd: process.cwd(), preferLocal: true, reject: false, shell: false, @@ -52,6 +53,7 @@ describe('makeCmdTasks', () => { await taskPromise expect(execa).toHaveBeenCalledTimes(2) expect(execa).lastCalledWith('test2', ['test.js'], { + cwd: process.cwd(), preferLocal: true, reject: false, shell: false, diff --git a/test/resolveTaskFn.spec.js b/test/resolveTaskFn.spec.js index 642d88559..1c4e70e9f 100644 --- a/test/resolveTaskFn.spec.js +++ b/test/resolveTaskFn.spec.js @@ -21,6 +21,7 @@ describe('resolveTaskFn', () => { await taskFn() expect(execa).toHaveBeenCalledTimes(1) expect(execa).lastCalledWith('node', ['--arg=true', './myscript.js', 'test.js'], { + cwd: process.cwd(), preferLocal: true, reject: false, shell: false, @@ -38,6 +39,7 @@ describe('resolveTaskFn', () => { await taskFn() expect(execa).toHaveBeenCalledTimes(1) expect(execa).lastCalledWith('node', ['--arg=true', './myscript.js', 'test.js'], { + cwd: process.cwd(), preferLocal: true, reject: false, shell: false, @@ -56,6 +58,7 @@ describe('resolveTaskFn', () => { await taskFn() expect(execa).toHaveBeenCalledTimes(1) expect(execa).lastCalledWith('node --arg=true ./myscript.js test.js', { + cwd: process.cwd(), preferLocal: true, reject: false, shell: true, @@ -73,6 +76,7 @@ describe('resolveTaskFn', () => { await taskFn() expect(execa).toHaveBeenCalledTimes(1) expect(execa).lastCalledWith('node --arg=true ./myscript.js test.js', { + cwd: process.cwd(), preferLocal: true, reject: false, shell: true, @@ -90,6 +94,7 @@ describe('resolveTaskFn', () => { await taskFn() expect(execa).toHaveBeenCalledTimes(1) expect(execa).lastCalledWith('node --arg=true ./myscript.js test.js', { + cwd: process.cwd(), preferLocal: true, reject: false, shell: '/bin/bash', @@ -121,23 +126,6 @@ describe('resolveTaskFn', () => { await taskFn() expect(execa).toHaveBeenCalledTimes(1) expect(execa).lastCalledWith('jest', ['test.js'], { - preferLocal: true, - reject: false, - shell: false, - }) - }) - - it('should always pass `process.cwd()` as `cwd` to `execa()` when relative = true', async () => { - expect.assertions(2) - const taskFn = resolveTaskFn({ - ...defaultOpts, - command: 'git diff', - relative: true, - }) - - await taskFn() - expect(execa).toHaveBeenCalledTimes(1) - expect(execa).lastCalledWith('git', ['diff', 'test.js'], { cwd: process.cwd(), preferLocal: true, reject: false,