From 18dd9ecd4e26eefd0a3bb8fea3efb5111d0fe8a1 Mon Sep 17 00:00:00 2001 From: Daniel Beus Date: Wed, 9 Aug 2017 14:45:51 -0600 Subject: [PATCH] Added ability to use regex in search --- index.js | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 830c360..f805036 100644 --- a/index.js +++ b/index.js @@ -8,15 +8,17 @@ var pathTransformer = require('./lib/pathTransformer'); var getReplaceMap = require('./lib/getReplaceMap'); var createWriteStream = require('./lib/createWriteStream'); +var regexCharacter = '~'; + function getReadStream(file) { - return new Promise(function(resolve, reject){ + return new Promise(function (resolve, reject) { var inputStream = fs.createReadStream(file, {}); - inputStream.on('open', function(){ + inputStream.on('open', function () { resolve(inputStream); }); - inputStream.on('error', function(error){ + inputStream.on('error', function (error) { reject(error); }); }); @@ -25,24 +27,34 @@ function getReadStream(file) { function chainReplacers(replaceMap) { function pipeReplacer(stream, oldValue) { var newValue = replaceMap[oldValue]; + + if (oldValue.length > 1 && oldValue[0] === regexCharacter && oldValue[1] === regexCharacter) { + var closing = oldValue.lastIndexOf('/'); + var flags = oldValue.substr(closing + 1); + oldValue = oldValue.substr(3, closing - 3); + oldValue = new RegExp(oldValue, flags); + } + return stream.pipe(replace(oldValue, newValue)); } - return function(inputStream) { + return function (inputStream) { return Object.keys(replaceMap) - .sort(function (a, b) { return b.length - a.length; }) + .sort(function (a, b) { + return b.length - a.length; + }) .reduce(pipeReplacer, inputStream); } } function interceptHash(callback) { - return function intercept(inputStream){ + return function intercept(inputStream) { return inputStream.pipe(digest('sha1', 'hex', callback)); } } function waitForFinish(stream) { - return new Promise(function(resolve, reject){ + return new Promise(function (resolve, reject) { stream.on('finish', resolve); stream.on('error', reject); }); @@ -58,27 +70,27 @@ function readAndReplaceStream(file, replaceMap) { } return getReadStream(file) - .then(interceptHash(function(hash) { + .then(interceptHash(function (hash) { result.inputHash = hash; })) .then(chainReplacers(replaceMap)) - .then(interceptHash(function(hash) { + .then(interceptHash(function (hash) { result.outputHash = hash; })) - .then(function(stream){ + .then(function (stream) { result.cache = stream.pipe(new StreamCache()); result.stream = stream; return stream; }) .then(waitForFinish) - .then(function(){ + .then(function () { return result; }); } function createDumpStream(cache) { return function dumpStream(outputStream) { - return new Promise(function(resolve, reject) { + return new Promise(function (resolve, reject) { var dumpStream = cache.pipe(outputStream); dumpStream.on('finish', resolve); dumpStream.on('error', reject); @@ -89,10 +101,10 @@ function createDumpStream(cache) { function runSinglePath(sourcePath, destPath, options) { return readAndReplaceStream(sourcePath, options.replaceMap) - .then(function handleReadAndReplace(result){ + .then(function handleReadAndReplace(result) { return createWriteStream(destPath, options.encoding) .then(createDumpStream(result.cache)) - .then(function(){ + .then(function () { return { src: sourcePath, dest: destPath, @@ -103,7 +115,7 @@ function runSinglePath(sourcePath, destPath, options) { } function listPaths(globStr) { - return new Promise(function(resolve, reject){ + return new Promise(function (resolve, reject) { glob(globStr, function (err, paths) { if (err) { reject(err); @@ -119,7 +131,7 @@ function runPaths(sourcePaths, options) { pathTransformer(options.destPattern) : identity; - return Promise.all(sourcePaths.map(function(sourcePath){ + return Promise.all(sourcePaths.map(function (sourcePath) { var destPath = getDestPath(sourcePath); return runSinglePath(sourcePath, destPath, options); })); @@ -129,12 +141,12 @@ module.exports = function run(options) { return Promise.all([ listPaths(options.source), getReplaceMap(options.encoding, options.replaceMapPath, options.replaceMap) - ]).then(function(results){ + ]).then(function (results) { var sourcePaths = results[0]; var replaceMap = results[1]; options.replaceMap = replaceMap; return runPaths(sourcePaths, options); - }).then(function(result){ + }).then(function (result) { return { options: options, result: result @@ -144,4 +156,4 @@ module.exports = function run(options) { function identity(i) { return i; -} +} \ No newline at end of file