-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (114 loc) · 3.88 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
/* globals module: true, process: false, Buffer: false */
"use strict";
var crypto = require("crypto");
var gutil = require("gulp-util");
var _ = require("lodash");
var mkdirp = require("mkdirp");
var slash = require("slash");
var through = require("through");
var json_format = require("gulp-json-format");
var fs = require("fs");
var path = require("path");
module.exports = hashsumx;
function hashsumx(options) {
options = _.defaults(options || {}, {
template_input: null,
template_output: null,
hash: "sha1",
hashlen: 40,
format: 'get',
debug: false
});
var dbg = options.debug;
var cwd = process.cwd();
var hashes = [];
function throwError(msg) {
throw new gutil.PluginError({
plugin: 'gulp-hashx',
message: msg || 'cowabunga, error dude'
});
}
function processFile(file) {
if (file.isNull()) {
return;
}
if (file.isStream()) {
this.emit("error", new gutil.PluginError("gulp-hashsum", "Streams not supported"));
return;
}
if (dbg) {
console.log('processFile: ' + file.path + ' (' + path.basename(file.path) + ')');
}
var dirname = path.dirname(file.path);
var basename = path.basename(file.path);
var ext = path.extname(file.path);
var basename2 = path.basename(file.path, ext);
var hash = crypto.createHash(options.hash).update(file.contents, "binary").digest("hex");
var hash_short = hash.substr(0, options.hashlen);
var new_filename;
if (options.format === 'embedded') {
new_filename = path.resolve(dirname, basename2 + '._v' + hash_short + ext);
} else {
new_filename = path.resolve(dirname, basename + '?' + hash_short);
}
var new_basename = path.basename(new_filename);
hashes.push({
dirname: dirname,
basename: basename,
basename2: basename2,
ext: ext,
filename: file.path,
hash: hash,
hash_short: hash_short,
new_filename: new_filename,
new_basename: new_basename,
});
if (dbg) {
console.log('new h: ' + hash + ' (' + JSON.stringify(hashes, null, 4) + ')');
}
this.push(file);
}
function writeSums() {
if (dbg) {
console.log('writing....');
console.log(JSON.stringify(hashes, null, 4));
}
var template_input = options.template_input;
if (typeof template_input === "string") {
template_input = [ template_input ];
}
var template_output = options.template_output;
if (typeof template_output === "string") {
template_output = [ template_output ];
}
for (var tidx = 0; tidx < template_input.length; tidx++) {
var template_input_file = path.resolve(cwd, template_input[tidx]);
var template_output_file = path.resolve(cwd, template_output[tidx]);
if (dbg) {
console.log(template_input_file);
console.log(template_output_file);
}
var indata = fs.readFileSync(template_input_file).toString('utf-8');
if (dbg) {
console.log(indata);
}
for (var idx in hashes) {
var item = hashes[idx];
if (dbg) {
console.log('doing: ' + item.basename);
}
indata = indata.replace(new RegExp(_.escapeRegExp(item.basename), 'g'), item.new_basename);
}
if (dbg) {
console.log(indata);
}
var outputDir = path.dirname(template_output_file);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
fs.writeFileSync(template_output_file, indata);
}
this.emit("end");
}
return through(processFile, writeSums);
}