Skip to content

Commit

Permalink
Fix not being able to escape more than one space in commands (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed May 22, 2019
1 parent 5766371 commit dd63573
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions fixtures/command with space
@@ -0,0 +1,3 @@
#!/usr/bin/env node
'use strict';
console.log(process.argv.slice(2).join('\n'));
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -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 = []) {
Expand Down
5 changes: 5 additions & 0 deletions test.js
Expand Up @@ -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');
Expand Down

0 comments on commit dd63573

Please sign in to comment.