Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Migrate to async/await #97

Merged
merged 1 commit into from
May 4, 2019
Merged
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
9 changes: 0 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ jobs:
path: "src/label/test-images"
- store_artifacts:
path: "src/resize/test-images"
test-node6:
<<: *test-common
docker:
- image: dwmkerr/node:6-imagemagick
test-node8:
<<: *test-common
docker:
Expand All @@ -38,11 +34,6 @@ workflows:
version: 2
build:
jobs:
- test-node6:
# All branches, all tags.
filters:
tags:
only: /.*/
- test-node8:
# All branches, all tags.
filters:
Expand Down
5 changes: 4 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
extends: airbnb-base
extends:
- airbnb-base
- plugin:node/recommended
env:
node: true
mocha: true
Expand All @@ -10,3 +12,4 @@ rules:
# tests, so relax the requirement to have single return statements
arrow-body-style: 0
import/no-extraneous-dependencies: [error, { devDependencies: ['**/*.specs.js'] }]
no-process-exit: 0
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![npm version](https://badge.fury.io/js/app-icon.svg)](https://badge.fury.io/js/app-icon) [![CircleCI](https://circleci.com/gh/dwmkerr/app-icon.svg?style=shield)](https://circleci.com/gh/dwmkerr/app-icon) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/3e334rknhjbpx555?svg=true)](https://ci.appveyor.com/project/dwmkerr/app-icon) [![codecov](https://codecov.io/gh/dwmkerr/app-icon/branch/master/graph/badge.svg)](https://codecov.io/gh/dwmkerr/app-icon) [![dependencies Status](https://david-dm.org/dwmkerr/app-icon/status.svg)](https://david-dm.org/dwmkerr/app-icon) [![devDependencies Status](https://david-dm.org/dwmkerr/app-icon/dev-status.svg)](https://david-dm.org/dwmkerr/app-icon?type=dev) [![GuardRails badge](https://badges.production.guardrails.io/dwmkerr/app-icon.svg)](https://www.guardrails.io) [![Greenkeeper badge](https://badges.greenkeeper.io/dwmkerr/app-icon.svg)](https://greenkeeper.io/) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)

Icon management for Mobile Apps. Create icons, generate all required sizes, label and annotate. Supports Native, Cordova, React Native, Xamarin and more. Inspired by [cordova-icon](https://github.com/AlexDisler/cordova-icon). Node 6 and onwards supported.
Icon management for Mobile Apps. Create icons, generate all required sizes, label and annotate. Supports Native, Cordova, React Native, Xamarin and more. Inspired by [cordova-icon](https://github.com/AlexDisler/cordova-icon). Node 8 and onwards supported.

<img src="./assets/banner.png" width="614" alt="Banner">

Expand Down Expand Up @@ -181,7 +181,7 @@ Note that Adaptive Icons of *all* supported sizes are generated. However, we als

## Developer Guide

The only dependencies are Node 6 (or above) and Yarn.
The only dependencies are Node 8 (or above) and Yarn.

Useful commands for development are:

Expand All @@ -198,8 +198,8 @@ Currently the linting style is based on [airbnb](https://github.com/airbnb/javas
Install the dependencies (I recommend [Node Version Manager](https://github.com/creationix/nvm)):

```bash
nvm install 6
nvm use 6
nvm install 8
nvm use 8
git clone git@github.com:dwmkerr/app-icon.git
cd app-icon
npm install && npm test
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
environment:
nodejs_version: "6"
nodejs_version: "8"

# Install scripts. (runs after repo cloning)
install:
Expand Down
179 changes: 77 additions & 102 deletions bin/app-icon.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env node

// We use Node 6 to keep compatibility high, so need the 'use strict' statement.
// eslint-disable-next-line
'use strict';
/* eslint-disable consistent-return */

const chalk = require('chalk');
const program = require('commander');
Expand All @@ -15,14 +13,23 @@ const labelImage = require('../src/label/label-image');
const fileExists = require('../src/utils/file-exists');

// Helper to throw an error if a file doesn't exist.
const errorIfMissing = (filePath, errorMessage) => {
return fileExists(filePath).then((exists) => {
if (!exists) {
console.error(`${chalk.red('error')}: ${errorMessage}`);
return process.exit(1);
}
return true;
});
const errorIfMissing = async (filePath, errorMessage) => {
try {
await fileExists(filePath);
} catch (err) {
console.error(`${chalk.red('error')}: ${errorMessage}`);
return process.exit(1);
}
};

const imageMagickCheck = async () => {
const version = await imagemagickCli.getVersion();

if (!version) {
console.error(' Error: ImageMagick must be installed. Try:');
console.error(' brew install imagemagick');
return process.exit(1);
}
};

// Create the program.
Expand All @@ -39,7 +46,7 @@ program
.option('--background-icon [optional]', "The background icon path. Defaults to 'icon.background.png'")
.option('--foreground-icon [optional]', "The foregroud icon path. Defaults to 'icon.foregroud.png'")
.option('--adaptive-icons [optional]', "Additionally, generate Android Adaptive Icon templates. Defaults to 'false'")
.action((parameters) => {
.action(async (parameters) => {
const {
icon,
backgroundIcon,
Expand All @@ -48,43 +55,31 @@ program
platforms,
adaptiveIcons,
} = parameters;
imagemagickCli.getVersion()
.then((version) => {
if (!version) {
console.error(' Error: ImageMagick must be installed. Try:');
console.error(' brew install imagemagick');
process.exit(1);
}
})
.then(() => {
// Check we have the files we need.
const operations = [];
operations.push(errorIfMissing(icon, `Source file '${icon}' does not exist. Add the file or specify source icon with the '--icon' parameter.`));
if (adaptiveIcons) {
const checkPath = backgroundIcon || 'icon.background.png';
operations.push(errorIfMissing(checkPath, `Background icon file '${checkPath}' does not exist. Add the file or specify background icon with the '--background-icon' parameter.`));
}
if (adaptiveIcons) {
const checkPath = foregroundIcon || 'icon.foreground.png';
operations.push(errorIfMissing(checkPath, `Foreground icon file '${checkPath}' does not exist. Add the file or specify foreground icon with the '--foreground-icon' parameter.`));
}
return Promise.all(operations);
})
.then(() => {
// Generate some icons.
return generate({
sourceIcon: icon,
backgroundIcon,
foregroundIcon,
searchRoot: search,
platforms,
adaptiveIcons,
});
})
.catch((generateErr) => {
console.error(chalk.red(`An error occurred generating the icons: ${generateErr.message}`));
return process.exit(1);

await imageMagickCheck();

await errorIfMissing(icon, `Source file '${icon}' does not exist. Add the file or specify source icon with the '--icon' parameter.`);
if (adaptiveIcons) {
const checkPath = backgroundIcon || 'icon.background.png';
dwmkerr marked this conversation as resolved.
Show resolved Hide resolved
await errorIfMissing(checkPath, `Background icon file '${checkPath}' does not exist. Add the file or specify background icon with the '--background-icon' parameter.`);
}
if (adaptiveIcons) {
const checkPath = foregroundIcon || 'icon.foreground.png';
await errorIfMissing(checkPath, `Foreground icon file '${checkPath}' does not exist. Add the file or specify foreground icon with the '--foreground-icon' parameter.`);
}
try {
await generate({
sourceIcon: icon,
backgroundIcon,
foregroundIcon,
searchRoot: search,
platforms,
adaptiveIcons,
});
} catch (err) {
console.error(chalk.red(`An error occurred generating the icons: ${err.message}`));
process.exit(1);
}
});

// Define the 'label' command.
Expand All @@ -95,38 +90,25 @@ program
.option('-o, --output <output>', "The output image, .e.g 'icon-out.png'.")
.option('-t, --top [top]', "The label to put on the top of the image, .e.g 'qa'.")
.option('-b, --bottom [bottom]', "The label to put on the bottom of the image, .e.g '1.2.5'.")
.action((parameters) => {
.action(async (parameters) => {
const {
input,
output,
top,
bottom,
} = parameters;

imagemagickCli.getVersion()
.then((version) => {
if (!version) {
console.error(' Error: ImageMagick must be installed. Try:');
console.error(' brew install imagemagick');
return process.exit(1);
}

// Check that we have a input file.
return fileExists(input);
})
.then((exists) => {
if (!exists) {
console.error(`Input file '${input}' does not exist.`);
return process.exit(1);
}
// Generate some icons then innit.
return labelImage(input, output, top, bottom);
})
.catch((labelErr) => {
console.error('An error occurred labelling the icon...');
console.log(labelErr);
return process.exit(1);
});
await imageMagickCheck();

await errorIfMissing(input, `Input file '${input}' does not exist.`);

try {
await labelImage(input, output, top, bottom);
} catch (err) {
console.error('An error occurred labelling the icon...');
dwmkerr marked this conversation as resolved.
Show resolved Hide resolved
console.log(err);
process.exit(1);
}
});

// Define the 'init' command.
Expand All @@ -135,38 +117,31 @@ program
.description('Initialises app icons by creating simple icon templates')
.option('-c, --caption [caption]', "An optional caption for the icon, e.g 'App'.")
.option('--adaptive-icons [optional]', "Additionally, generate Android Adaptive Icon templates. Defaults to 'false'")
.action((params) => {
.action(async (params) => {
const { caption, adaptiveIcons } = params;
imagemagickCli.getVersion()
.then((version) => {
if (!version) {
console.error(' Error: ImageMagick must be installed. Try:');
console.error(' brew install imagemagick');
return process.exit(1);
}

// Create the icon from the template, captioned if needed.
const input = path.resolve(__dirname, '../src/init/icon.template.png');
return init(input, './icon.png', { caption })
.then(() => console.log(`Created icon '${chalk.green('icon.png')}'`));
})
.then(() => {
// If we are going to use adaptive icons, create them.
if (!adaptiveIcons) return;

await imageMagickCheck();

// Create the icon from the template, captioned if needed.
const input = path.resolve(__dirname, '../src/init/icon.template.png');

try {
await init(input, './icon.png', { caption });
console.log(`Created icon '${chalk.green('icon.png')}'`);

if (adaptiveIcons) {
const inputBackground = path.resolve(__dirname, '../src/init/icon.background.template.png');
const inputForeground = path.resolve(__dirname, '../src/init/icon.foreground.template.png');
init(inputBackground, './icon.background.png')
.then(() => init(inputForeground, './icon.foreground.png', { caption }))
.then(() => {
console.log(`Created icon '${chalk.green('icon.background.png')}'`);
console.log(`Created icon '${chalk.green('icon.foreground.png')}'`);
});
})
.catch((createError) => {
console.error('An error occurred creating the icon...');
console.log(createError);
return process.exit(1);
});
await init(inputBackground, './icon.background.png');
await init(inputForeground, './icon.foreground.png', { caption });
console.log(`Created icon '${chalk.green('icon.background.png')}'`);
console.log(`Created icon '${chalk.green('icon.foreground.png')}'`);
}
} catch (err) {
console.error('An error occurred creating the icon...');
console.log(err);
return process.exit(1);
}
});

// Extend the help with some examples.
Expand Down
Loading