diff --git a/CHANGELOG.md b/CHANGELOG.md index 778f2db..e0e0cfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project [will be documented](http://keepachangelog.com/) in this file. This project *tries to* adhere to [Semantic Versioning](http://semver.org/). +## [0.7.0] - 2016-14-17 + +- uses config/res as the default folder to persist generated icons (not backwards compatible) +- Add --help option +- Ability to specify output path +- Backwards-compatibility mode to use platforms path instead of new defaults (-c) + ## [0.6.0] - 2016-03-08 - Allow platform-specific icons (0c26dfe) diff --git a/README.md b/README.md index 7cd8454..f4efdc1 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,25 @@ Then run: $ cordova-icon +You may specify the output path and directory as follows: + + # output to path/to/res/icon + $ cordova-splash -p path/to/res icon + +WARNING: If you were using a previous version of cordova-icon and expect the generated files to be in their respective ./platforms +path, use the compability mode: + + $ cordova-icon -c + +This will override the -p and -i settings. + For good results, your file shoud be: - square - for Android and iOS, at least 192\*192px (512\*512px recommended to be future-proof) - for Windows, at least 1240\*1240px + ### Creating a cordova-cli hook Since the execution of cordova-icon is pretty fast, you can add it as a cordova-cli hook to execute before every build. diff --git a/index.js b/index.js index f49ce38..bfff23a 100644 --- a/index.js +++ b/index.js @@ -6,11 +6,14 @@ var colors = require('colors'); var _ = require('underscore'); var Q = require('q'); var wrench = require('wrench'); +var optparse = require('optparse'); /** - * Check which platforms are added to the project and return their icon names and sizes - * - * @param {String} projectName + * 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) { @@ -20,7 +23,8 @@ var getPlatforms = function (projectName) { name : 'ios', // TODO: use async fs.exists isAdded : fs.existsSync('platforms/ios'), - iconsPath : 'platforms/ios/' + projectName + '/Images.xcassets/AppIcon.appiconset/', + iconsPath : (settings.RESOURCE_PATH + '/' + settings.ICON_DIR + '/ios/').replace('//', '/'), + platformIconsPath: 'platforms/ios/' + projectName + '/Images.xcassets/AppIcon.appiconset/', icons : [ { name : 'icon-40.png', size : 40 }, { name : 'icon-40@2x.png', size : 80 }, @@ -44,7 +48,8 @@ var getPlatforms = function (projectName) { platforms.push({ name : 'android', isAdded : fs.existsSync('platforms/android'), - iconsPath : 'platforms/android/res/', + iconsPath : (settings.RESOURCE_PATH + '/' + settings.ICON_DIR + '/android/').replace('//', '/'), + platformIconsPath: 'platforms/android/res/', icons : [ { name : 'drawable/icon.png', size : 96 }, { name : 'drawable-hdpi/icon.png', size : 72 }, @@ -58,7 +63,8 @@ var getPlatforms = function (projectName) { platforms.push({ name : 'windows', isAdded : fs.existsSync('platforms/windows'), - iconsPath : 'platforms/windows/images/', + iconsPath : (settings.RESOURCE_PATH + '/' + settings.ICON_DIR + '/windows/').replace('//', '/'), + platformIconsPath: 'platforms/windows/images/', icons : [ { name : 'StoreLogo.scale-100.png', size : 50 }, { name : 'StoreLogo.scale-125.png', size : 63 }, @@ -104,12 +110,15 @@ var getPlatforms = function (projectName) { /** - * @var {Object} settings - names of the config file and of the icon image - * TODO: add option to get these values as CLI params + * @var {Object} settings - names of the config 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'; +settings.RESOURCE_PATH = 'config/res'; // without trailing slash +settings.ICON_DIR = 'icon'; // without slashes +settings.USE_PLATFORMS_PATH = false; // true to use platforms path /** * @var {Object} console utils @@ -131,7 +140,7 @@ display.header = function (str) { /** * read the config file and get the project name - * + * * @return {Promise} resolves to a string - the project's name */ var getProjectName = function () { @@ -154,9 +163,11 @@ var getProjectName = function () { /** * Resizes, crops (if needed) and creates a new icon in the platform's folder. - * - * @param {Object} platform - * @param {Object} icon + * + * @param {Object} + * platform + * @param {Object} + * icon * @return {Promise} */ var generateIcon = function (platform, icon) { @@ -166,7 +177,8 @@ var generateIcon = function (platform, icon) { if (fs.existsSync(platformPath)) { srcPath = platformPath; } - var dstPath = platform.iconsPath + icon.name; + var dstPath = (settings.USE_PLATFORMS_PATH ? + platform.platformIconsPath : platform.iconsPath) + icon.name; var dst = path.dirname(dstPath); if (!fs.existsSync(dst)) { wrench.mkdirSyncRecursive(dst); @@ -183,7 +195,7 @@ var generateIcon = function (platform, icon) { deferred.reject(err); } else { deferred.resolve(); - display.success(icon.name + ' created'); + display.success(icon.name + ' created [' + dstPath + ']'); } }); if (icon.height) { @@ -208,8 +220,9 @@ var generateIcon = function (platform, icon) { /** * Generates icons based on the platform object - * - * @param {Object} platform + * + * @param {Object} + * platform * @return {Promise} */ var generateIconsForPlatform = function (platform) { @@ -224,8 +237,9 @@ var generateIconsForPlatform = function (platform) { /** * Goes over all the platforms and triggers icon generation - * - * @param {Array} platforms + * + * @param {Array} + * platforms * @return {Promise} */ var generateIcons = function (platforms) { @@ -246,8 +260,9 @@ var generateIcons = function (platforms) { /** * Checks if at least one platform was added to the project - * - * @return {Promise} resolves if at least one platform was found, rejects otherwise + * + * @return {Promise} resolves if at least one platform was found, rejects + * otherwise */ var atLeastOnePlatformFound = function () { var deferred = Q.defer(); @@ -268,7 +283,7 @@ var atLeastOnePlatformFound = function () { /** * Checks if a valid icon file exists - * + * * @return {Promise} resolves if exists, rejects otherwise */ var validIconExists = function () { @@ -287,7 +302,7 @@ var validIconExists = function () { /** * Checks if a config.xml file exists - * + * * @return {Promise} resolves if exists, rejects otherwise */ var configFileExists = function () { @@ -304,6 +319,35 @@ var configFileExists = function () { return deferred.promise; }; +/** + * parse command line options + */ +var parseOptions = function() { + var switches = [ + ['-h', '--help', 'Show this help'], + ['-p', '--path PATH', 'resource path, defaults to ' + settings.RESOURCE_PATH], + ['-i', '--icon DIR', 'icon directory in PATH, defaults to ' + settings.ICON_DIR], + ['-c', '--compat', 'uses default path in platforms (backwards compatibility, overrides -p and -i)'], + ]; + var parser = new optparse.OptionParser(switches); + parser.on('help', function() { + console.log(parser.toString()); + process.exit(); + }); + parser.on('path', function(opt, path) { + settings.RESOURCE_PATH = path; + }); + parser.on('icon', function(opt, path) { + settings.SCREEN_DIR = path; + }); + parser.on('compat', function() { + settings.USE_PLATFORMS_PATH = true; + }); + parser.parse(process.argv); +} + +parseOptions(); + display.header('Checking Project & Icon'); atLeastOnePlatformFound() diff --git a/package.json b/package.json index 9fa6042..c0fcdac 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "q": "^1.0.1", "underscore": "^1.6.0", "wrench": "^1.5.8", - "xml2js": "^0.4.3" + "xml2js": "^0.4.3", + "optparse": "^1.0.5" } }