diff --git a/README.md b/README.md index 02134c1..d0ccbe2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # cordova-res-generator -Automatic icon and splash screen resizing tool for **Cordova**/**ionic**/**PhoneGap** based applications. +Automatic icon and splash screen resizing tool for **Cordova**/**Ionic**/**PhoneGap** based applications. It automatically resizes and copies your ```icon.png``` and ```splash.png``` files for mobile platforms. @@ -26,6 +26,18 @@ or $ crgen +### Options + + Options: + + -V, --version output the version number + -i, --icon [optional] optional icon file path (default: ./resources/icon.png) + -s, --splash [optional] optional splash file path (default: ./resources/splash.png) + -p, --platforms [optional] optional comma separated platform token list without any space (default: all platforms processed) + (available tokens: android, ios, windows, blackberry10) + -o, -outputdir [optional] optional output directory (default: ./resources/) + -h, --help output usage information + --- ### Platforms diff --git a/index.js b/index.js index d81a372..cf0bc9b 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -#!/usr/bin/env node +#!/usr/bin / env node 'use strict'; @@ -10,6 +10,7 @@ var Q = require('bluebird'); var fs = require('fs-extra'); var path = require('path'); var Jimp = require('jimp'); +var _ = require('lodash'); // helpers @@ -27,20 +28,37 @@ var display = { }, header: (str) => { console.log(''); - console.log(str.cyan.underline); + console.log(str.underline); } }; -// app main variables +// 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 getImages(settings) + return checkPlatforms(settings) + .then((selPlatforms) => g_selectedPlatforms = selPlatforms) + .then(() => getImages(settings)) .then((iobjs) => { g_imageObjects = iobjs; }) @@ -48,6 +66,36 @@ function check(settings) { } +function checkPlatforms(settings) { + var platformsKeys = _.keys(PLATFORMS); + + if (!settings.platforms || typeof settings.platforms !== 'string') { + display.success('Processing files for all platforms'); + return Q.resolve(platformsKeys); + } + + var platforms = settings.platforms.split(','); + 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 = { @@ -193,19 +241,12 @@ function generate(imageObj, settings) { display.header('Generating files'); - var configs = [ - // android - require('./platforms/icons/android'), - require('./platforms/splash/android'), - // ios - require('./platforms/icons/ios'), - require('./platforms/splash/ios'), - // windows - require('./platforms/icons/windows'), - require('./platforms/splash/windows'), - // blackberry10 - require('./platforms/icons/blackberry10'), - ]; + var configs = []; + + g_selectedPlatforms.forEach((platform) => { + PLATFORMS[platform].definitions.forEach((def) => configs.push(require(def))); + }); + return Q.mapSeries(configs, (config) => { return generateForConfig(imageObj, settings, config); @@ -228,6 +269,7 @@ program .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 comma separated list without any space (default: all platforms processed)') .option('-o, -outputdir [optional]', 'optional output directory (default: ./resources/)') .parse(process.argv); @@ -236,6 +278,7 @@ program 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') }; diff --git a/package-lock.json b/package-lock.json index 1762011..09119d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cordova-res-generator", - "version": "0.1.1", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -387,6 +387,11 @@ "xtend": "4.0.1" } }, + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=" + }, "mime": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/mime/-/mime-1.4.1.tgz", diff --git a/package.json b/package.json index 7308e83..83b1a11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-res-generator", - "version": "0.1.3", + "version": "0.2.0", "description": "Generates icon & splash screen for cordova/ionic projects using javascript only.", "main": "index.js", "preferGlobal": false, @@ -37,7 +37,8 @@ "commander": "^2.11.0", "jimp": "^0.2.28", "fs-extra": "^4.0.2", - "bluebird": "^3.5.1" + "bluebird": "^3.5.1", + "lodash": "^4.17.4" }, "contributors": [ { diff --git a/yarn.lock b/yarn.lock index 41a8188..394ee33 100644 --- a/yarn.lock +++ b/yarn.lock @@ -304,6 +304,10 @@ load-bmfont@^1.2.3: xhr "^2.0.1" xtend "^4.0.0" +lodash@^4.17.4: + version "4.17.4" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" + mime-db@~1.30.0: version "1.30.0" resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.30.0.tgz#74c643da2dd9d6a45399963465b26d5ca7d71f01"