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

fix: escape the regex for the path to the entry module of application #982

Merged
merged 2 commits into from
Jul 18, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down
16 changes: 15 additions & 1 deletion index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
});
});