Skip to content
This repository was archived by the owner on Mar 15, 2018. It is now read-only.
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
50 changes: 31 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand All @@ -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);
});
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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);
}));
Expand All @@ -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
Expand All @@ -144,4 +156,4 @@ module.exports = function run(options) {

function identity(i) {
return i;
}
}