-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathicons-to-blob.js
133 lines (129 loc) · 4.17 KB
/
icons-to-blob.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
#!/usr/bin/env node
// @ts-check
"use strict";
/**
* @typedef {import("./dependencies").Buffer} Buffer
* @typedef { {
* data: Buffer; width: number; height: number;
* } } PNGImage
*/
var allIcons = [
{ 19: "/icons/enabled_19.png", 38: "/icons/enabled_38.png" },
{ 19: "/icons/partial_19.png", 38: "/icons/partial_38.png" },
{ 19: "/icons/disabled_19.png", 38: "/icons/disabled_38.png" }
];
/** @type {import("./dependencies").FileSystem} */
// @ts-ignore
var fs = require("fs");
/** @type {import("./dependencies").ProcessType} */
// @ts-ignore
var process = require("process");
try {
var lib = require("./dependencies");
} catch (ex) { lib = null; }
var destRoot = +(process.env["BUILD_NDEBUG"] || 0) > 0 ? "dist/" : process.env["LOCAL_DIST"] || "";
var srcRoot = process.cwd();
{
var cwd = srcRoot.split(require("path").sep).slice(-1)[0];
if ("background content front lib pages".split(" ").includes(cwd)) {
srcRoot = srcRoot + "/..";
}
}
destRoot = require("path").resolve(destRoot || srcRoot);
/**
* Convert a single image
* @param {string} src - path of image source file
* @returns {Promise<PNGImage>}
*/
function readPNGImage(src) {
return new Promise((resolve, reject) => {
var PNG = require("pngjs").PNG;
fs.createReadStream(src).pipe(new PNG({
filterType: 6
})).on('parsed', function () {
resolve(this);
}).on('error', function (err) {
reject(err);
})
});
}
/**
* Convert all image files
* @param {((error?: any) => any) | null} [callback]
* @param {Object} [options]
* @param {Array<{ [size: string]: string }>} [options.icons = allIcons]
* @param {(src: string) => string} [options.getDest]
* @param {(src: string, dest: string) => boolean} [options.checkLatest]
* @param {(message: string, ...params: any[]) => void} [options.print = console.log]
* @returns {Promise<void>}
*/
function main(callback = null
, {
icons = allIcons,
getDest,
checkLatest = lib && lib.compareFileTime,
print = console.log
} = {}) {
let consumed = 0;
const didFinish = () => {
let local_cb = callback;
callback = null;
local_cb ? local_cb()
// @ts-ignore
: consumed <= 0 && typeof require === "function" && require.main === module
&& process.argv.indexOf("-q") > 0 ? void 0
: print("All %d new icons in %d converted.", consumed, totalCount);
};
destRoot = destRoot.replace(/\\/g, "/");
destRoot && destRoot.slice(-1) != "/" && (destRoot += "/");
const totalCount = icons.length, allPromises = [];
for (let i = 0; i < totalCount; i++) {
const submap = icons[i],
sublist = Object.keys(submap).sort().map(i => submap[i].replace(/^\//, ""));
let dest = getDest ? getDest(sublist[0]) : destRoot + sublist[0].split("_", 1)[0] + ".bin"
, islatest = 0;
const srcList = sublist.map(i => srcRoot + "/" + i);
checkLatest && srcList.forEach(filePath => checkLatest(filePath, dest) && islatest++);
if (islatest === srcList.length) { continue; }
const destFolder = dest.split("/").slice(0, -1).join("/");
if (destFolder && !fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder, {recursive: true});
}
allPromises.push(Promise.all(srcList.map(readPNGImage)).then(([img1, img2]) => {
const imagesData = [img1.data, img2.data];
// @ts-ignore
const allBuffer = Buffer.concat(imagesData);
return new Promise((resolve, reject) => {
fs.writeFile(dest, allBuffer, (err) => {
if (err) { return reject(err); }
consumed++;
print("Write binary image data to %s", dest, ", byte[", img1.data.length, "+", img2.data.length, "]");
resolve(dest);
})
})
}));
}
if (allPromises.length <= 0) {
return Promise.resolve(didFinish());
} else {
return Promise.all(allPromises).then(didFinish);
}
}
if (typeof module !== "undefined") {
module.exports = {
allIcons: allIcons,
readPNGImage: readPNGImage,
main: main,
/**
* setup dest root
* @param {string} newRoot - new root of dest files
*/
setDestRoot: function (newRoot) {
destRoot = newRoot;
}
};
}
// @ts-ignore
if (typeof require === "function" && require.main === module) {
main();
}