Skip to content

Commit

Permalink
fix(cli): ensure temp files are always removed
Browse files Browse the repository at this point in the history
Even in cases when the CLI has errored, ensure that any temp files
created are removed when shutting down.
  • Loading branch information
JamieMason committed Nov 4, 2019
1 parent aa02337 commit 7413b22
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const cli = async (options: ICliOptions) => {
}
complete('Finished');
} catch (err) {
bug(err);
await clean(options);
bug(err);
}
};
5 changes: 4 additions & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import chalk from 'chalk';
import { IOptions } from '.';
import { clean } from './tmpdir';

let color = new chalk.constructor({ enabled: true });

Expand All @@ -16,8 +18,9 @@ export const bug = (err: Error): void => {
process.exit(1);
};

export const panic = (value: string): void => {
export const panic = async (value: string, options: IOptions): Promise<void> => {
console.log(color.red('! %s'), value);
await clean(options);
process.exit(1);
};

Expand Down
2 changes: 1 addition & 1 deletion src/run-imagealpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 panic(`ImageAlpha.app is not installed (${IMAGEALPHA_URL})`);
return panic(`ImageAlpha.app is not installed (${IMAGEALPHA_URL})`, options);
}
await pngquant(pngFilePaths, options);
verbose(`${IMAGEALPHA.name} has finished`);
Expand Down
2 changes: 1 addition & 1 deletion src/run-imageoptim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { info, panic, verbose } from './log';
export const runImageOptim: AppRunner = async (options) => {
info(`Running ${IMAGEOPTIM.name}...`);
if (!(await pathExists(IMAGEOPTIM_BIN_PATH))) {
return panic(`ImageOptim.app is not installed (${IMAGEOPTIM_URL})`);
return panic(`ImageOptim.app is not installed (${IMAGEOPTIM_URL})`, options);
}
await execa(IMAGEOPTIM_BIN_PATH, [options.tmpDir]);
verbose(`${IMAGEOPTIM.name} has finished`);
Expand Down
4 changes: 2 additions & 2 deletions src/run-jpegmini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export const runJpegMini: AppRunner = async (options) => {
const [app, canAutomate] = await Promise.all([jpegMini, assistiveDeviceSupport]);

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

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

info(`Running ${app.name}...`);
Expand Down
6 changes: 4 additions & 2 deletions src/tmpdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { verbose } from './log';
const sourceToTmp = ({ source, tmp }: IFile) => copy(source, tmp);
const tmpToSource = ({ source, tmp }: IFile) => copy(tmp, source);

export const clean = (options: { tmpDir: string }) => remove(options.tmpDir);
export const clean = (options: { tmpDir: string }) => {
verbose(`Deleting temp directory ${options.tmpDir}`);
return remove(options.tmpDir);
};

export const setup = async (options: IOptions) => {
verbose(`Creating temp directory ${options.tmpDir}`);
Expand All @@ -17,6 +20,5 @@ export const setup = async (options: IOptions) => {
export const tearDown = async (options: IOptions) => {
verbose(`Copying ${options.filePaths.length} files back to original location`);
await Promise.all(options.filePaths.map(tmpToSource));
verbose(`Deleting temp directory ${options.tmpDir}`);
await clean(options);
};

0 comments on commit 7413b22

Please sign in to comment.