diff --git a/lib/installer.js b/lib/installer.js index 1e1e207..88022e4 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -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. @@ -122,7 +132,7 @@ const checkFilenameForLine = async (filename, line) => { } } - return !!filecontent.match(`${line}`); + return !!filecontent.match(escapeRegExp(`${line}`)); }; /** @@ -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); } @@ -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) {