forked from JohnCoker/cordova-icon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
250 lines (237 loc) · 7.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
var fs = require('fs');
var xml2js = require('xml2js');
var ig = require('imagemagick');
var colors = require('colors');
var _ = require('underscore');
var Q = require('q');
/**
* 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 getPlatforms = function (projectName) {
var deferred = Q.defer();
var platforms = [];
platforms.push({
name : 'ios',
// TODO: use async fs.exists
isAdded : fs.existsSync('platforms/ios'),
iconsPath : 'platforms/ios/' + projectName + '/Resources/icons/',
icons : [
{ name : 'icon-40.png', size : 40 },
{ name : 'icon-40@2x.png', size : 80 },
{ name : 'icon-50.png', size : 50 },
{ name : 'icon-50@2x.png', size : 100 },
{ name : 'icon-60.png', size : 60 },
{ name : 'icon-60@2x.png', size : 120 },
{ name : 'icon-60@3x.png', size : 180 },
{ name : 'icon-72.png', size : 72 },
{ name : 'icon-72@2x.png', size : 144 },
{ name : 'icon-76.png', size : 76 },
{ name : 'icon-76@2x.png', size : 152 },
{ name : 'icon-small.png', size : 29 },
{ name : 'icon-small@2x.png', size : 58 },
{ name : 'icon.png', size : 57 },
{ name : 'icon@2x.png', size : 114 },
]
});
platforms.push({
name : 'android',
iconsPath : 'platforms/android/res/',
isAdded : fs.existsSync('platforms/android'),
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 },
]
});
// TODO: add all 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.CONFIG_FILE = 'config.xml';
settings.ICON_FILE = 'icon.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('');
};
/**
* 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();
data = 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 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();
ig.resize({
srcPath: settings.ICON_FILE,
dstPath: platform.iconsPath + icon.name,
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');
}
});
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 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 in the root folder');
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 in the root folder');
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('');
});