diff --git a/index.js b/index.js index 2ed399dc..3d24db39 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const path = require("path"); const { existsSync } = require("fs"); +const escapeRegExp = require("escape-string-regexp"); const { ANDROID_APP_PATH } = require("./androidProjectHelpers"); const { getPackageJson, @@ -62,8 +63,7 @@ exports.getAppPath = (platform, projectDir) => { exports.getEntryPathRegExp = (appFullPath, entryModule) => { const entryModuleFullPath = path.join(appFullPath, entryModule); - // Windows paths contain `\`, so we need to convert each of the `\` to `\\`, so it will be correct inside RegExp - const escapedPath = entryModuleFullPath.replace(/\\/g, "\\\\"); + const escapedPath = escapeRegExp(entryModuleFullPath); return new RegExp(escapedPath); } diff --git a/index.spec.ts b/index.spec.ts index eda14f88..d97dc20d 100644 --- a/index.spec.ts +++ b/index.spec.ts @@ -61,7 +61,7 @@ describe('index', () => { path.join = originalPathJoin; }); - it('returns RegExp that matches Windows', () => { + it('returns RegExp that works with Windows paths', () => { const appPath = "D:\\Work\\app1\\app"; path.join = path.win32.join; const regExp = getEntryPathRegExp(appPath, entryModule); @@ -74,5 +74,19 @@ describe('index', () => { const regExp = getEntryPathRegExp(appPath, entryModule); expect(!!regExp.exec(`${appPath}/${entryModule}`)).toBe(true); }); + + it('returns RegExp that works with Windows paths with special symbol in path', () => { + const appPath = "D:\\Work\\app1\\app (2)"; + path.join = path.win32.join; + const regExp = getEntryPathRegExp(appPath, entryModule); + expect(!!regExp.exec(`${appPath}\\${entryModule}`)).toBe(true); + }); + + it('returns RegExp that works with POSIX paths with special symbol in path', () => { + const appPath = "/usr/local/lib/app1/app (2)"; + path.join = path.posix.join; + const regExp = getEntryPathRegExp(appPath, entryModule); + expect(!!regExp.exec(`${appPath}/${entryModule}`)).toBe(true); + }); }); });