diff --git a/fixtures/command with space b/fixtures/command with space new file mode 100755 index 0000000000..e9baf7ef67 --- /dev/null +++ b/fixtures/command with space @@ -0,0 +1,3 @@ +#!/usr/bin/env node +'use strict'; +console.log(process.argv.slice(2).join('\n')); diff --git a/index.js b/index.js index 3fbb537222..52ffd4d1bc 100644 --- a/index.js +++ b/index.js @@ -23,13 +23,13 @@ function handleEscaping(tokens, token, index) { return [token]; } - const previousToken = tokens[index - 1]; + const previousToken = tokens[tokens.length - 1]; - if (!previousToken.endsWith('\\')) { - return [...tokens, token]; + if (previousToken.endsWith('\\')) { + return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`]; } - return [...tokens.slice(0, index - 1), `${previousToken.slice(0, -1)} ${token}`]; + return [...tokens, token]; } function parseCommand(command, args = []) { diff --git a/test.js b/test.js index 5b8bb6526b..aeefcc76c0 100644 --- a/test.js +++ b/test.js @@ -123,6 +123,11 @@ test('escape other whitespaces in string arguments', async t => { t.is(stdout, 'foo\tbar'); }); +test('allow escaping spaces in commands', async t => { + const {stdout} = await execa('./fixtures/command\\ with\\ space foo bar'); + t.is(stdout, 'foo\nbar'); +}); + test('allow escaping spaces in string arguments', async t => { const {stdout} = await execa('node fixtures/echo foo\\ bar'); t.is(stdout, 'foo bar');