-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
80 lines (64 loc) · 2.02 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
'use strict';
var Stream = require('stream');
var gutil = require('gulp-util');
var minimatch = require('minimatch');
var PLUGIN_NAME = 'gulp-cordova-app-loader-manifest';
var calManifest = function calManifest(options) {
options = options || {};
if (!options.load) {
options.load = ['**'];
} else if (!(options.load instanceof Array)) {
options.load = [options.load];
}
var manifest = {
files: {},
load: [],
root: options.root || './'
};
var stream = new Stream.Transform({objectMode: true});
stream._transform = function (file, unused, done) {
if (file.isNull() || !file.stat.isFile()) {
return done();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return done();
}
var hasher = require('crypto').createHash('sha256');
var filename = encodeURI(file.relative);
var key = filename.replace(/\//g, '_');
manifest.files[key] = {
filename: filename,
version: hasher.update(file.contents).digest('hex')
};
var i, pattern;
var doLoad = false;
for (i = 0; i < options.load.length; i++) {
pattern = options.load[i];
if (pattern.charAt(0) === '!') {
if (minimatch(filename, pattern, {flipNegate: true})) {
doLoad = false;
break;
}
} else {
if (minimatch(filename, pattern)) {
doLoad = true;
}
}
}
if (doLoad) {
manifest.load.push(filename);
}
done();
};
stream._flush = function (done) {
var file = new gutil.File({
path: 'manifest.json',
contents: new Buffer(JSON.stringify(manifest, null, 4))
});
stream.push(file);
return done();
};
return stream;
};
module.exports = calManifest;