From c73deb5845c260c08644797147af3adc0657b86e Mon Sep 17 00:00:00 2001
From: brucejo <brucejo.wk@outlook.com>
Date: Fri, 10 Nov 2017 10:56:41 -0800
Subject: [PATCH] Handle escaping and single quotes

---
 src/__tests__/index.js | 17 +++++++++++++++++
 src/index.js           | 17 +++++++++++++++--
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/src/__tests__/index.js b/src/__tests__/index.js
index 328c5bf..985df57 100644
--- a/src/__tests__/index.js
+++ b/src/__tests__/index.js
@@ -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)
diff --git a/src/index.js b/src/index.js
index cddf21a..edad2d9 100644
--- a/src/index.js
+++ b/src/index.js
@@ -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
     }
   }