-
Notifications
You must be signed in to change notification settings - Fork 13
/
generate.js
71 lines (61 loc) · 2.72 KB
/
generate.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
const fse = require('fs-extra');
const jimp = require('jimp');
const glob = require('glob');
const path = require('path');
function ensureTrailingSlash(path) {
return (path.endsWith('/') === false) ? path + "/" : path;
}
function resizeAndSave(factor, source, target) {
jimp.read(source, function (err, image) {
image.resize(image.bitmap.width * factor, jimp.AUTO);
image.write(target, err => {
if (err) {
console.error(target + " ERROR: " + error);
} else {
console.log(target + " generated");
}
});
});
}
function generate(files, targetAndroid, targetIos) {
for (let file of files) {
let filename = path.basename(file);
let filetype = filename.substr(filename.lastIndexOf('.'), filename.length);
filename = filename.substr(0, filename.lastIndexOf('.'));
if (filename.indexOf("@3x")!==-1) {
filename = filename.substr(0, filename.lastIndexOf('@'));
}
console.info("process " + filename + filetype);
if (typeof targetAndroid !== "undefined") {
resizeAndSave(0.5, file, targetAndroid + 'drawable-hdpi/' + filename.toLowerCase() + filetype);
resizeAndSave(0.25, file, targetAndroid + 'drawable-ldpi/' + filename.toLowerCase() + filetype);
resizeAndSave(1 / 3, file, targetAndroid + 'drawable-mdpi/' + filename.toLowerCase() + filetype);
resizeAndSave(2 / 3, file, targetAndroid + 'drawable-xhdpi/' + filename.toLowerCase() + filetype);
resizeAndSave(1, file, targetAndroid + 'drawable-xxhdpi/' + filename.toLowerCase() + filetype);
}
if (typeof targetIos !== "undefined") {
resizeAndSave(1/3, file, targetIos + filename + filetype);
resizeAndSave(2/3, file, targetIos + filename + '@2x' + filetype);
resizeAndSave(1, file, targetIos + filename + '@3x' + filetype);
}
}
}
module.exports = (q, targetAndroid, targetIos) => {
if (typeof targetAndroid !== "undefined") {
targetAndroid = ensureTrailingSlash(targetAndroid);
fse.ensureDirSync(targetAndroid + 'drawable-hdpi');
fse.ensureDirSync(targetAndroid + 'drawable-ldpi');
fse.ensureDirSync(targetAndroid + 'drawable-mdpi');
fse.ensureDirSync(targetAndroid + 'drawable-xhdpi');
fse.ensureDirSync(targetAndroid + 'drawable-xxhdpi');
}
if (typeof targetIos !== "undefined") {
targetIos = ensureTrailingSlash(targetIos);
fse.ensureDirSync(targetIos);
}
if (q.length == 1 && q[0].indexOf("*") !== -1) {
glob(q[0], {}, (er, files) => generate(files, targetAndroid, targetIos));
} else {
generate(q, targetAndroid, targetIos);
}
}