Skip to content

Commit

Permalink
fix(cli): exit with error status if an app is not installed
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
The following scenarios will display an error and exit with a status
code of 1, where previously they would display a warning:

+ Not using `--no-imageoptim` when ImageOptim.app is not installed.
+ Using `--imagealpha` when ImageAlpha.app is not installed.
+ Using `--jpegmini` when JPEGmini.app is not installed.

Closes #180
  • Loading branch information
JamieMason committed Sep 15, 2019
1 parent e7c84ac commit 6c3d799
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export const bug = (err: Error): void => {
process.exit(1);
};

export const panic = (value: string): void => {
console.log(color.red('! %s'), value);
process.exit(1);
};

export const result = (
label: string = 'TOTAL',
prettySizeBefore: string,
Expand Down
4 changes: 2 additions & 2 deletions src/run-imagealpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { pathExists } from 'fs-extra';
import { AppRunner, IOptions } from '.';
import { IMAGEALPHA, IMAGEALPHA_URL, PNGQUANT_BIN_PATH } from './constants';
import { isSupported } from './is-supported';
import { info, verbose, warning } from './log';
import { info, panic, verbose } from './log';
import { pngquant } from './pngquant';

export const runImageAlpha: AppRunner = async (options: IOptions) => {
Expand All @@ -11,7 +11,7 @@ export const runImageAlpha: AppRunner = async (options: IOptions) => {
.map((file) => file.tmp)
.filter(isSupported(IMAGEALPHA.supports));
if (!(await pathExists(PNGQUANT_BIN_PATH))) {
return warning(`ImageAlpha.app is not installed (${IMAGEALPHA_URL})`);
return panic(`ImageAlpha.app is not installed (${IMAGEALPHA_URL})`);
}
await pngquant(pngFilePaths, options);
verbose(`${IMAGEALPHA.name} has finished`);
Expand Down
4 changes: 2 additions & 2 deletions src/run-imageoptim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import * as execa from 'execa';
import { pathExists } from 'fs-extra';
import { AppRunner } from '.';
import { IMAGEOPTIM, IMAGEOPTIM_BIN_PATH, IMAGEOPTIM_URL } from './constants';
import { info, verbose, warning } from './log';
import { info, panic, verbose } from './log';

export const runImageOptim: AppRunner = async (options) => {
info(`Running ${IMAGEOPTIM.name}...`);
if (!(await pathExists(IMAGEOPTIM_BIN_PATH))) {
return warning(`ImageOptim.app is not installed (${IMAGEOPTIM_URL})`);
return panic(`ImageOptim.app is not installed (${IMAGEOPTIM_URL})`);
}
await execa(IMAGEOPTIM_BIN_PATH, [options.tmpDir]);
verbose(`${IMAGEOPTIM.name} has finished`);
Expand Down
8 changes: 4 additions & 4 deletions src/run-jpegmini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
runJpegMini as startJpegMini,
supportsAssistiveDevices
} from './applescript';
import { ASSISTIVE_DEVICES_URL, HOMEPAGE_URL, JPEG_MINI_URL } from './constants';
import { info, verbose, warning } from './log';
import { ASSISTIVE_DEVICES_URL, JPEG_MINI_URL } from './constants';
import { info, panic, verbose } from './log';

export const runJpegMini: AppRunner = async (options) => {
verbose('Locating JPEGmini installation');
Expand All @@ -18,11 +18,11 @@ export const runJpegMini: AppRunner = async (options) => {
const [app, canAutomate] = await Promise.all([jpegMini, assistiveDeviceSupport]);

if (!app) {
return warning(`JPEGmini is not installed (${JPEG_MINI_URL})`);
return panic(`JPEGmini is not installed (${JPEG_MINI_URL})`);
}

if (!canAutomate) {
return warning(`Support for assistive devices needed, see ${ASSISTIVE_DEVICES_URL}`);
return panic(`Support for assistive devices needed, see ${ASSISTIVE_DEVICES_URL}`);
}

info(`Running ${app.name}...`);
Expand Down

2 comments on commit 6c3d799

@jamesstout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beat me to it .. I was just trying a fix:

notinstalled-2019-09-16 at 3 16 25 AM

Nice one.

@JamieMason
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn, sorry James!

Please sign in to comment.