This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
147 lines (128 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var replace = require('replacestream');
var StreamCache = require('stream-cache');
var digest = require('digest-stream');
var fs = require('fs');
var glob = require('glob');
var pathTransformer = require('./lib/pathTransformer');
var getReplaceMap = require('./lib/getReplaceMap');
var createWriteStream = require('./lib/createWriteStream');
function getReadStream(file) {
return new Promise(function(resolve, reject){
var inputStream = fs.createReadStream(file, {});
inputStream.on('open', function(){
resolve(inputStream);
});
inputStream.on('error', function(error){
reject(error);
});
});
}
function chainReplacers(replaceMap) {
function pipeReplacer(stream, oldValue) {
var newValue = replaceMap[oldValue];
return stream.pipe(replace(oldValue, newValue));
}
return function(inputStream) {
return Object.keys(replaceMap)
.sort(function (a, b) { return b.length - a.length; })
.reduce(pipeReplacer, inputStream);
}
}
function interceptHash(callback) {
return function intercept(inputStream){
return inputStream.pipe(digest('sha1', 'hex', callback));
}
}
function waitForFinish(stream) {
return new Promise(function(resolve, reject){
stream.on('finish', resolve);
stream.on('error', reject);
});
}
function readAndReplaceStream(file, replaceMap) {
var result = {
inputHash: null,
outputHash: null,
cache: null,
stream: null
}
return getReadStream(file)
.then(interceptHash(function(hash) {
result.inputHash = hash;
}))
.then(chainReplacers(replaceMap))
.then(interceptHash(function(hash) {
result.outputHash = hash;
}))
.then(function(stream){
result.cache = stream.pipe(new StreamCache());
result.stream = stream;
return stream;
})
.then(waitForFinish)
.then(function(){
return result;
});
}
function createDumpStream(cache) {
return function dumpStream(outputStream) {
return new Promise(function(resolve, reject) {
var dumpStream = cache.pipe(outputStream);
dumpStream.on('finish', resolve);
dumpStream.on('error', reject);
});
};
}
function runSinglePath(sourcePath, destPath, options) {
return readAndReplaceStream(sourcePath, options.replaceMap)
.then(function handleReadAndReplace(result){
return createWriteStream(destPath, options.encoding)
.then(createDumpStream(result.cache))
.then(function(){
return {
src: sourcePath,
dest: destPath,
changed: result.inputHash !== result.outputHash
};
});
});
}
function listPaths(globStr) {
return new Promise(function(resolve, reject){
glob(globStr, function (err, paths) {
if (err) {
reject(err);
} else {
resolve(paths);
}
});
});
}
function runPaths(sourcePaths, options) {
var getDestPath = options.destPattern ?
pathTransformer(options.destPattern) :
identity;
return Promise.all(sourcePaths.map(function(sourcePath){
var destPath = getDestPath(sourcePath);
return runSinglePath(sourcePath, destPath, options);
}));
}
module.exports = function run(options) {
return Promise.all([
listPaths(options.source),
getReplaceMap(options.encoding, options.replaceMapPath, options.replaceMap)
]).then(function(results){
var sourcePaths = results[0];
var replaceMap = results[1];
options.replaceMap = replaceMap;
return runPaths(sourcePaths, options);
}).then(function(result){
return {
options: options,
result: result
};
});
}
function identity(i) {
return i;
}