-
Notifications
You must be signed in to change notification settings - Fork 143
/
pack.js
244 lines (216 loc) · 7.93 KB
/
pack.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// simple node.js script to pack the files making up WebToEpub into single file
"use strict";
var fs = require("fs");
var JSZip = require("../plugin/jszip/dist/jszip.min.js");
var DOMParser = require("@xmldom/xmldom").DOMParser;
var extractFileListFromHtml = function(htmlAsString) {
let dom = new DOMParser().parseFromString(htmlAsString, "text/html");
if (dom != null) {
return Array.from(dom.getElementsByTagName("script"))
.map(e => e.getAttribute("src"));
}
return [];
}
var getFileList = function(fileName) {
return readFilePromise(fileName).then(function(data) {
return extractFileListFromHtml(data.toString());
});
}
var adjustedFileListForEslint = function(fileList) {
return fileList
.filter(e => e !== "jszip/dist/jszip.min.js")
.map(f => "../plugin/" + f);
}
// wrap readFile in a promise
var readFilePromise = function(fileName) {
return new Promise(function(resolve, reject) {
console.log("reading file: " + fileName);
fs.readFile(fileName, function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
var writeFilePromise = function(fileName, buffer) {
return new Promise(function(resolve, reject) {
console.log("writing file: " + fileName);
fs.writeFile(fileName, new Buffer(buffer), function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
// package geting all the files
var readAllFiles = function(fileList, loadedFiles) {
return fileList.reduce(function(sequence, fileName) {
return sequence.then(function () {
return readFilePromise(fileName);
}).then(function(data) {
console.log("saving file: " + fileName);
loadedFiles.push({
fileName: fileName,
text: data.toString()
});
});
}, Promise.resolve());
}
var countLines = function(fileText) {
return fileText.split("\n").filter(s => s != "").length;
}
var makeIndexLine = function(fileName, startIndex, count) {
return "\"" + fileName + "\", " + (startIndex + 1) + ", " + (startIndex + count) + "\r\n";
}
//=================================================================
// pack source into packed.js So its easy to be examined with eslint
// just run eslint against packed.js
var loadedFiles = [];
getFileList("../plugin/popup.html").then(function(fileList) {
fileList = adjustedFileListForEslint(fileList);
console.log(fileList);
return readAllFiles(fileList, loadedFiles);
}).then(function (data) {
let temp = "";
let lineCount = 0;
let index = "";
for(let f of loadedFiles) {
temp += f.text;
let count = countLines(f.text);
index += makeIndexLine(f.fileName, lineCount, count);
lineCount += count;
}
fs.writeFileSync("packed.js", temp);
fs.writeFileSync("index.csv", index);
}).catch(function (err) {
console.log(err);
});
//=================================================================
// This is bit where we pack the extension into a zip & xpi files.
var addToZipFile = function(zip, nameInZip, filePath) {
return readFilePromise(filePath).then(function (data) {
zip.file(nameInZip, data.toString());
});
}
var writeZipToDisk = function(zip, filePath) {
console.log("writeZipToDisk");
return zip.generateAsync({type:"uint8array", compression: "DEFLATE"}).then(function (arraybuffer) {
return writeFilePromise(filePath, arraybuffer);
});
}
var addFilesToZip = function(zip, fileList) {
return fileList.reduce(function(sequence, fileName) {
return sequence.then(function () {
return addToZipFile(zip, fileName, "../plugin/" + fileName);
});
}, Promise.resolve());
}
var getLocaleFilesNames = function() {
return new Promise(function(resolve, reject) {
fs.readdir("../plugin/_locales", function (err, files) {
if (err) {
reject(err);
} else {
resolve(files.map(f => "_locales/" + f + "/messages.json"));
}
});
});
}
var addPopupHtmlToZip = function(zip) {
return readFilePromise("../plugin/popup.html")
.then(function (data) {
let htmlAsString = data.toString()
.split("\r")
.filter(s => !s.includes("/experimental/"))
.join("\r");
zip.file("popup.html", htmlAsString);
})
}
var addBinaryFileToZip = function(zip, fileName, nameInZip) {
return readFilePromise(fileName)
.then(function(data) {
zip.file(nameInZip, data);
});
}
var addImageFileToZip = function(zip, fileName) {
let dest = "images/" + fileName;
return addBinaryFileToZip(zip, "../plugin/" + dest, dest);
}
var addCssFileToZip = function(zip, fileName) {
let dest = "css/" + fileName;
return addBinaryFileToZip(zip, "../plugin/" + dest, dest);
}
var packNonManifestExtensionFiles = function(zip, packedFileName) {
return addBinaryFileToZip(zip, "../plugin/book128.png", "book128.png")
.then(function () {
return addImageFileToZip(zip, "ChapterStateDownloading.svg");
}).then(function () {
return addImageFileToZip(zip, "ChapterStateLoaded.svg");
}).then(function () {
return addImageFileToZip(zip, "ChapterStateNone.svg");
}).then(function () {
return addImageFileToZip(zip, "ChapterStateSleeping.svg");
}).then(function () {
return addCssFileToZip(zip, "default.css");
}).then(function () {
return addCssFileToZip(zip, "alwaysDark.css");
}).then(function () {
return addCssFileToZip(zip, "autoDark.css");
}).then(function () {
return getFileList("../plugin/popup.html");
}).then(function(fileList) {
return getLocaleFilesNames().then(function(localeNames) {
return ["js/ContentScript.js"].concat(localeNames)
.concat(fileList.filter(n => !n.includes("/experimental/")));
});
}).then(function (fileList) {
return addFilesToZip(zip, fileList);
}).then(function () {
return addPopupHtmlToZip(zip);
}).then(function() {
return writeZipToDisk(zip, packedFileName);
}).then(function() {
console.log("Wrote Zip to disk");
}).catch(function (err) {
console.log(err);
});
}
var makeManifestForFirefox = function(data) {
let manifest = JSON.parse(data.toString());
delete(manifest.incognito);
manifest.manifest_version = 2;
// fix permissions/host_permissions
let permissions = manifest.permissions;
permissions = permissions.filter(p => p != "scripting");
manifest.permissions = permissions.concat(manifest.host_permissions);
delete manifest.host_permissions;
// rename action => browser_action
manifest.browser_action = manifest.action;
delete manifest.action;
return manifest;
}
var makeManifestForChrome = function(data) {
let manifest = JSON.parse(data.toString());
delete(manifest.browser_specific_settings);
delete(manifest.action.browser_style);
manifest.permissions = manifest.permissions
.filter(p => !p.startsWith("webRequest"));
return manifest;
}
var packExtension = function(manifest, fileExtension) {
let zip = new JSZip();
zip.file("manifest.json", JSON.stringify(manifest));
return packNonManifestExtensionFiles(zip, "WebToEpub" + manifest.version + fileExtension);
}
// pack the extensions for Chrome and firefox
readFilePromise("../plugin/manifest.json")
.then(function (data) {
packExtension(makeManifestForFirefox(data), ".xpi");
packExtension(makeManifestForChrome(data), ".zip");
}).catch(function (err) {
console.log(err);
});