-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
204 lines (176 loc) · 6.68 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*jslint node: true, nomen: true */
'use strict';
/**package
{
"name": "volo-appcache",
"description": "A volo command for generating an appcache manifest",
"keywords": [
"volo"
],
"version": "0.0.1",
"homepage": "http://github.com/volojs/volo-appcache",
"author": "James Burke <[email protected]> (http://github.com/jrburke)",
"licenses": [
{
"type": "BSD",
"url": "https://github.com/volojs/volo-ghdeploy/blob/master/LICENSE"
},
{
"type": "MIT",
"url": "https://github.com/volojs/volo-ghdeploy/blob/master/LICENSE"
}
],
"engines": {
"node": ">=0.6.7"
}
}
**/
var fs = require('fs'),
crypto = require('crypto'),
manifestAttrRegExp = /manifest="manifest\.appcache"/;
function generateDigest(q, files, dir) {
var masterDeferred = q.defer(),
digests = [],
i = 0;
function getDigest(fileName) {
var shaSum = crypto.createHash('sha1'),
d = q.defer(),
stream = fs.ReadStream(fileName);
stream.on('data', function (data) {
shaSum.update(data);
});
stream.on('end', function () {
d.resolve(shaSum.digest('base64'));
});
return d.promise;
}
function digestFile(fileName) {
getDigest(fileName).then(function (digest) {
var shaSum;
digests[i] = digest;
i += 1;
if (i < files.length) {
digestFile(files[i]);
} else {
//All done, now generate the final digest,
//using the combination of the other digests
shaSum = crypto.createHash('sha1');
shaSum.update(digests.join(','));
masterDeferred.resolve(shaSum.digest('base64'));
}
});
}
digestFile(files[0]);
return masterDeferred.promise;
}
function frontSlash(path) {
return path.replace(/\\/g, '/');
}
/**
* Returns a volo command that is wired up to use the given
* directory to generate a manifest.
*
* @param {Object} options, where the allowed options are:
*
* @param {Array} depends: a set of volo commands this command should
* depend on. Default is empty.
*
* @param {String} dir the directory that has the contents that should
* be included in the manifest. Default is 'www-built'
*
* @param {Sting} htmlPath the path to the file inside the "dir" directory
* that is an HTML file that should get the "manifest" attribute inserted in
* its HTML tag. Default is 'index.html'.
*
* @param {String} manifestTemplate the path to a template to use for the
* manifest. Defaults to the manifest.template in the directory housing
* this module. Note that the template contains some tokens replaced
* by this command.
*
* @param {RegExp or Function} a regular expression (function) matching
* (returning true) additional files to exclude from the appcache.
*
* @return {Object} The volo command.
*/
module.exports = function (options) {
//Set up defaults
var dir = options.dir || 'www-built',
htmlPath = options.htmlPath || 'index.html',
manifestTemplate = options.manifestTemplate ||
__dirname + '/manifest.template',
trailingChar = dir.charAt(dir.length - 1);
//Make sure dir does not have a trailing slash
if (trailingChar === '/' || trailingChar === '\\') {
dir = dir.substring(0, dir.length - 1);
}
//Return an object that conforms to the volo command API.
return {
summary: 'Generates the manifest.appcache file for ' + dir + ' and ' +
'modifies ' + htmlPath + ' to add the "manifest" attribute to ' +
'the <html> tag.',
depends: options.depends,
run: function (d, v, namedArgs) {
if (!v.exists(dir)) {
d.reject(dir + ' does not exist');
return;
}
try {
var q = v.require('q'),
manifest = v.read(manifestTemplate),
master = v.read(dir + '/' + htmlPath),
fullFilePaths,
appFiles;
fullFilePaths = v.getFilteredFileList(dir, null, /manifest\.appcache/);
appFiles = fullFilePaths.map(function (file) {
file = frontSlash(file);
var parts, part,
start = file.indexOf('/' + dir + '/');
start = (start !== -1) ? (start + 11) : 0;
file = file.substr(start, file.length);
//Weed out files that start with a dot, as they are most
//likely hidden files not to cache.
//In particular, .htacccess
parts = file.split('/');
part = parts[parts.length - 1];
if (file === 'manifest.appcache' ||
(part && part.indexOf('.') === 0)) {
return '';
} else {
return file;
}
}).filter(function (file) {
//Filter out files removed.
return !!file;
});
if (options.exclude) {
// filter out additional files, with a function or regexp
appFiles = appFiles.filter(function(file) {
if (typeof(options.exclude) === 'function') {
return !(options.exclude(file));
} else {
return !options.exclude.test(file);
}
});
}
//If the html file does not already have a manifest attribute,
//add it. Allow for multiple html tags via IE conditional
//comments
if (!manifestAttrRegExp.test(master)) {
master = master
.replace(/<html\s?/g, '<html manifest="manifest.appcache" ')
.replace(/manifest\.appcache"\s>/g, 'manifest.appcache">');
v.write(dir + '/' + htmlPath, master);
}
generateDigest(q, fullFilePaths, dir).then(function (stamp) {
manifest = v.template(manifest, {
files : appFiles.join('\n'),
stamp : stamp
});
v.write(dir + '/manifest.appcache', manifest);
}).then(d.resolve, d.reject);
} catch (e) {
d.reject(e);
}
}
};
};