Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to escape regular expression chars #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ const scriptFromShell = (shell = systemShell()) => {
return path.join(__dirname, 'scripts/bash.sh');
};

/**
* Helper to escape regular expression chars
*
* @param {String} str - String to escape the chars from
* @returns The escaped text
*/
const escapeRegExp = (str) => {
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}

/**
* Helper to return the expected location for SHELL config file, based on the
* provided shell value.
Expand Down Expand Up @@ -122,7 +132,7 @@ const checkFilenameForLine = async (filename, line) => {
}
}

return !!filecontent.match(`${line}`);
return !!filecontent.match(escapeRegExp(`${line}`));
};

/**
Expand Down Expand Up @@ -324,7 +334,7 @@ const removeLinesFromFilename = async (filename, name) => {
? `# tabtab source for packages`
: `# tabtab source for ${name} package`;

const hasLine = !!filecontent.match(`${sourceLine}`);
const hasLine = !!filecontent.match(escapeRegExp(`${sourceLine}`));
if (!hasLine) {
return debug('File %s does not include the line: %s', filename, sourceLine);
}
Expand All @@ -333,7 +343,7 @@ const removeLinesFromFilename = async (filename, name) => {
const buffer = lines
// Build up the new buffer, removing the 3 lines following the sourceline
.map((line, index) => {
const match = line.match(sourceLine);
const match = line.match(escapeRegExp(sourceLine));
if (match) {
lineIndex = index;
} else if (lineIndex + 3 <= index) {
Expand Down