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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
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.9.x] -
- Add --help option
- Ability to specify resource output path via --path
- Ability to specify resource icon output path via --icon-dir

## [0.9.2] - 2017-01-23
- Add missing Windows 8 sizes (#91)

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ You also can specify manually a location for your `config.xml` or `icon.png`:

$ cordova-icon --config=config.xml --icon=icon.png

You may specify the output path and directory as follows:

# output to path/to/res/icon
$ cordova-icon --path path/to/res --icon-dir icon

This will generate icons in the --path and --icon-dir paths instead of each cordova platforms folders.

If you run a old version of Cordova for iOS / Mac and you need your files in `/Resources/icons/`, use this option:

$ cordova-icon --xcode-old

For good results, your file should be:

- square
- for Android and iOS, at least 1024\*1024px
- for Windows, at least 1240\*1240px
Expand Down
104 changes: 80 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ var ig = require('imagemagick');
var colors = require('colors');
var _ = require('underscore');
var Q = require('q');
var argv = require('minimist')(process.argv.slice(2));
var optparse = require('optparse');

/**
* @var {Object} settings - names of the config file and of the icon image
*/
var settings = {};
settings.CONFIG_FILE = argv.config || 'config.xml';
settings.ICON_FILE = argv.icon || 'icon.png';
settings.OLD_XCODE_PATH = argv['xcode-old'] || false;
settings.CONFIG_FILE = 'config.xml';
settings.ICON_FILE = 'icon.png';
settings.OLD_XCODE_PATH = false;
settings.RESOURCE_PATH = 'config/res'; // without trailing slash
settings.ICON_DIR = 'icon'; // without slashes
settings.USE_PLATFORMS_PATH = true; // true to use platforms path

/**
* 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 @@ -34,7 +39,8 @@ var getPlatforms = function (projectName) {
name : 'ios',
// TODO: use async fs.exists
isAdded : fs.existsSync('platforms/ios'),
iconsPath : 'platforms/ios/' + projectName + xcodeFolder,
iconsPath : (settings.RESOURCE_PATH + '/' + settings.ICON_DIR + '/ios/').replace('//', '/'),
platformIconsPath: 'platforms/ios/' + projectName + xcodeFolder,
icons : [
{ name: 'icon-20.png', size : 20 },
{ name: 'icon-20@2x.png', size : 40 },
Expand Down Expand Up @@ -69,7 +75,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 Down Expand Up @@ -103,7 +110,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 @@ -182,7 +190,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 @@ -205,9 +213,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 @@ -217,7 +227,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)) {
fs.mkdirsSync(dst);
Expand All @@ -234,7 +245,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 @@ -259,8 +270,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 @@ -275,8 +287,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 @@ -297,8 +310,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 @@ -319,7 +333,7 @@ var atLeastOnePlatformFound = function () {

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

/**
* Checks if a config.xml file exists
*
*
* @return {Promise} resolves if exists, rejects otherwise
*/
var configFileExists = function () {
Expand All @@ -355,6 +369,48 @@ var configFileExists = function () {
return deferred.promise;
};

/**
* parse command line options
*/
var parseOptions = function() {
var switches = [
['-h', '--help', 'Show this help'],
['-i', '--icon DIR', 'icon file in PATH, defaults to ' + settings.ICON_FILE],
['-c', '--config DIR', 'config file in PATH, defaults to ' + settings.CONFIG_FILE],
['-p', '--path PATH', 'resource path, defaults to ' + settings.RESOURCE_PATH],
['-id', '--icon-dir DIR', 'icon directory in PATH, defaults to ' + settings.ICON_DIR],
['-xo', '--xcode-old', 'use old version of Cordova for iOS and generate file in /Resources/icons/'],
];
var parser = new optparse.OptionParser(switches);
parser.on('help', function() {
console.log(parser.toString());
process.exit();
});
parser.on('config', function(opt, path) {
settings.CONFIG_FILE = path;
});
parser.on('icon', function(opt, path) {
settings.ICON_FILE = path;
});
parser.on('path', function(opt, path) {
// Only update if value provided, otherwise assum default and disable .
if (path) {
settings.RESOURCE_PATH = path;
}
settings.USE_PLATFORMS_PATH = false;
});
parser.on('icon-dir', function(opt, path) {
settings.ICON_DIR = path;
settings.USE_PLATFORMS_PATH = false;
});
parser.on('xcode-old', function() {
settings.OLD_XCODE_PATH = true;
});
parser.parse(process.argv);
}

parseOptions();

display.header('Checking Project & Icon');

atLeastOnePlatformFound()
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"colors": "^0.6.2",
"fs-extra": "^0.30.0",
"imagemagick": "^0.1.3",
"minimist": "^1.2.0",
"q": "^1.0.1",
"underscore": "^1.6.0",
"xml2js": "^0.4.3"
"xml2js": "^0.4.3",
"optparse": "^1.0.5"
}
}