-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
118 lines (102 loc) · 3.55 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
'use strict';
var crypto = require('crypto');
var path = require('path');
var through = require('through');
var assign = require('object-assign');
var defaults = require('lodash.defaults');
var gutil = require('gulp-util');
var Promise = require('bluebird');
var DEFAULT_OPTIONS = {
fileName: 'busters.json',
algo: 'md5',
length: 0,
transform: Object,
formatter: JSON.stringify,
relativePath: '.',
};
var OPTION_TYPES = {
fileName: ['String'],
algo: ['String', 'Function'],
length: ['Number'],
transform: ['Function'],
formatter: ['Function'],
relativePath: ['String'],
};
var hashesStore = {}; // options.fileName: { relativePath: hash }
function error(msg) {
return new gutil.PluginError('gulp-buster', msg);
}
function hash(file, options) {
return typeof options.algo === 'function'
? options.algo.call(undefined, file)
: crypto.createHash(options.algo).update(file.contents).digest('hex');
}
function sliceHash(hash, options) {
// positive length = leading characters; negative = trailing
return options.length
? options.length > 0
? hash.slice(0, options.length)
: hash.slice(options.length)
: hash;
}
function relativePath(projectPath, relativePath, filePath) {
return path.relative(path.join(projectPath, relativePath), filePath).replace(/\\/g, '/');
}
function getType(value) {
return {}.toString.call(value).slice(8, -1);
}
function assignOptions(options) {
if (typeof options === 'string') options = { fileName: options };
options = options || {};
Object.keys(options).forEach(function(option) {
if (!OPTION_TYPES.hasOwnProperty(option)) throw error('Unsupported option: ' + option);
if (options[option] !== undefined && OPTION_TYPES[option].indexOf(getType(options[option])) === -1) throw error('`options.' + option + '` must be of type ' + OPTION_TYPES[option].join(' or '));
});
return defaults({}, options, DEFAULT_OPTIONS);
}
module.exports = exports = function(options) {
options = assignOptions(options);
var hashes = hashesStore[options.fileName] = hashesStore[options.fileName] || {},
hashingPromises = [];
function hashFile(file) {
if (file.isNull()) return; // ignore
if (file.isStream()) return this.emit('error', error('Streaming not supported'));
// start hashing files as soon as they are received for maximum concurrency
hashingPromises.push(
Promise.try(hash.bind(undefined, file, options)).then(function(hashed) {
if (typeof hashed !== 'string') throw error('Return/fulfill value of `options.algo` must be a string');
hashes[relativePath(file.cwd, options.relativePath, file.path)] = sliceHash(hashed, options);
})
);
}
function endStream() {
Promise.all(hashingPromises).bind(this).then(function() {
return options.transform.call(undefined, assign({}, hashes));
}).then(function(transformed) {
return options.formatter.call(undefined, transformed);
}).then(function(formatted) {
if (typeof formatted !== 'string') throw error('Return/fulfill value of `options.formatter` must be a string');
this.emit('data', new gutil.File({
path: path.join(process.cwd(), options.fileName),
contents: new Buffer(formatted),
}));
this.emit('end');
}).catch(function(err) {
this.emit('error', err instanceof gutil.PluginError ? err : error(err));
});
}
return through(hashFile, endStream);
};
// for testing. Don't use, may be removed or changed at anytime
assign(exports, {
_Promise: Promise,
_DEFAULT_OPTIONS: DEFAULT_OPTIONS,
_error: error,
_hash: hash,
_relativePath: relativePath,
_getType: getType,
_assignOptions: assignOptions,
_reset: function() {
hashesStore = {};
},
});