From 0b45ddbb3c219ff9a5dab29705ac885cd79ac060 Mon Sep 17 00:00:00 2001 From: Shazron Abdullah Date: Sun, 8 Nov 2020 11:53:57 +0800 Subject: [PATCH] fix: ACNA-949 - command chaining, and separators broken (#321) --- src/lib/app-helper.js | 8 ++++++-- test/commands/lib/app-helper.test.js | 22 +++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/lib/app-helper.js b/src/lib/app-helper.js index eddad858..df99b1f8 100644 --- a/src/lib/app-helper.js +++ b/src/lib/app-helper.js @@ -52,9 +52,13 @@ async function runPackageScript (scriptName, dir, cmdArgs = []) { aioLogger.debug(`running npm run-script ${scriptName} in dir: ${dir}`) const pkg = await fs.readJSON(path.join(dir, 'package.json')) if (pkg && pkg.scripts && pkg.scripts[scriptName]) { - const command = pkg.scripts[scriptName].split(' ') - const child = execa(command[0], command.slice(1).concat(cmdArgs), { + let command = pkg.scripts[scriptName] + if (cmdArgs.length) { + command = `${command} ${cmdArgs.join(' ')}` + } + const child = execa.command(command, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], + shell: true, cwd: dir, preferLocal: true }) diff --git a/test/commands/lib/app-helper.test.js b/test/commands/lib/app-helper.test.js index ad862943..894de804 100644 --- a/test/commands/lib/app-helper.test.js +++ b/test/commands/lib/app-helper.test.js @@ -66,7 +66,10 @@ describe('exports helper methods', () => { }) test('runPackageScript success', async () => { - fs.readJSON.mockReturnValue({ scripts: { test: 'some-script some-arg-1 some-arg-2' } }) + const scripts = { + test: 'some-script some-arg-1 some-arg-2' + } + fs.readJSON.mockReturnValue({ scripts }) const ipcMessage = { type: 'long-running-process', @@ -105,7 +108,7 @@ describe('exports helper methods', () => { } }) - execa.mockReturnValueOnce({ + execa.command.mockReturnValueOnce({ on: mockChildProcessOn }) @@ -113,21 +116,26 @@ describe('exports helper methods', () => { expect(mockChildProcessOn).toHaveBeenCalledWith('message', expect.any(Function)) expect(process.on).toHaveBeenCalledWith('exit', expect.any(Function)) expect(process.kill).toHaveBeenCalledWith(ipcMessage.data.pid, 'SIGTERM') - return expect(execa).toHaveBeenCalledWith('some-script', ['some-arg-1', 'some-arg-2'], expect.any(Object)) + return expect(execa.command).toHaveBeenCalledWith(scripts.test, expect.any(Object)) }) test('runPackageScript success with additional command arg/flag', async () => { // succeeds if npm run-script returns success - fs.readJSON.mockReturnValue({ scripts: { cmd: 'some-script some-arg-1 some-arg-2' } }) + const scripts = { + cmd: 'some-script some-arg-1 some-arg-2' + } + fs.readJSON.mockReturnValue({ scripts }) const mockChildProcessOn = jest.fn() - execa.mockReturnValueOnce({ + execa.command.mockReturnValueOnce({ on: mockChildProcessOn }) - await appHelper.runPackageScript('cmd', '', ['--my-flag']) + const cmdExtraArgs = ['--my-flag'] + await appHelper.runPackageScript('cmd', '', cmdExtraArgs) expect(mockChildProcessOn).toHaveBeenCalledWith('message', expect.any(Function)) - return expect(execa).toHaveBeenCalledWith('some-script', ['some-arg-1', 'some-arg-2', '--my-flag'], expect.any(Object)) + const finalCommand = `${scripts.cmd} ${cmdExtraArgs.join(' ')}` + return expect(execa.command).toHaveBeenCalledWith(finalCommand, expect.any(Object)) }) test('runPackageScript logs if package.json does not have matching script', async () => {