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

Commit 5cadf73

Browse files
authored
Merge pull request #982 from NativeScript/fatme/fix-entry-module-regex
fix: escape the regex for the path to the entry module of application
2 parents 48da2a1 + 82063e2 commit 5cadf73

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Diff for: index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require("path");
22
const { existsSync } = require("fs");
3+
const escapeRegExp = require("escape-string-regexp");
34
const { ANDROID_APP_PATH } = require("./androidProjectHelpers");
45
const {
56
getPackageJson,
@@ -62,8 +63,7 @@ exports.getAppPath = (platform, projectDir) => {
6263

6364
exports.getEntryPathRegExp = (appFullPath, entryModule) => {
6465
const entryModuleFullPath = path.join(appFullPath, entryModule);
65-
// Windows paths contain `\`, so we need to convert each of the `\` to `\\`, so it will be correct inside RegExp
66-
const escapedPath = entryModuleFullPath.replace(/\\/g, "\\\\");
66+
const escapedPath = escapeRegExp(entryModuleFullPath);
6767
return new RegExp(escapedPath);
6868
}
6969

Diff for: index.spec.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('index', () => {
6161
path.join = originalPathJoin;
6262
});
6363

64-
it('returns RegExp that matches Windows', () => {
64+
it('returns RegExp that works with Windows paths', () => {
6565
const appPath = "D:\\Work\\app1\\app";
6666
path.join = path.win32.join;
6767
const regExp = getEntryPathRegExp(appPath, entryModule);
@@ -74,5 +74,19 @@ describe('index', () => {
7474
const regExp = getEntryPathRegExp(appPath, entryModule);
7575
expect(!!regExp.exec(`${appPath}/${entryModule}`)).toBe(true);
7676
});
77+
78+
it('returns RegExp that works with Windows paths with special symbol in path', () => {
79+
const appPath = "D:\\Work\\app1\\app (2)";
80+
path.join = path.win32.join;
81+
const regExp = getEntryPathRegExp(appPath, entryModule);
82+
expect(!!regExp.exec(`${appPath}\\${entryModule}`)).toBe(true);
83+
});
84+
85+
it('returns RegExp that works with POSIX paths with special symbol in path', () => {
86+
const appPath = "/usr/local/lib/app1/app (2)";
87+
path.join = path.posix.join;
88+
const regExp = getEntryPathRegExp(appPath, entryModule);
89+
expect(!!regExp.exec(`${appPath}/${entryModule}`)).toBe(true);
90+
});
7791
});
7892
});

0 commit comments

Comments
 (0)