-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (74 loc) · 2.77 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
var fs = require('fs');
var cheerio = require('cheerio');
var through = require('through2');
var gulp = require('gulp');
var rename = require('gulp-rename');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
// Consts
var PLUGIN_NAME = 'gulp-app-cache-builder';
function buildAppCache(options) {
var excludeString = options.ExcludeString;
var projectFile = options.ProjectFile;
var excludes = excludeString.split(",");
var contentNames = [];
var newLine = "\n";
var now = new Date();
return through.obj(function (file, encoding, done) {
var $ = cheerio.load(file.contents);
$('Content').each(function (index, contentNode) {
var fileName = "/" + contentNode.attribs["include"].replace(/\\/g, "\/");
var exclude = false;
for (var i = 0; i < excludes.length; i++) {
var ext = excludes[i].toLowerCase();
if (ext.startsWith(".")) {
if (fileName.toLowerCase().endsWith(ext)) {
exclude = true;
break;
}
}
else {
if (fileName.toLowerCase().includes(ext)) {
exclude = true;
break;
}
}
}
if (!exclude) {
contentNames.push(fileName);
}
});
var output = "";
output += "CACHE MANIFEST" + newLine;
output += "# build time " +
(now.getMonth() + 1) + "/" + now.getDate() + "/" + now.getFullYear() + " " +
now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + " " + (now.getHours() <= 12 ? "AM" : "PM") + newLine;
output += "CACHE:" + newLine;
//add the cache section
contentNames.forEach(function (name, index) {
output += name + newLine;
});
//add the network section
//*
output += "NETWORK:" + newLine;
output += "*" + newLine;
output += "FALLBACK:";
file.contents = Buffer(output);
this.push(file);
done();
});
}
// Plugin level function(dealing with files)
function gulpAppCacheBuilder(options) {
if (!options) {
throw new PluginError(PLUGIN_NAME, 'options required!');
}
if (!options || !options.ProjectFile || !fs.existsSync(options.ProjectFile)) {
throw new PluginError(PLUGIN_NAME, 'options.ProjectFile must be a valid csproj file.');
}
return gulp
.src(options.ProjectFile)
.pipe(buildAppCache(options));
}
// Exporting the plugin main function
module.exports = gulpAppCacheBuilder;