-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
150 lines (132 loc) · 3.35 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
const cli = require('cli');
const fs = require('fs');
const mime = require('mime-types');
const path = require('path');
let debug = false;
function packer(options) {
if(!options.source) {
cli.error("No source parameter specified");
return;
}
if(!fs.existsSync(options.source)) {
cli.error("Source directory \"" + options.source + "\" doesn't exist");
return;
}
if(!options.output) {
cli.error("No output parameter specified");
return;
}
if(!fs.existsSync(options.output)) {
cli.error("Output directory \"" + options.output + "\" doesn't exist");
return;
}
const source = addTrailingSlash(options.source);
const output = addTrailingSlash(options.output);
const name = options.name || "pack";
debug = options.debug || false;
options.mimeTypes = options.mimeTypes || [];
printDebug("Source: " + source);
printDebug("Output: " + output);
printDebug("Name: " + name);
const files = listFiles(source);
// printDebug(files);
pack(files, source, output, name, options.mimeTypes);
}
function addTrailingSlash(str) {
if(str.substr(-1) == "/") {
return str;
}
return str + "/";
}
function listFiles(dir, fileList = []) {
const files = fs.readdirSync(dir);
for(let i = 0, l = files.length; i < l; i++) {
const file = files[i];
const currentFile = dir + file;
if(fs.statSync(currentFile).isDirectory()) {
listFiles(currentFile + "/", fileList);
} else if(file.substr(0, 1) != ".") {
fileList.push(currentFile);
}
}
return fileList;
}
function pack(files, source, output, name, mimeTypes) {
printDebug("Packing:");
const buffers = [];
const datas = [];
let p = 0;
for(let i = 0, l = files.length; i < l; i++) {
const file = files[i];
printDebug(file);
const mimetype = resolveMimetype(file, mimeTypes);
printDebug("- Resolved mime-type: " + mimetype);
const size = fs.statSync(file)["size"];
printDebug("- Size: " + size);
const fileContent = fs.readFileSync(file);
buffers.push(fileContent);
datas.push([file.replace(source, ""), p, p + size, mimetype]);
p += size;
}
fs.writeFileSync(output + name + ".pack", Buffer.concat(buffers));
fs.writeFileSync(output + name + ".json", JSON.stringify(datas));
}
function resolveMimetype(file, mimeTypes) {
let mimetype = mime.lookup(file);
if(mimetype) {
printDebug("- Detected mime-type: " + mimetype);
mimetype = validateMimetype(mimetype, mimeTypes);
} else {
printDebug("- Detected mime-type: None");
const ext = path.extname(file);
switch(ext) {
case ".txt":
case ".obj":
mimetype = "text/plain";
break;
case ".css":
mimetype = "text/css";
break;
case ".twig":
mimetype = "text/twig";
break;
case ".json":
mimetype = "application/json";
break;
case ".dds":
case ".pvr":
case ".glb":
mimetype = "application/octet-stream";
break;
default:
mimetype = "text/plain";
break;
}
}
return mimetype;
}
function validateMimetype(mimetype, mimeTypes) {
const validMimetypes = mimeTypes.concat([
// Text
"text/plain",
// Images
"image/gif", "image/jpeg", "image/png", "image/tiff", "image/webp",
// JSON
"application/json",
// Twig
"text/twig",
// Others non text
"application/octet-stream"
]);
if(validMimetypes.indexOf(mimetype) == -1) {
return "text/plain";
} else {
return mimetype;
}
}
function printDebug(msg) {
if(debug) {
cli.debug(msg);
}
}
module.exports = packer;