Skip to content
This repository was archived by the owner on Oct 19, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
88 changes: 66 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 : '[email protected]', size : 80 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand Down Expand Up @@ -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
Expand All @@ -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 () {
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -268,7 +283,7 @@ var atLeastOnePlatformFound = function () {

/**
* Checks if a valid icon file exists
*
*
* @return {Promise} resolves if exists, rejects otherwise
*/
var validIconExists = function () {
Expand All @@ -287,7 +302,7 @@ var validIconExists = function () {

/**
* Checks if a config.xml file exists
*
*
* @return {Promise} resolves if exists, rejects otherwise
*/
var configFileExists = function () {
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}