Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
fix: handle escaping and single quotes (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazo authored and Kent C. Dodds committed Dec 21, 2017
1 parent 50299d9 commit 7fa5c08
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ it(`should handle quoted scripts`, () => {
})
})

it(`should handle escaped characters`, () => {
// this escapes \,",' and $
crossEnv(['GREETING=Hi', 'NAME=Joe', 'echo \\"\\\'\\$GREETING\\\'\\" && echo $NAME'], {
shell: true,
})
expect(
crossSpawnMock.spawn,
).toHaveBeenCalledWith("echo \"'$GREETING'\" && echo $NAME", [], {
stdio: 'inherit',
shell: true,
env: Object.assign({}, process.env, {
GREETING: 'Hi',
NAME: 'Joe',
}),
})
})

it(`should do nothing given no command`, () => {
crossEnv([])
expect(crossSpawnMock.spawn).toHaveBeenCalledTimes(0)
Expand Down
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,21 @@ function parseCommand(args) {
envSetters[match[1]] = value
} else {
// No more env setters, the rest of the line must be the command and args
command = args[i]
commandArgs = args.slice(i + 1)
let cStart = []
cStart = args.slice(i)
// Regex:
// match "\'" or "'"
// or match "\" if followed by [$"\] (lookahead)
.map((a) => {
const re = new RegExp(/(\\)?'|([\\])(?=[$"\\])/, 'g')

This comment has been minimized.

Copy link
@davidrissato

davidrissato Dec 21, 2017

This line breaks compatibility with previous node versions. When using node 4.4.3, I get this message:

TypeError: Cannot supply flags when constructing one RegExp from another
    at new RegExp (native)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:262:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:431:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
// Eliminate all matches except for "\'" => "'"
return a.replace(re, (m) => {
if(m === "\\'") return "'"
return ""
})
})
command = cStart[0]
commandArgs = cStart.slice(1)
break
}
}
Expand Down

0 comments on commit 7fa5c08

Please sign in to comment.