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

Commit 7fa5c08

Browse files
sazoKent C. Dodds
authored andcommitted
fix: handle escaping and single quotes (#158)
1 parent 50299d9 commit 7fa5c08

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/__tests__/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ it(`should handle quoted scripts`, () => {
7474
})
7575
})
7676

77+
it(`should handle escaped characters`, () => {
78+
// this escapes \,",' and $
79+
crossEnv(['GREETING=Hi', 'NAME=Joe', 'echo \\"\\\'\\$GREETING\\\'\\" && echo $NAME'], {
80+
shell: true,
81+
})
82+
expect(
83+
crossSpawnMock.spawn,
84+
).toHaveBeenCalledWith("echo \"'$GREETING'\" && echo $NAME", [], {
85+
stdio: 'inherit',
86+
shell: true,
87+
env: Object.assign({}, process.env, {
88+
GREETING: 'Hi',
89+
NAME: 'Joe',
90+
}),
91+
})
92+
})
93+
7794
it(`should do nothing given no command`, () => {
7895
crossEnv([])
7996
expect(crossSpawnMock.spawn).toHaveBeenCalledTimes(0)

src/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,21 @@ function parseCommand(args) {
5151
envSetters[match[1]] = value
5252
} else {
5353
// No more env setters, the rest of the line must be the command and args
54-
command = args[i]
55-
commandArgs = args.slice(i + 1)
54+
let cStart = []
55+
cStart = args.slice(i)
56+
// Regex:
57+
// match "\'" or "'"
58+
// or match "\" if followed by [$"\] (lookahead)
59+
.map((a) => {
60+
const re = new RegExp(/(\\)?'|([\\])(?=[$"\\])/, 'g')
61+
// Eliminate all matches except for "\'" => "'"
62+
return a.replace(re, (m) => {
63+
if(m === "\\'") return "'"
64+
return ""
65+
})
66+
})
67+
command = cStart[0]
68+
commandArgs = cStart.slice(1)
5669
break
5770
}
5871
}

0 commit comments

Comments
 (0)