-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpack.js
59 lines (56 loc) · 1.8 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
module.exports = function() {
var fs = require("fs");
var opts = arguments[0];
var callback = arguments[1];
var inputFolder = opts.input;
var outputFile = opts.output;
var list = { "meta": [0, 0] };
var count = 0;
var size = 0;
var MIME_TYPES = {
"mp3": "audio/mpeg",
"m4a": "audio/mp4",
"mp4": "audio/mp4",
"mpg": "audio/mpeg",
"mpeg": "audio/mpeg",
"mpga": "audio/mpeg",
"mp1": "audio/mpeg",
"mp2": "audio/mpeg",
"aac": "audio/aac",
"ogg": "audio/ogg",
"oga": "audio/ogg",
"wav": "audio/wav",
"webm": "audio/webm",
"weba": "audio/webm",
"aif": "audio/x-aiff",
"aifc": "audio/x-aiff",
"aiff": "audio/x-aiff",
"mid": "audio/midi",
"midi": "audio/midi"
};
listFiles(inputFolder);
list["meta"] = [count, size];
fs.writeFileSync(outputFile, JSON.stringify(list));
function listFiles(folder) {
try {
var files = fs.readdirSync(folder);
files.forEach(function (file) {
var ext = file.split(".").pop();
var filePath = folder + "/" + file;
var stats = fs.statSync(filePath);
if (stats.isDirectory()) listFiles(filePath);
else {
var data = fs.readFileSync(filePath, "base64");
if(MIME_TYPES[ext]) {
list[opts.noBasePath ? filePath.replace(inputFolder + "/", "") : filePath] = "data:" + MIME_TYPES[ext] + ";base64," + data;
count++;
size += stats.size;
}
}
});
}
catch (e) {
callback("failed with exception: " + e.message);
}
}
}