forked from AlexDisler/cordova-icon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
397 lines (375 loc) · 12.5 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
require("colors");
var fs = require("fs-extra");
var path = require("path");
var xml2js = require("xml2js");
var ig = require("imagemagick");
var _ = require("underscore");
var Q = require("q");
var argv = require("minimist")(process.argv.slice(2));
var display = require("./display");
var updateContentsJson = require("./ios_cordova_contents_override");
/**
* @var {Object} settings - names of the config file and of the icon image
*/
var settings = {};
settings.CONFIG_FILE = argv.config || "config.xml";
settings.ICON_FILE = argv.icon || "icon.png";
settings.OLD_XCODE_PATH = argv["xcode-old"] || false;
settings.OLD_ANDROID_PLATFORM = argv["old-android-platform"] || false;
/**
* Check which platforms are added to the project and return their icon names and sizes
*
* @param {String} projectName
* @return {Promise} resolves with an array of platforms
*/
var getPlatforms = function (projectName) {
var deferred = Q.defer();
var platforms = [];
var xcodeFolder = "/Images.xcassets/AppIcon.appiconset/";
var androidMipmapIconName = "ic_launcher.png";
if (settings.OLD_XCODE_PATH) {
xcodeFolder = "/Resources/icons/";
}
if (settings.OLD_ANDROID_PLATFORM) {
androidMipmapIconName = "icon.png";
}
platforms.push({
name: "ios",
isAdded: fs.existsSync("platforms/ios"),
iconsPath: "platforms/ios/" + projectName + xcodeFolder,
icons: [
{ name: "icon-20.png", size: 20 },
{ name: "[email protected]", size: 40 },
{ name: "[email protected]", size: 60 },
{ name: "[email protected]", size: 48 },
{ name: "[email protected]", size: 55 },
{ name: "[email protected]", size: 66 },
{ name: "icon-29.png", size: 29 },
{ name: "[email protected]", size: 58 },
{ name: "[email protected]", size: 87 },
{ name: "icon-40.png", size: 40 },
{ name: "[email protected]", size: 80 },
{ name: "[email protected]", size: 88 },
{ name: "[email protected]", size: 92 },
{ name: "icon-50.png", size: 50 },
{ name: "[email protected]", size: 100 },
{ name: "[email protected]", size: 102 },
{ name: "[email protected]", size: 108 },
{ name: "[email protected]", size: 120 },
{ name: "[email protected]", size: 180 },
{ name: "icon-72.png", size: 72 },
{ name: "[email protected]", size: 144 },
{ name: "icon-76.png", size: 76 },
{ name: "[email protected]", size: 152 },
{ name: "[email protected]", size: 167 },
{ name: "[email protected]", size: 172 },
{ name: "[email protected]", size: 196 },
{ name: "[email protected]", size: 216 },
{ name: "[email protected]", size: 234 },
{ name: "[email protected]", size: 258 },
{ name: "icon-1024.png", size: 1024 },
{ name: "icon.png", size: 57 },
{ name: "[email protected]", size: 114 },
],
extraTask: function () {
return updateContentsJson(projectName);
},
});
const android = {
name: "android",
isAdded: fs.existsSync("platforms/android"),
iconsPath: "platforms/android/app/src/main/res/",
icons: [
{ name: "drawable/icon.png", size: 96 },
{ name: "drawable-hdpi/icon.png", size: 72 },
{ name: "drawable-ldpi/icon.png", size: 36 },
{ name: "drawable-mdpi/icon.png", size: 48 },
{ name: "drawable-xhdpi/icon.png", size: 96 },
{ name: "drawable-xxhdpi/icon.png", size: 144 },
{ name: "drawable-xxxhdpi/icon.png", size: 192 },
{ name: "mipmap-hdpi/" + androidMipmapIconName, size: 72 },
{ name: "mipmap-ldpi/" + androidMipmapIconName, size: 36 },
{ name: "mipmap-mdpi/" + androidMipmapIconName, size: 48 },
{ name: "mipmap-xhdpi/" + androidMipmapIconName, size: 96 },
{ name: "mipmap-xxhdpi/" + androidMipmapIconName, size: 144 },
{ name: "mipmap-xxxhdpi/" + androidMipmapIconName, size: 192 },
],
};
if (!settings.OLD_ANDROID_PLATFORM) {
android.icons.push(
{ name: "mipmap-hdpi-v26/ic_launcher_foreground.png", size: 72 },
{ name: "mipmap-ldpi-v26/ic_launcher_foreground.png", size: 36 },
{ name: "mipmap-mdpi-v26/ic_launcher_foreground.png", size: 48 },
{ name: "mipmap-xhdpi-v26/ic_launcher_foreground.png", size: 96 },
{ name: "mipmap-xxhdpi-v26/ic_launcher_foreground.png", size: 144 },
{ name: "mipmap-xxxhdpi-v26/ic_launcher_foreground.png", size: 192 }
);
}
platforms.push(android);
platforms.push({
name: "osx",
isAdded: fs.existsSync("platforms/osx"),
iconsPath: "platforms/osx/" + projectName + xcodeFolder,
icons: [
{ name: "icon-16x16.png", size: 16 },
{ name: "icon-32x32.png", size: 32 },
{ name: "icon-64x64.png", size: 64 },
{ name: "icon-128x128.png", size: 128 },
{ name: "icon-256x256.png", size: 256 },
{ name: "icon-512x512.png", size: 512 },
],
});
platforms.push({
name: "windows",
isAdded: fs.existsSync("platforms/windows"),
iconsPath: "platforms/windows/images/",
icons: [
{ name: "StoreLogo.scale-100.png", size: 50 },
{ name: "StoreLogo.scale-125.png", size: 63 },
{ name: "StoreLogo.scale-140.png", size: 70 },
{ name: "StoreLogo.scale-150.png", size: 75 },
{ name: "StoreLogo.scale-180.png", size: 90 },
{ name: "StoreLogo.scale-200.png", size: 100 },
{ name: "StoreLogo.scale-240.png", size: 120 },
{ name: "StoreLogo.scale-400.png", size: 200 },
{ name: "Square44x44Logo.scale-100.png", size: 44 },
{ name: "Square44x44Logo.scale-125.png", size: 55 },
{ name: "Square44x44Logo.scale-140.png", size: 62 },
{ name: "Square44x44Logo.scale-150.png", size: 66 },
{ name: "Square44x44Logo.scale-200.png", size: 88 },
{ name: "Square44x44Logo.scale-240.png", size: 106 },
{ name: "Square44x44Logo.scale-400.png", size: 176 },
{ name: "Square71x71Logo.scale-100.png", size: 71 },
{ name: "Square71x71Logo.scale-125.png", size: 89 },
{ name: "Square71x71Logo.scale-140.png", size: 99 },
{ name: "Square71x71Logo.scale-150.png", size: 107 },
{ name: "Square71x71Logo.scale-200.png", size: 142 },
{ name: "Square71x71Logo.scale-240.png", size: 170 },
{ name: "Square71x71Logo.scale-400.png", size: 284 },
{ name: "Square150x150Logo.scale-100.png", size: 150 },
{ name: "Square150x150Logo.scale-125.png", size: 188 },
{ name: "Square150x150Logo.scale-140.png", size: 210 },
{ name: "Square150x150Logo.scale-150.png", size: 225 },
{ name: "Square150x150Logo.scale-200.png", size: 300 },
{ name: "Square150x150Logo.scale-240.png", size: 360 },
{ name: "Square150x150Logo.scale-400.png", size: 600 },
{ name: "Square310x310Logo.scale-100.png", size: 310 },
{ name: "Square310x310Logo.scale-125.png", size: 388 },
{ name: "Square310x310Logo.scale-140.png", size: 434 },
{ name: "Square310x310Logo.scale-150.png", size: 465 },
{ name: "Square310x310Logo.scale-180.png", size: 558 },
{ name: "Square310x310Logo.scale-200.png", size: 620 },
{ name: "Square310x310Logo.scale-400.png", size: 1240 },
{ name: "Wide310x150Logo.scale-80.png", size: 248, height: 120 },
{ name: "Wide310x150Logo.scale-100.png", size: 310, height: 150 },
{ name: "Wide310x150Logo.scale-125.png", size: 388, height: 188 },
{ name: "Wide310x150Logo.scale-140.png", size: 434, height: 210 },
{ name: "Wide310x150Logo.scale-150.png", size: 465, height: 225 },
{ name: "Wide310x150Logo.scale-180.png", size: 558, height: 270 },
{ name: "Wide310x150Logo.scale-200.png", size: 620, height: 300 },
{ name: "Wide310x150Logo.scale-240.png", size: 744, height: 360 },
{ name: "Wide310x150Logo.scale-400.png", size: 1240, height: 600 },
],
});
// TODO: add missing platforms
deferred.resolve(platforms);
return deferred.promise;
};
/**
* read the config file and get the project name
*
* @return {Promise} resolves to a string - the project's name
*/
var getProjectName = function () {
var deferred = Q.defer();
var parser = new xml2js.Parser();
fs.readFile(settings.CONFIG_FILE, function (err, data) {
if (err) {
deferred.reject(err);
}
parser.parseString(data, function (err, result) {
if (err) {
deferred.reject(err);
}
var projectName = result.widget.name[0];
deferred.resolve(projectName);
});
});
return deferred.promise;
};
/**
* Resizes, crops (if needed) 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();
var srcPath = settings.ICON_FILE;
var platformPath = srcPath.replace(/\.png$/, "-" + platform.name + ".png");
if (fs.existsSync(platformPath)) {
srcPath = platformPath;
}
var dstPath = platform.iconsPath + icon.name;
var dst = path.dirname(dstPath);
if (!fs.existsSync(dst)) {
fs.mkdirsSync(dst);
}
ig.resize(
{
srcPath: srcPath,
dstPath: dstPath,
quality: 1,
format: "png",
width: icon.size,
height: icon.size,
},
function (err, stdout, stderr) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
display.success(icon.name + " created");
}
}
);
if (icon.height) {
ig.crop(
{
srcPath: srcPath,
dstPath: dstPath,
quality: 1,
format: "png",
width: icon.size,
height: icon.height,
},
function (err, stdout, stderr) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
display.success(icon.name + " cropped");
}
}
);
}
return deferred.promise;
};
/**
* Generates icons based on the platform object
*
* @param {Object} platform
* @return {Promise}
*/
var generateIconsForPlatform = function (platform) {
display.header("Generating Icons for " + platform.name);
var all = [];
var icons = platform.icons;
icons.forEach(function (icon) {
all.push(generateIcon(platform, icon));
});
if (platform.extraTask) {
all.push(platform.extraTask());
}
return Promise.all(all);
};
/**
* 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 at least one platform was added to the project
*
* @return {Promise} resolves if at least one platform was found, rejects otherwise
*/
var atLeastOnePlatformFound = function () {
var deferred = Q.defer();
getPlatforms().then(function (platforms) {
var activePlatforms = _(platforms).where({ isAdded: true });
if (activePlatforms.length > 0) {
display.success(
"platforms found: " + _(activePlatforms).pluck("name").join(", ")
);
deferred.resolve();
} else {
display.error(
"No cordova platforms found. " +
"Make sure you are in the root folder of your Cordova project " +
"and add platforms with 'cordova platform add'"
);
deferred.reject();
}
});
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();
} else {
display.error(settings.ICON_FILE + " does not exist");
deferred.reject();
}
});
return deferred.promise;
};
/**
* Checks if a config.xml file exists
*
* @return {Promise} resolves if exists, rejects otherwise
*/
var configFileExists = function () {
var deferred = Q.defer();
fs.exists(settings.CONFIG_FILE, function (exists) {
if (exists) {
display.success(settings.CONFIG_FILE + " exists");
deferred.resolve();
} else {
display.error("cordova's " + settings.CONFIG_FILE + " does not exist");
deferred.reject();
}
});
return deferred.promise;
};
display.header("Checking Project & Icon");
atLeastOnePlatformFound()
.then(validIconExists)
.then(configFileExists)
.then(getProjectName)
.then(getPlatforms)
.then(generateIcons)
.catch(function (err) {
if (err) {
console.log(err);
}
})
.then(function () {
console.log("");
});