-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
138 lines (108 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
"use strict";
var through = require('through'),
gutil = require('gulp-util'),
crypto = require('crypto'),
path = require('path'),
minimatch = require('minimatch'),
slash = require('slash'),
lineBreak = '\n';
function manifest(options) {
var filename, exclude, cache, include, hasher, cwd, contents, timestamp;
options = options || {};
if(options.basePath) {
gutil.log('basePath option is deprecated. Consider using gulp.src base instead: https://github.com/gulpjs/gulp/blob/master/docs/API.md#optionsbase');
}
filename = options.filename || 'app.manifest';
include = Array.prototype.concat(options.include || []);
exclude = Array.prototype.concat(options.exclude || []).concat(include);
cache = Array.prototype.concat(options.cache || []);
hasher = crypto.createHash('sha256');
cwd = process.cwd();
contents = [];
timestamp = options.timestamp || (typeof(options.timestamp) === 'undefined');
contents.push('CACHE MANIFEST');
if (timestamp) {
contents.push('# Time: ' + new Date());
}
if (options.revision) {
contents.push('# Revision: ' + options.revision);
}
contents.push('');
contents.push('CACHE:');
contents = contents.concat(include);
cache.forEach(function (file) {
contents.push(encodeURI(file));
});
function shouldExcludeFile(filePath) {
return exclude.some(minimatch.bind(null, filePath));
}
function writeToManifest(file) {
var prefix, suffix, filepath;
if (file.isNull()) return;
if (file.isStream()) return this.emit('error', new gutil.PluginError('gulp-manifest', 'Streaming not supported'));
prefix = slash(options.prefix || '');
suffix = slash(options.suffix || '');
filepath = slash(file.relative);
if (shouldExcludeFile(filepath)) {
return;
}
if(options.basePath) { // deprecated
var relative = path.relative(file.base, __dirname);
filepath = filepath.replace(new RegExp('^' + path.join(relative, options.basePath)), '');
}
filepath = [prefix, filepath, suffix].join('');
contents.push(encodeURI(filepath));
if (options.hash) {
hasher.update(file.contents, 'binary');
}
}
function endStream() {
// Network section
options.network = options.network || ['*'];
contents.push('');
contents.push('NETWORK:');
options.network.forEach(function (file) {
contents.push(encodeURI(file));
});
// Fallback section
if (options.fallback) {
contents.push('');
contents.push('FALLBACK:');
if (typeof options.fallback === 'string') {
options.fallback = [options.fallback];
}
options.fallback.forEach(function (file) {
var firstSpace = file.indexOf(' ');
if (firstSpace === -1) {
return gutil.log('Invalid format for FALLBACK entry', file);
}
contents.push(
encodeURI(file.substring(0, firstSpace)) +
' ' +
encodeURI(file.substring(firstSpace + 1))
);
});
}
// Settings section
if (options.preferOnline) {
contents.push('');
contents.push('SETTINGS:');
contents.push('prefer-online');
}
// output hash to cache manifest
if (options.hash) {
contents.push('');
contents.push('# hash: ' + hasher.digest("hex"));
}
var manifestFile = new gutil.File({
cwd: cwd,
base: cwd,
path: path.join(cwd, filename),
contents: new Buffer(contents.join(lineBreak))
});
this.emit('data', manifestFile);
this.emit('end');
}
return through(writeToManifest, endStream);
}
module.exports = manifest;