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

Handle escaping and single quotes #158

Merged
merged 1 commit into from Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/__tests__/index.js
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
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')
// Eliminate all matches except for "\'" => "'"
return a.replace(re, (m) => {
if(m === "\\'") return "'"
return ""
})
})
command = cStart[0]
commandArgs = cStart.slice(1)
break
}
}
Expand Down