forked from radiovisual/icon-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icongen.js
executable file
·110 lines (87 loc) · 3.73 KB
/
icongen.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
#!/usr/bin/env node
var lwip = require('lwip');
var chalk = require('chalk');
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));
var fs = require("fs");
var Promise = require('promise');
var mkdirp = require('mkdirp');
var iconGenPackage = require(path.join(__dirname, "package.json"));
var iconSizes = require(path.join(__dirname, "icon-sizes.json"));
if (!Object.keys(iconSizes)) {
reportError("invalid json in icon-sizes.json");
process.exit(1);
}
var platforms = argv._;
if(!platforms.length) {
platforms = ['android', 'other'];
}
var source_image = argv.s;
var output_destination = argv.o || "";
outputSessionInformation(platforms, source_image, output_destination);
// save the platforms in a que so that
// we can process the images in order
var platform_que = [];
var index = 0;
// Which platforms will we output?
platforms.forEach(function(platform){
var platformConfig = iconSizes[platform.toLowerCase()];
platformConfig && platformConfig.forEach(function(value){ platform_que.push(value); });
});
function processQueObject(queObject){
lwip.open(source_image, function(err, image) {
if (err) throw err;
var filename = queObject.filename + path.extname(source_image);
var folder = queObject.folder;
var fullpath = path.join(output_destination, folder, filename);
var dirpath = path.join(output_destination,folder);
mkdirp(dirpath, function (err) {
if (err) {
reportError("Cannot make directory: "+dirpath);
console.error(err);
process.exit(1);
} else {
var width = queObject.width;
var height = queObject.hasOwnProperty("height") ? queObject.height : width;
image.batch()
.resize(width, height)
.writeFile(fullpath, function(err) {
if (err) {
console.log(chalk.red(" [x] ") + chalk.red(fullpath));
console.log(err.message);
};
console.log(chalk.green(" [x] ") + chalk.white(fullpath));
index++;
if (index < platform_que.length){
processQueObject(platform_que[index]);
} else {
console.log();
console.log( chalk.gray("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"));
console.log( chalk.magenta("COMPLETE: ") + chalk.white("processed "+index+" icons"));
console.log( chalk.gray("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"));
process.exit(0);
}
});
}
});
});
}
// start the que with the first cue item
processQueObject(platform_que[index]);
function reportError(msg){
console.log();
var m = chalk.white.bgRed(' ERROR: ') + " " +chalk.white.bold.bgBlack(msg);
console.log(m);
console.log();
}
function outputSessionInformation(platforms, source_image, output_destination){
var _output = output_destination === "" ? "./ (current directory)" : output_destination;
console.log();
console.log(chalk.gray("Icon Generator version ", iconGenPackage.version, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"));
//console.log(chalk.gray());
console.log(chalk.magenta("Creating icons for the following platforms:")+ " "+chalk.white(platforms.toString()));
console.log(chalk.cyan("Using source image: ")+source_image);
console.log(chalk.cyan("Saving icons to: ") + " "+ _output);
console.log(chalk.gray("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"));
console.log();
}