From ec61eb87bb3e7f186bcc56f4b3da2a1889b3eed8 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Fri, 3 Apr 2015 16:01:07 +0200 Subject: [PATCH] feat: initial release --- .editorconfig | 13 ++++++++ .gitattributes | 1 + .gitignore | 11 +++++++ .travis.yml | 23 +++++++++++++ README.md | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ cli.js | 53 ++++++++++++++++++++++++++++++ icons.json | 16 ++++++++++ index.js | 43 +++++++++++++++++++++++++ license | 21 ++++++++++++ package.json | 48 ++++++++++++++++++++++++++++ test.js | 38 ++++++++++++++++++++++ 11 files changed, 354 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 README.md create mode 100644 cli.js create mode 100644 icons.json create mode 100644 index.js create mode 100644 license create mode 100644 package.json create mode 100644 test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5d12634 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81b477d --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*.log +.tmp +www/ +node_modules/ +bower_components/ +credentials.json +res/ +config.xml +platforms/ +app/img/logo.png +.env diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8a3baca --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +language: node_js +node_js: +- iojs +sudo: false +cache: + directories: + - node_modules +notifications: + email: false + slack: + secure: U8RoCCWTJpF//cWHk/0I1asptg3rVRFehRt2qeGz42Z5VYxh0g/Yr3DQBOGDOcRpbzfOBdFK4rOm9Nh5LLYiXiOEUGuGi1ZH+p7yWNkX+kZI9JzxktZjqKBgHQO2N/aBOGuYm/eBJvbFhZryDubYVf/V736f4N3KxL19JW4LsCY= +deploy: + provider: npm + email: david@excellenteasy.com + skip_cleanup: true + on: + branch: master + repo: excellenteasy/android-icons + api_key: + secure: Ad3fh52neMPTDdft52/vLt9dkhvNKE2PZw3hpnh7SzXdg+GObqj4dnU0qN9pkma66dgBLyyzCjDY6S0Kzz+wZ46emu6jXd7I/zwY6D3mfHfoMOd2vVM366W+fmfYwp0YVDfScs6DYiBYcz7P9JA+riQQUfsXF9aiW7IRgDiPOwA= +env: + global: + secure: F3YYiOKoWR7zd5WF1HL/2YR6dqiXP6dLreaf5+crIV2MUpal0hm11/9Ub5ldlEMosQ3yndo3dIqzEQIK/IiaDX3rpajbfU3aEQs5Yt86TzeobZ7TjaRHthZzTmw5WxGmyM7Rd7yeJBiYRzItiwldrB4aRJYmcJQtC+tyWlVqSKA= diff --git a/README.md b/README.md new file mode 100644 index 0000000..6e94c13 --- /dev/null +++ b/README.md @@ -0,0 +1,87 @@ +# android-icons +[![Build Status](https://travis-ci.org/excellenteasy/android-icons.svg?branch=master)](https://travis-ci.org/excellenteasy/android-icons) +[![Dependency Status](https://david-dm.org/excellenteasy/android-icons.svg)](https://david-dm.org/excellenteasy/android-icons) +[![devDependency Status](https://david-dm.org/excellenteasy/android-icons/dev-status.svg)](https://david-dm.org/excellenteasy/android-icons#info=devDependencies) + +> Get android icon file names and dimensions (width) + +The default icon file names (as used by [cordova](https://cordova.apache.org/docs/en/4.0.0/config_ref_images.md.html#Icons%20and%20Splash%20Screens)) and required sizes for android are listed in a [JSON file](icons.json). This information is useful, for example, when you want to generate icons with the required sizes or to create a [`config.xml`](http://docs.phonegap.com/en/3.5.0/config_ref_images.md.html) file for a PhoneGap/Cordova project or if you just need to create the icons for your android project from one source icon. +This information also exists for [iOS icons](https://github.com/excellenteasy/ios-icons). + + +## Install + +```sh +$ npm install --save android-icons +``` + + +## Usage + +```js +var icons = require('android-icons'); + +icons(); +//=> [{ "name": "mdpi.png", "width": 48 }, { "name": "hdpi.png", "width": 72 }, ... ] + +icons({size: '48'}) +//=> {name: 'mdpi.png', width: 48} + +icons({size: 'xhdpi'}) +//=> {name: 'xhdpi.png', width: 96} +``` + + +## API + +### icons() + +Returns an array of icons, each icon being represented by an object with `name` and `width` properties. + +> Notice that icons are always squares, so no `height` property is provided. + +### icons(options) +#### options + +Only option for now is `size`, which can be either a `Number` or `String` value. If it is a `Number`, it represents the width in pixels. If it is a `String`, you can use `"mdpi"`, `"hdpi"` etc. to refer to a certain size or the complete file name, e.g. `xhdpi.png`. + +Returns icon object for that size or `null`. + +For example: + +```js +icons({size: 'xhdpi'}) +//=> {name: 'xhdpi.png', width: 96} +``` + + +## CLI +> android-icons logs to stdout in comma-separated values format (csv) by default so you can easy pipe to other commands in UNIX systems. + +```sh +$ npm install --global android-icons +``` + +```sh +$ android-icons --help +Get android icon file names and dimensions (width) + +Use `--format json` to set output to JSON. +Get specifc icon by size or name by using `--size`. + +Examples: + $ android-icons --size 48 + icon-40@2x.png,80 + + $ android-icons --size 80 --format json + {"name":"icon-40@2x.png","width":80} + + $ android-icons --size small + icon-small.png,29 +``` + +## Semantic Releases +This module is being [semantically released](https://github.com/boennemann/semantic-release). You can safely use `"^1.0.0"` in your `package.json`. + +## License +MIT © [David Pfahler](http://excellenteasy.com) diff --git a/cli.js b/cli.js new file mode 100644 index 0000000..0762c8c --- /dev/null +++ b/cli.js @@ -0,0 +1,53 @@ +#!/usr/bin/env node +'use strict' +var abbrev = require('abbrev') +var argv = require('minimist')(process.argv.slice(2), abbrev('help', 'version', 'size', 'format')) +var pkg = require('./package.json') +var icons = require('./') + +function help () { + console.log([ + pkg.description, + '', + 'Use `--format json` to set output to JSON.', + 'Get specifc icon by size or name by using `--size`.', + '', + 'Examples:', + ' $ android-icons --size 48', + ' mdpi.png,48', + '', + ' $ android-icons --size 48 --format json', + ' {"name":"mdpi.png","width":48}', + '', + ' $ android-icons --size xhdpi', + ' xhdpi.png,96' + ].join('\n')) +} + +function formatLog (icons, argv) { + var format = (argv.format || 'csv').toLowerCase() + if (format === 'json') { + return JSON.stringify(icons) + } + if (!Array.isArray(icons)) { + icons = [icons] + } + return icons.map(function (icon) { + return icon.name + ',' + icon.width + }).join('\n') +} + +function cli () { + if (argv.help) return help() + + if (argv.version) return console.log(pkg.version) + + var options = { + size: argv.size || argv.s + } + + var output = icons(options) + if (output) console.log(formatLog(output, argv)) +} + +cli() diff --git a/icons.json b/icons.json new file mode 100644 index 0000000..87ae59e --- /dev/null +++ b/icons.json @@ -0,0 +1,16 @@ +[{ + "name": "mdpi.png", + "width": 48 +}, { + "name": "hdpi.png", + "width": 72 +}, { + "name": "xhdpi.png", + "width": 96 +}, { + "name": "xxhdpi.png", + "width": 144 +}, { + "name": "xxxhdpi.png", + "width": 192 +}] diff --git a/index.js b/index.js new file mode 100644 index 0000000..4eaa5b4 --- /dev/null +++ b/index.js @@ -0,0 +1,43 @@ +'use strict' +var icons = require('./icons.json') +var idRegEx = /^(.*)\.png$/ +var widths = icons.map(function (icon) { + return icon.width +}) +var ids = icons.map(function (icon) { + return icon.name.match(idRegEx)[1] +}) + +function getIconForSize (size) { + var width = getWidthForSize(size) + + if (!width) { + return null + } + + return icons[widths.indexOf(width)] || null +} + +function getWidthForSize (size) { + if (typeof size === 'number') return size + + var width = Number(size) + if (!isNaN(width)) return width + + width = widths[ids.indexOf(size)] + if (width) return width + + return widths[ids.indexOf(size.match(idRegEx)[1])] +} + +module.exports = function (options) { + options = options || {} + var size = options.size + if (!size) return icons + + return getIconForSize(size) +} + +module.exports.icons = icons +module.exports.getIconForSize = getIconForSize +module.exports.getWidthForSize = getWidthForSize diff --git a/license b/license new file mode 100644 index 0000000..f1736ce --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) David Pfahler (excellenteasy.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..bf2bb5d --- /dev/null +++ b/package.json @@ -0,0 +1,48 @@ +{ + "name": "android-icons", + "version": "0.0.0-semantically-released", + "description": "Get android icon file names and dimensions (width)", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/excellenteasy/android-icons" + }, + "bin": { + "android-icons": "cli.js" + }, + "author": { + "name": "David Pfahler", + "email": "david@excellenteasy.com", + "url": "http://excellenteasy.com" + }, + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "standard && node test.js", + "prepublish": "semantic-release pre", + "postpublish": "semantic-release post" + }, + "files": [ + "index.js", + "cli.js", + "icons.json" + ], + "keywords": [ + "cli", + "bin", + "icon", + "android", + "cordova", + "phonegap" + ], + "dependencies": { + "abbrev": "^1.0.5", + "minimist": "^1.1.0" + }, + "devDependencies": { + "semantic-release": "^3.0.4", + "standard": "^3.3.2", + "tape": "^3.0.3" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..e2ebe48 --- /dev/null +++ b/test.js @@ -0,0 +1,38 @@ +'use strict' +var test = require('tape') +var androidIcons = require('./') + +test('returns all icons in array', function (t) { + t.plan(2) + var icons = androidIcons() + t.ok(Array.isArray(icons), 'returned an array') + t.equal(icons.length, 5, '5 icons returned') +}) + +test('returns icon for size 48 as Number', function (t) { + t.plan(2) + var icon = androidIcons({size: 48}) + t.ok(icon.name === 'mdpi.png', 'icon name correct') + t.ok(icon.width === 48, 'icon width correct') +}) + +test('returns icon for size 48 as String', function (t) { + t.plan(2) + var icon = androidIcons({size: '48'}) + t.ok(icon.name === 'mdpi.png', 'icon name correct') + t.ok(icon.width === 48, 'icon width correct') +}) + +test('returns icon for size 96', function (t) { + t.plan(2) + var icon = androidIcons({size: 96}) + t.ok(icon.name === 'xhdpi.png', 'icon name correct') + t.ok(icon.width === 96, 'icon width correct') +}) + +test('returns icon for size xhdpi', function (t) { + t.plan(2) + var icon = androidIcons({size: 'xhdpi'}) + t.ok(icon.name === 'xhdpi.png', 'icon name correct') + t.ok(icon.width === 96, 'icon width correct') +})