forked from AlexDisler/cordova-splash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
309 lines (296 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
var fs = require('fs-extra');
var path = require('path');
var glob = require('glob');
var ig = require('imagemagick');
var colors = require('colors');
var Q = require('q');
var argv = require('minimist')(process.argv.slice(2));
/**
* @var {Object} settings
*/
var settings = {
PROJECT_ROOT: '.',
SPLASH_FILE: 'splash.png',
OLD_XCODE_PATH: false,
OLD_ANDROID_PATH: false,
STORYBOARD_VARIANT: 'single' /* one of 'device', 'universal', or 'single' (default) */
};
/**
* Init settings from options or argv
*
* @param {Object} options
*/
var initSettings = function (options = {}) {
settings.PROJECT_ROOT = options.project || argv.project || settings.PROJECT_ROOT;
settings.SPLASH_FILE = options.splash || argv.splash || settings.SPLASH_FILE;
settings.OLD_XCODE_PATH = options['xcode-old'] || argv['xcode-old'] || settings.OLD_XCODE_PATH;
settings.OLD_ANDROID_PATH = options['android-old'] || argv['android-old'] || settings.OLD_ANDROID_PATH;
settings.STORYBOARD_VARIANT = options.storyboard || argv.storyboard || settings.STORYBOARD_VARIANT;
};
/**
* Return known platforms with their splash screen names and sizes
*
* @return {Promise} resolves with an array of platforms
*/
var getPlatforms = function () {
var deferred = Q.defer();
var platforms = [];
var xcodeFolder = 'Images.xcassets/LaunchImage.launchimage';
var xcodeStoryboardFolder = 'Images.xcassets/LaunchStoryboard.imageset';
var androidFolder = 'app/src/main/res';
var windowsFolder = 'images';
if (settings.OLD_XCODE_PATH) {
xcodeFolder = 'Resources/splash';
}
if (settings.OLD_ANDROID_PATH) {
androidFolder = 'res';
}
platforms.push({
name: 'ios',
splashPattern: path.join(settings.PROJECT_ROOT, 'platforms/ios', '*', xcodeFolder),
splashes: [
// iPhone
{ name: 'Default~iphone.png', width: 320, height: 480 },
{ name: 'Default@2x~iphone.png', width: 640, height: 960 },
{ name: 'Default-568h@2x~iphone.png', width: 640, height: 1136 },
{ name: 'Default-667h.png', width: 750, height: 1334 },
{ name: 'Default-736h.png', width: 1242, height: 2208 },
{ name: 'Default-Landscape-736h.png', width: 2208, height: 1242 },
{ name: 'Default-2436h.png', width: 1125, height: 2436 },
{ name: 'Default-Landscape-2436h.png', width: 2436, height: 1125 },
{ name: 'Default-2688h.png', width: 1242, height: 2688 },
{ name: 'Default-Landscape-2688h.png', width: 2688, height: 1242 },
{ name: 'Default-1792h.png', width: 828, height: 1792 },
{ name: 'Default-Landscape-1792h.png', width: 1792, height: 828 },
// iPad
{ name: 'Default-Portrait~ipad.png', width: 768, height: 1024 },
{ name: 'Default-Portrait@2x~ipad.png', width: 1536, height: 2048 },
{ name: 'Default-Landscape~ipad.png', width: 1024, height: 768 },
{ name: 'Default-Landscape@2x~ipad.png', width: 2048, height: 1536 }
]
});
platforms.push({
name: 'ios-storyboard',
splashPattern: path.join(settings.PROJECT_ROOT, 'platforms/ios', '*', xcodeStoryboardFolder),
splashes: settings.STORYBOARD_VARIANT === 'device' ? [
// iPhone
{ name: 'Default@2x~iphone~anyany.png', width: 1334, height: 1334 },
{ name: 'Default@2x~iphone~comany.png', width: 750, height: 1334 },
{ name: 'Default@2x~iphone~comcom.png', width: 1334, height: 750 },
{ name: 'Default@3x~iphone~anyany.png', width: 2208, height: 2208 },
{ name: 'Default@3x~iphone~anycom.png', width: 2208, height: 1242 },
{ name: 'Default@3x~iphone~comany.png', width: 1242, height: 2208 },
// iPad
{ name: 'Default@2x~ipad~anyany.png', width: 2732, height: 2732 },
{ name: 'Default@2x~ipad~comany.png', width: 1278, height: 2732 }
] : settings.STORYBOARD_VARIANT === 'universal' ? [
// multi-image
{ name: 'Default@2x~universal~anyany.png', width: 2732, height: 2732 },
{ name: 'Default@2x~universal~comany.png', width: 1278, height: 2732 },
{ name: 'Default@2x~universal~comcom.png', width: 1334, height: 750 },
{ name: 'Default@3x~universal~anyany.png', width: 2208, height: 2208 },
{ name: 'Default@3x~universal~anycom.png', width: 2208, height: 1242 },
{ name: 'Default@3x~universal~comany.png', width: 1242, height: 2208 }
] : [
// single-image
{ name: 'Default@2x~universal~anyany.png', width: 2732, height: 2732 }
]
});
platforms.push({
name: 'android',
splashPattern: path.join(settings.PROJECT_ROOT, 'platforms/android', androidFolder),
splashes: [
// Landscape
{ name: 'drawable-land-ldpi/screen.png', width: 320, height: 200 },
{ name: 'drawable-land-mdpi/screen.png', width: 480, height: 320 },
{ name: 'drawable-land-hdpi/screen.png', width: 800, height: 480 },
{ name: 'drawable-land-xhdpi/screen.png', width: 1280, height: 720 },
{ name: 'drawable-land-xxhdpi/screen.png', width: 1600, height: 960 },
{ name: 'drawable-land-xxxhdpi/screen.png', width: 1920, height: 1280 },
// Portrait
{ name: 'drawable-port-ldpi/screen.png', width: 200, height: 320 },
{ name: 'drawable-port-mdpi/screen.png', width: 320, height: 480 },
{ name: 'drawable-port-hdpi/screen.png', width: 480, height: 800 },
{ name: 'drawable-port-xhdpi/screen.png', width: 720, height: 1280 },
{ name: 'drawable-port-xxhdpi/screen.png', width: 960, height: 1600 },
{ name: 'drawable-port-xxxhdpi/screen.png', width: 1280, height: 1920 }
]
});
platforms.push({
name: 'windows',
splashPattern: path.join(settings.PROJECT_ROOT, 'platforms/windows', windowsFolder),
splashes: [
// Landscape
{ name: 'SplashScreen.scale-100.png', width: 620, height: 300 },
{ name: 'SplashScreen.scale-125.png', width: 775, height: 375 },
{ name: 'SplashScreen.scale-140.png', width: 868, height: 420 },
{ name: 'SplashScreen.scale-150.png', width: 930, height: 450 },
{ name: 'SplashScreen.scale-180.png', width: 1116, height: 540 },
{ name: 'SplashScreen.scale-200.png', width: 1240, height: 600 },
{ name: 'SplashScreen.scale-400.png', width: 2480, height: 1200 },
// Portrait
{ name: 'SplashScreenPhone.scale-240.png', width: 1152, height: 1920 },
{ name: 'SplashScreenPhone.scale-140.png', width: 672, height: 1120 },
{ name: 'SplashScreenPhone.scale-100.png', width: 480, height: 800 }
]
});
deferred.resolve(platforms);
return deferred.promise;
};
/**
* @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('');
};
/**
* Crop and create 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();
var srcPath = settings.SPLASH_FILE;
var platformPath = srcPath.replace(/\.png$/, '-' + platform.name + '.png');
if (fs.existsSync(platformPath)) {
srcPath = platformPath;
}
var dstPath = path.join(platform.splashPath, splash.name);
var dst = path.dirname(dstPath);
if (!fs.existsSync(dst)) {
fs.mkdirsSync(dst);
}
ig.crop({
srcPath: srcPath,
dstPath: dstPath,
quality: 1,
format: 'png',
width: splash.width,
height: splash.height
} , function(err, stdout, stderr){
if (err) {
deferred.reject(err);
} else {
display.success(splash.name + ' created');
deferred.resolve();
}
});
return deferred.promise;
};
/**
* Generate 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 = [];
platform.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;
};
/**
* Go over the platforms and trigger splash screen generation
*
* @param {Array} platforms
* @return {Promise}
*/
var generateSplashes = function (platforms) {
var deferred = Q.defer();
var sequence = Q();
var all = [];
platforms.forEach(function (platform) {
sequence = sequence.then(function () {
return generateSplashForPlatform(platform);
});
all.push(sequence);
});
Q.all(all).then(function () {
deferred.resolve();
});
return deferred.promise;
};
/**
* Filter and transform platforms that are really added to the project
*
* @param {Array} platforms
* @return {Promise} resolves with the array of active platforms, rejects otherwise
*/
var asActivePlatforms = function (platforms) {
var deferred = Q.defer();
var activePlatforms = [];
platforms.forEach(function (platform) {
var splashPath = glob.sync(platform.splashPattern).shift();
if (splashPath) {
platform.splashPath = splashPath;
activePlatforms.push(platform);
}
});
if (activePlatforms.length > 0) {
display.success('platforms found: ' + activePlatforms.map(function (platform) { return platform.name; }).join(', '));
deferred.resolve(activePlatforms);
} 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;
};
/**
* Check 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();
} else {
display.error(settings.SPLASH_FILE + ' does not exist');
deferred.reject();
}
});
return deferred.promise;
};
function run(options) {
display.header('Checking Splash & Project');
initSettings(options);
return validSplashExists()
.then(getPlatforms)
.then(asActivePlatforms)
.then(generateSplashes)
.catch(function (err) {
if (err) {
console.log(err);
}
}).then(function () {
console.log('');
});
}
module.exports = run;