Skip to content

Commit

Permalink
used code from PR: mklabs#49 by sorleone
Browse files Browse the repository at this point in the history
  • Loading branch information
int0h committed Sep 16, 2019
1 parent 356406d commit 42f33a1
Showing 1 changed file with 13 additions and 3 deletions.
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 @@ -329,7 +339,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 @@ -338,7 +348,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

0 comments on commit 42f33a1

Please sign in to comment.