-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (58 loc) · 1.75 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
let through = require("through2");
let path = require("path");
let PluginError = require("plugin-error");
let texturePacker = require("free-tex-packer-core");
let appInfo = require("./package.json");
function fixPath(path) {
return path.split("\\").join("/");
}
function getExtFromPath(path) {
return path.split(".").pop().toLowerCase();
}
function getError(txt) {
return new PluginError(appInfo.name, txt)
}
const SUPPORTED_EXT = ["png", "jpg", "jpeg"];
module.exports = function (options) {
let files = [];
let firstFile = null;
function bufferContents(file, enc, cb) {
if (file.isNull()) {
cb();
return;
}
if (file.isStream()) {
cb(getError("Streaming not supported"));
return;
}
if (!firstFile) firstFile = file;
if (SUPPORTED_EXT.indexOf(getExtFromPath(file.relative)) < 0) {
cb();
return;
}
files.push({ path: fixPath(file.relative), contents: file.contents });
cb();
}
function endStream(cb) {
if (!files.length) {
cb();
return;
}
if (!options) options = {};
options.appInfo = appInfo;
texturePacker(files, options, (files, error) => {
if (error) {
cb(getError(error.message || error.description || "Unknown error"));
return;
}
for (let item of files) {
let file = firstFile.clone({ contents: false });
file.path = path.join(firstFile.base, item.name);
file.contents = item.buffer;
this.push(file);
}
cb();
});
}
return through.obj(bufferContents, endStream);
};