-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
113 lines (105 loc) · 4.04 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
'use strict';
var Stream = require('stream');
var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var PATTERN_KEYS = Object.keys(path.parse('')).concat('hash');
function parseFilePattern(pattern, fileName, cwd, hash) {
pattern = pattern || '';
fileName = fileName || '';
var values = path.parse(fileName);
values.dir = path.relative(cwd, values.dir);
values.hash = hash;
return PATTERN_KEYS.reduce(function(newFilePath, key) {
return newFilePath.replace('{' + key + '}', values[key]);
}, pattern);
}
module.exports = function hashmark(contents, options, callback) {
contents = (contents instanceof Array) ? contents : [contents];
var mapEvents = new EventEmitter();
var fileCount = contents.length;
var map = {};
if (!callback && typeof options === 'function') {
callback = options;
options = {};
}
options.digest = options.digest || 'sha256';
options.length = options.length || 0;
if (callback) {
mapEvents.on('error', callback);
mapEvents.on('end', function (map) {
callback(null, map);
});
}
contents.forEach(function(contents){
if (contents instanceof Stream.Readable) {
contents.fileName = contents.fileName || '<anonymous>';
fileContents(contents);
} else {
fs.stat(contents, function(err, stats){
if(stats.isFile()){
var stream = fs.createReadStream(contents);
stream.fileName = contents;
fileContents(stream);
}
});
}
});
function fileContents(stream) {
var contents = new Buffer('');
var hash = crypto.createHash(options.digest);
hash.on('error', mapEvents.emit.bind(mapEvents, 'error'));
stream.on('error', mapEvents.emit.bind(mapEvents, 'error'));
if (options.pattern) {
var contentStream = new Stream.PassThrough();
contentStream.on('error', mapEvents.emit.bind(mapEvents, 'error'));
contentStream.on('data', function (chunk) {
contents = Buffer.concat([contents, chunk]);
});
stream.pipe(contentStream);
}
stream.on('end', function () {
var digest = hash.digest('hex');
if (options.length) {
digest = digest.slice(0, options.length);
}
if (options.pattern) {
var fileName = path.resolve(options.cwd,
parseFilePattern(options.pattern, stream.fileName, options.cwd, digest));
if (options.rename === true ) {
fs.rename(stream.fileName, fileName, function (err) {
if (err) {
return mapEvents.emit('error', err);
}
mapEvents.emit('file', stream.fileName, fileName);
});
} else {
mkdirp(path.dirname(fileName), function (err) {
if (err) {
return mapEvents.emit('error', err);
} else {
fs.writeFile(fileName, contents, function (err) {
if (err) {
return mapEvents.emit('error', err);
}
mapEvents.emit('file', stream.fileName, fileName);
});
}
});
}
} else {
mapEvents.emit('file', stream.fileName, digest);
}
});
stream.pipe(hash, { end: false });
}
return mapEvents.on('file', function (fileName, newFileName) {
map[path.relative(options.cwd, fileName)] = path.relative(options.cwd, newFileName);
fileCount--;
if (fileCount === 0) {
mapEvents.emit('end', map);
}
});
};