forked from olivab/cordova-res-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
354 lines (288 loc) · 10.6 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin / env node
'use strict';
// libs init
var program = require('commander');
var colors = require('colors');
var Q = require('bluebird');
var fs = require('fs-extra');
var path = require('path');
var Jimp = require('jimp');
var _ = require('lodash');
var Gauge = require("gauge");
// helpers
var display = {
info: (str) => {
console.log(str);
},
success: (str) => {
str = ' ' + 'V'.green + ' ' + str;
console.log(str);
},
error: (str) => {
str = ' ' + 'X'.red + ' ' + str;
console.log(str);
},
header: (str) => {
console.log('');
console.log(str);
}
};
// app main variables and constants
const PLATFORMS = {
'android': {
definitions: ['./platforms/icons/android', './platforms/splash/android']
},
'ios': {
definitions: ['./platforms/icons/ios', './platforms/splash/ios']
},
'windows': {
definitions: ['./platforms/icons/windows', './platforms/splash/windows']
},
'blackberry10': {
definitions: ['./platforms/icons/blackberry10']
}
};
var g_imageObjects;
var g_selectedPlatforms = [];
// app functions
function check(settings) {
display.header('Checking files and directories');
return checkPlatforms(settings)
.then((selPlatforms) => g_selectedPlatforms = selPlatforms)
.then(() => getImages(settings))
.then((iobjs) => {
g_imageObjects = iobjs;
})
.then(() => checkOutPutDir(settings));
}
function checkPlatforms(settings) {
var platformsKeys = _.keys(PLATFORMS);
if (!settings.platforms || !Array.isArray(settings.platforms)) {
display.success('Processing files for all platforms');
return Q.resolve(platformsKeys);
}
var platforms = settings.platforms;
var platformsToProcess = [];
var platformsUnknown = [];
platforms.forEach(platform => {
if (_.find(platformsKeys, (p) => platform === p)) {
platformsToProcess.push(platform);
} else {
platformsUnknown.push(platform);
}
});
if (platformsUnknown.length > 0) {
display.error('Bad platforms: ' + platformsUnknown);
return Q.reject('Bad platforms: ' + platformsUnknown);
}
display.success('Processing files for: ' + platformsToProcess);
return Q.resolve(platformsToProcess);
}
function getImages(settings) {
var imageObjects = {
icon: null,
splash: null
};
var promise = Q.resolve();
if (settings.makeicon) {
promise = promise.then(() => checkIconFile(settings.iconfile))
.then((image) => {
imageObjects.icon = image;
});
}
if (settings.makesplash) {
promise = promise.then(() => checkSplashFile(settings.splashfile))
.then((image) => {
imageObjects.splash = image;
});
}
return promise.then(() => {
return imageObjects;
});
function checkIconFile(iconFileName) {
var defer = Q.defer();
Jimp.read(iconFileName)
.then((image) => {
var width = image.bitmap.width;
var height = image.bitmap.height;
if (width === 1024 && width === height) {
display.success('Icon file ok (' + width + 'x' + height + ')');
defer.resolve(image);
} else {
display.error('Bad icon file (' + width + 'x' + height + ')');
defer.reject('Bad image format');
}
})
.catch((err) => {
display.error('Could not load icon file');
defer.reject(err);
});
return defer.promise;
}
function checkSplashFile(splashFileName) {
var defer = Q.defer();
Jimp.read(splashFileName)
.then((image) => {
var width = image.bitmap.width;
var height = image.bitmap.height;
if (width === 2732 && width === height) {
display.success('Splash file ok (' + width + 'x' + height + ')');
defer.resolve(image);
} else {
display.error('Bad splash file (' + width + 'x' + height + ')');
defer.reject('Bad image format');
}
})
.catch((err) => {
display.error('Could not load splash file');
defer.reject(err);
});
return defer.promise;
}
}
function checkOutPutDir(settings) {
var dir = settings.outputdirectory;
return fs.pathExists(dir)
.then((exists) => {
if (exists) {
display.success('Output directory ok (' + dir + ')');
} else {
display.error('Output directory not found (' + dir + ')');
throw ('Output directory not found: ' + dir);
}
});
}
function generateForConfig(imageObj, settings, config) {
var platformPath = path.join(settings.outputdirectory, config.path);
var transformIcon = (definition) => {
var defer = Q.defer();
var image = imageObj.icon.clone();
var outputFilePath = path.join(platformPath, definition.name);
image.resize(definition.size, definition.size)
.write(outputFilePath,
(err) => {
if (err) defer.reject(err);
//display.info('Generated icon file for ' + outputFilePath);
defer.resolve();
});
return defer.promise;
};
var transformSplash = (definition) => {
var defer = Q.defer();
var image = imageObj.splash.clone();
var size_width = image.bitmap.width;
var size_height = image.bitmap.height;
if (definition.width < 1200 || definition.height < 1200) {
var reduce_rate = Math.max(
1200 / definition.width,
1200 / definition.height
);
size_width = Math.round(size_width / reduce_rate);
size_height = Math.round(size_height / reduce_rate);
}
var x = (size_width - definition.width) / 2;
var y = (size_height - definition.height) / 2;
var width = definition.width;
var height = definition.height;
var outputFilePath = path.join(platformPath, definition.name);
image
.resize(size_width, size_height, image.RESIZE_BICUBIC)
.crop(x, y, width, height)
.rgba(false)
.deflateLevel(9)
.deflateStrategy(3)
.write(outputFilePath,
(err) => {
if (err) defer.reject(err);
//display.info('Generated splash file for ' + outputFilePath);
defer.resolve();
});
return defer.promise;
};
return fs.ensureDir(platformPath)
.then(() => {
var definitions = config.definitions;
var sectionName = "Generating " + config.type + ' files for ' + config.platform;
var definitionCount = definitions.length;
var progressIndex = 0;
var gauge = new Gauge();
gauge.show(sectionName, 0);
return Q.mapSeries(definitions, (def) => {
var transformPromise = Q.resolve();
transformPromise = transformPromise.then(() => {
progressIndex++;
var progressRate = progressIndex / definitionCount;
gauge.show(sectionName, progressRate);
gauge.pulse(def.name);
});
switch (config.type) {
case 'icon':
transformPromise = transformPromise.then(() => transformIcon(def));
break;
case 'splash':
transformPromise = transformPromise.then(() => transformSplash(def));
break;
}
return transformPromise;
}).then(() => {
gauge.disable();
display.success('Generated ' + config.type + ' files for ' + config.platform);
}).catch((err) => {
gauge.disable();
throw (err);
});
});
}
function generate(imageObj, settings) {
display.header('Generating files');
var configs = [];
g_selectedPlatforms.forEach((platform) => {
PLATFORMS[platform].definitions.forEach((def) => configs.push(require(def)));
});
var filteredConfigs = _.filter(configs, (config) => {
if (config.type === 'icon' && settings.makeicon) return true;
if (config.type === 'splash' && settings.makesplash) return true;
return false;
});
return Q.mapSeries(filteredConfigs, (config) => {
return generateForConfig(imageObj, settings, config);
})
.then(() => {
//display.success("Successfully generated all files");
});
}
function catchErrors(err) {
if (err)
console.log('Error: ', err);
}
// cli helper configuration
function processList(val) {
return val.split(',');
}
var pjson = require('./package.json');
program
.version(pjson.version)
.description(pjson.description)
.option('-i, --icon [optional]', 'optional icon file path (default: ./resources/icon.png)')
.option('-s, --splash [optional]', 'optional splash file path (default: ./resources/splash.png)')
.option('-p, --platforms [optional]', 'optional platform token comma separated list (default: all platforms processed)', processList)
.option('-o, --outputdir [optional]', 'optional output directory (default: ./resources/)')
.option('-I, --makeicon [optional]', 'option to process icon files only')
.option('-S, --makesplash [optional]', 'option to process splash files only')
.parse(process.argv);
// app settings and default values
var g_settings = {
iconfile: program.icon || path.join('.', 'resources', 'icon.png'),
splashfile: program.splash || path.join('.', 'resources', 'splash.png'),
platforms: program.platforms || undefined,
outputdirectory: program.outputdir || path.join('.', 'resources'),
makeicon: program.makeicon || (!program.makeicon && !program.makesplash) ? true : false,
makesplash: program.makesplash || (!program.makeicon && !program.makesplash) ? true : false
};
// app entry point
console.log("***************************");
console.log("cordova-res-generator " + pjson.version);
console.log("***************************");
check(g_settings)
.then(() => generate(g_imageObjects, g_settings))
.catch((err) => catchErrors(err));