forked from eberlitz/splashicon-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
491 lines (458 loc) · 15.2 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
var fs = require('fs');
var path = require('path');
var xml2js = require('xml2js');
var ig = require('imagemagick');
var colors = require('colors');
var _ = require('underscore');
var Q = require('q');
var nodeFs = require('node-fs');
/**
* Check which platforms are added to the project and return their icon names and sized
*
* @param {String} projectName
* @return {Promise} resolves with an array of platforms
*/
var getPlatformIcons = function() {
var deferred = Q.defer();
var platforms = [];
//ok
platforms.push({
name: 'ios',
isAdded: true,
iconsPath: 'res/icons/ios/',
icons: [
// Default icon (avoid Apple submit error ITMS-90032)
{ name: "../../../icon.png", size: 57 },
// iOS 8.0+, iPhone 6 Plus
{ name:'icon-60-3x.png', size:180 },
// iOS 7.0+, iPhone / iPod Touch -->
{ name:'icon-60.png', size:60 },
{ name:'icon-60-2x.png', size:120 },
// iPad -->
{ name:'icon-76.png', size:76 },
{ name:'icon-76-2x.png', size:152 },
// iOS 6.1, Spotlight Icon -->
{ name:'icon-40.png', size:40 },
{ name:'icon-40-2x.png', size:80 },
// iPhone / iPod Touch -->
{ name:'icon-57.png', size:57 },
{ name:'icon-57-2x.png', size:114 },
// iPad -->
{ name:'icon-72.png', size:72 },
{ name:'icon-72-2x.png', size:144 },
// iPhone Spotlight and Settings Icon -->
{ name:'icon-small.png', size:29 },
{ name:'icon-small-2x.png', size:58 },
// iPad Spotlight and Settings Icon -->
{ name:'icon-50.png', size:50 },
{ name:'icon-50-2x.png', size:100 }
]
});
//ok
platforms.push({
name: 'android',
iconsPath: 'res/icons/android/',
isAdded: true,
icons: [
{ name:'icon-36-ldpi.png', size: 36, density:'ldpi' },
{ name:'icon-48-mdpi.png', size: 48, density:'mdpi' },
{ name:'icon-72-hdpi.png', size: 72, density:'hdpi' },
{ name:'icon-96-xhdpi.png', size: 96, density:'xhdpi' },
{ name:'icon-144-xxhdpi.png', size: 144, density:'xxhdpi' },
{ name:'icon-192-xxxhdpi.png', size: 192, density:'xxxhdpi' },
]
});
//ok
platforms.push({
name: 'wp8',
iconsPath: 'res/icons/wp8/',
isAdded: true,
icons: [
{ name: 'ApplicationIcon.png', size: 99 },
{ name: 'Background.png', size: 159 },
]
});
//ok
platforms.push({
name: 'windows',
iconsPath: 'res/icons/windows/',
isAdded: true,
icons: [
{ name:'Square150x150Logo.scale-100.png', size:150 },
{ name:'Square150x150Logo.scale-240.png', size:360 },
{ name:'Square30x30Logo.scale-100.png', size:30 },
{ name:'Square310x310Logo.scale-100.png', size:310 },
{ name:'Square44x44Logo.scale-240.png', size:106 },
{ name:'Square70x70Logo.scale-100.png', size:70 },
{ name:'Square71x71Logo.scale-240.png', size:170 },
{ name:'StoreLogo.scale-100.png', size:50 },
{ name:'StoreLogo.scale-240.png', size:120 },
{ name:'Wide310x150Logo.scale-100.png', width:310, height:150 },
{ name:'Wide310x150Logo.scale-240.png', width:744, height:360 }
]
});
//ok
platforms.push({
name: 'store',
iconsPath: 'store/icons/',
isAdded: true,
icons: [
{ path:'./android/', name: "icon-512.png", size: 512 },
{ path:'./wp8/', name: "icon-300.png", size: 300 },
{ path:'./ios/', name: "icon-1024.jpg", size: 1024 } // App Store
]
});
platforms = settings.ICON_PLATFORMS || platforms
deferred.resolve(platforms);
return deferred.promise;
};
/**
* Check which platforms are added to the project and return their splash screen names and sizes
*
* @param {String} projectName
* @return {Promise} resolves with an array of platforms
*/
var getPlatformSplashs = function() {
var deferred = Q.defer();
var platforms = [];
//ok
platforms.push({
name: 'ios',
isAdded: true,
splashPath: 'res/screens/ios/',
splash: [
{ name:"screen-iphone-portrait.png", width:320, height:480 }, // iPhone
{ name:"screen-iphone-portrait-2x.png", width:640, height:960 }, // iPhone Retina
{ name:"screen-ipad-portrait.png", width:768, height:1024 }, // iPad
{ name:"screen-ipad-portrait-2x.png", width:1536, height:2048 }, // iPad Retina
{ name:"screen-ipad-landscape.png", width:1024, height:768 }, // iPad
{ name:"screen-ipad-landscape-2x.png", width:2048, height:1536 }, // iPad Retina
{ name:"screen-iphone-568h-2x.png", width:640, height:1136 }, // iPhone 5 Retina
{ name:"screen-iphone-portrait-667h.png", width:750, height:1334 }, // iPhone 6
{ name:"screen-iphone-portrait-736h.png", width:1242, height:2208 }, // iPhone 6 Plus
{ name:"screen-iphone-landscape-736h.png", width:2208, height:1242 }, // iPhone 6 Plus
]
});
//ok
platforms.push({
name: 'android',
isAdded: true,
splashPath: 'res/screens/android/',
splash: [
{ name:"screen-ldpi-portrait.png", width: 320, height: 426, density:"port-ldpi" }, // 200x320
{ name:"screen-ldpi-landscape.png", width: 426, height: 320, density:"land-ldpi" }, // 320x200
{ name:"screen-hdpi-portrait.png", width: 480, height: 640, density:"port-hdpi" }, // 320x480
{ name:"screen-hdpi-landscape.png", width: 640, height: 480, density:"land-hdpi" }, // 480x320
{ name:"screen-mdpi-portrait.png", width: 320, height: 470, density:"port-mdpi" }, // 480x800
{ name:"screen-mdpi-landscape.png", width: 470, height: 320, density:"land-mdpi" }, // 800x480
{ name:"screen-xhdpi-portrait.png", width: 720, height: 960, density:"port-xhdpi" }, // 720x1280
{ name:"screen-xhdpi-landscape.png", width: 960, height: 720, density:"land-xhdpi" } // 1280x720
]
});
//ok
// https://msdn.microsoft.com/en-us/library/windows/apps/ff769511(v=vs.105).aspx
platforms.push({
name: 'wp8',
isAdded: true,
splashPath: 'res/screens/wp8/',
splash: [
{ width: 768, height: 1280, name:"SplashScreenImage.jpg" },
{ width: 480, height: 800, name:"SplashScreenImage.screen-WVGA.jpg" },
{ width: 768, height: 1280, name:"SplashScreenImage.screen-WXGA.jpg" },
{ width: 720, height: 1280, name:"SplashScreenImage.screen-720p.jpg" }
]
});
//ok
platforms.push({
name: 'windows',
isAdded: true,
splashPath: 'res/screens/windows/',
splash: [
{ width:620, height:300, name:"SplashScreen.scale-100.png" },
{ width:1152, height:1920, name:"SplashScreen.scale-240.png" },
{ width:1152, height:1920, name:"SplashScreenPhone.scale-240.png" },
]
});
platforms.push({
name: 'store',
isAdded: true,
splashPath: 'store/screens/',
splash: [
{ path: './android/', name: 'cover-store.png', width: 1024, height: 500 },
]
});
platforms = settings.SPLASH_PLATFORMS || platforms
deferred.resolve(platforms);
return deferred.promise;
};
/**
* @var {Object} settings - names of the confix file and of the icon image
* TODO: add option to get these values as CLI params
*/
var settings = {};
settings.ICON_FILE = path.join('model', 'icon.png');
settings.SPLASH_FILE = path.join('model', 'splash.png');
/**
* @var {Object} console utils
*/
var display = {};
display.success = function(str) {
str = '✓ '.green + str;
console.log(' ' + str);
};
display.error = function(str) {
str = '✗ '.red + str;
console.log(' ' + str);
};
display.header = function(str) {
console.log('');
console.log(' ' + str.cyan.underline);
console.log('');
};
/**
* Resizes and creates a new icon in the platform's folder.
*
* @param {Object} platform
* @param {Object} icon
* @return {Promise}
*/
var generateIcon = function(platform, icon) {
var deferred = Q.defer();
try {
var filePath = path.join(platform.iconsPath, icon.name);
var filedirName = path.dirname(filePath);
if (!fs.existsSync(filedirName)) {
nodeFs.mkdirSync(filedirName, '0777', true);
}
if (icon.size) {
ig.resize({
srcPath: settings.ICON_FILE,
dstPath: filePath,
quality: 1,
format: icon.name.replace(/.*\.(\w+)$/i, '$1').toLowerCase(),
width: icon.size,
height: icon.size
}, function(err, stdout, stderr) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
display.success(icon.name + ' created');
}
});
} else {
ig.crop({
srcPath: settings.ICON_FILE,
dstPath: filePath,
quality: 1,
format: icon.name.replace(/.*\.(\w+)$/i, '$1').toLowerCase(),
width: icon.width,
height: icon.height
}, function(err, stdout, stderr) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
display.success(icon.name + ' created');
}
});
}
} catch (error) {
deferred.reject(err);
}
return deferred.promise;
};
/**
* Generates icons based on the platform object
*
* @param {Object} platform
* @return {Promise}
*/
var generateIconsForPlatform = function(platform) {
var deferred = Q.defer();
display.header('Generating Icons for ' + platform.name);
var all = [];
var icons = platform.icons;
icons.forEach(function(icon) {
all.push(generateIcon(platform, icon));
});
Q.all(all).then(function() {
deferred.resolve();
}).catch(function(err) {
console.log(err);
});
return deferred.promise;
};
/**
* Goes over all the platforms and triggers icon generation
*
* @param {Array} platforms
* @return {Promise}
*/
var generateIcons = function(platforms) {
var deferred = Q.defer();
var sequence = Q();
var all = [];
_(platforms).where({
isAdded: true
}).forEach(function(platform) {
sequence = sequence.then(function() {
return generateIconsForPlatform(platform);
});
all.push(sequence);
});
Q.all(all).then(function() {
deferred.resolve();
});
return deferred.promise;
};
/**
* Checks if a valid icon file exists
*
* @return {Promise} resolves if exists, rejects otherwise
*/
var validIconExists = function() {
var deferred = Q.defer();
fs.exists(settings.ICON_FILE, function(exists) {
if (exists) {
display.success(settings.ICON_FILE + ' exists');
deferred.resolve(true);
} else {
display.error(settings.ICON_FILE + ' does not exist in the root folder');
deferred.resolve(false);
}
});
return deferred.promise;
};
/**
* Crops and creates a new splash in the platform's folder.
*
* @param {Object} platform
* @param {Object} splash
* @return {Promise}
*/
var generateSplash = function(platform, splash) {
var deferred = Q.defer();
try {
var filePath = path.join(platform.splashPath, splash.name);
var filedirName = path.dirname(filePath);
if (!fs.existsSync(filedirName)) {
nodeFs.mkdirSync(filedirName, '0777', true);
}
ig.crop({
srcPath: settings.SPLASH_FILE,
dstPath: filePath,
quality: 1,
format: splash.name.replace(/.*\.(\w+)$/i, '$1').toLowerCase(),
width: splash.width,
height: splash.height,
}, function(err, stdout, stderr) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
display.success(splash.name + ' created');
}
});
} catch (error) {
deferred.reject(err);
}
return deferred.promise;
};
/**
* Generates splash based on the platform object
*
* @param {Object} platform
* @return {Promise}
*/
var generateSplashForPlatform = function(platform) {
var deferred = Q.defer();
display.header('Generating splash screen for ' + platform.name);
var all = [];
var splashes = platform.splash;
splashes.forEach(function(splash) {
all.push(generateSplash(platform, splash));
});
Q.all(all).then(function() {
deferred.resolve();
}).catch(function(err) {
console.log(err);
});
return deferred.promise;
};
/**
* Goes over all the platforms and triggers splash screen generation
*
* @param {Array} platforms
* @return {Promise}
*/
var generateSplashes = function(platforms) {
var deferred = Q.defer();
var sequence = Q();
var all = [];
_(platforms).where({
isAdded: true
}).forEach(function(platform) {
sequence = sequence.then(function() {
return generateSplashForPlatform(platform);
});
all.push(sequence);
});
Q.all(all).then(function() {
deferred.resolve();
});
return deferred.promise;
};
/**
* Checks if a valid splash file exists
*
* @return {Promise} resolves if exists, rejects otherwise
*/
var validSplashExists = function() {
var deferred = Q.defer();
fs.exists(settings.SPLASH_FILE, function(exists) {
if (exists) {
display.success(settings.SPLASH_FILE + ' exists');
deferred.resolve(true);
} else {
display.error(settings.SPLASH_FILE + ' does not exist in the root folder');
deferred.resolve(false);
}
});
return deferred.promise;
};
function generate(options) {
settings = options || settings;
display.header('Checking Splash & Icon');
return Q.all([validIconExists(), validSplashExists()])
.then(function(results) {
var hasIcon = results[0] === true;
var hasSplash = results[1] === true;
var promise;
if (!hasIcon && !hasSplash) {
//console.log(arguments);
promise = Q.reject();
}
if (hasIcon) {
promise = Q.when(promise)
.then(getPlatformIcons)
.then(generateIcons);
}
if (hasSplash) {
promise = Q.when(promise)
.then(getPlatformSplashs)
.then(generateSplashes);
}
return Q.when(promise);
})
.catch(function(err) {
if (err) {
console.log(err);
}
}).then(function() {
console.log('');
});
}
module.exports = {
generate: generate
};