Skip to content

Commit

Permalink
feat: uses SilentError to fail silently; logs it as warning
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 8, 2019
1 parent c72844d commit f9d8ad3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
13 changes: 0 additions & 13 deletions src/bin/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,11 @@ import terminate from '~/utils/terminate-children';
import logger from '~/utils/logger';
import { wait, status } from 'promist';

let silent: boolean | null = null;

export function setSilent(value?: boolean): void {
// Only set on first call
if (typeof value === 'boolean') return;

silent = value || false;
}

export default function attach(): void {
_attach();
options({
resolver(type, arg) {
try {
if (silent) {
logger.debug('Silent: exiting with code 0');
return resolver('exit', 0);
}
if (type === 'signal') {
logger.debug('Received a termination signal: exiting with code 1');
return resolver('exit', 1);
Expand Down
9 changes: 8 additions & 1 deletion src/bin/kpo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import main from './main';
import logger from '~/utils/logger';
import { terminate, state } from 'exits';
import attach from './attach';
import { error, isOpenError } from '~/utils/errors';
import { error, isOpenError, isSilentError } from '~/utils/errors';

// Attach exits hooks
attach();
// Run main
main(process.argv.slice(2)).catch(async (err) => {
if (state().triggered) return;

if (isSilentError(err)) {
logger.debug('Silent: exiting with code 0');
logger.warn(err.message);
if (err.root.stack) logger.trace(err.root.stack);
return terminate('exit', 0);
}

err = error(err);
logger.error(err.message);
if (err.root.stack) {
Expand Down
2 changes: 0 additions & 2 deletions src/bin/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import list from './list';
import raise from './raise';
import stream from './stream';
import logger from '~/utils/logger';
import { setSilent } from '../attach';

export default async function main(argv: string[]): Promise<void> {
const pkg = await loadPackage(__dirname, { title: true });
Expand Down Expand Up @@ -85,7 +84,6 @@ export default async function main(argv: string[]): Promise<void> {
return acc;
}, {})
};
setSilent(options.silent);

let first = cmd._.shift();
const scopes: string[] = [];
Expand Down
4 changes: 3 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getAllTasks, getTask } from './tasks';
import { IPaths, ILoaded, IChild, ITasks, ITask } from './types';
import getBinPaths from '~/utils/paths';
import logger from '~/utils/logger';
import { SilentError } from '~/utils/errors';

const lazy = <T>(fn: () => Promise<T>): Promise<T> =>
_lazy((resolve, reject) =>
Expand Down Expand Up @@ -46,7 +47,8 @@ export default async function contain<T>(
core.restore();
} catch (err) {
core.restore();
throw err;
const opts = await core.options.catch(() => options);
throw opts.silent ? new SilentError(undefined, err) : err;
}
return res;
}
Expand Down
13 changes: 12 additions & 1 deletion src/public/tags/kpo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import expose, { TExposedOverload } from '~/utils/expose';
import toArgv from 'string-argv';
import { splitBy } from 'cli-belt';
import main from '~/bin/main';
import { isSilentError } from '~/utils/errors';
import logger from '~/utils/logger';

export default expose(kpo) as TExposedOverload<
typeof kpo,
Expand All @@ -28,6 +30,15 @@ function kpo(...args: any[]): (args?: string[]) => Promise<void> {
split[1] = split[1].concat(_argv || []);
if (split[1].length) argv = split[0].concat('--').concat(split[1]);

await main(argv);
try {
await main(argv);
} catch (err) {
if (isSilentError(err)) {
logger.warn(err.message);
if (err.root.stack) logger.trace(err.root.stack);
} else {
throw err;
}
}
};
}
2 changes: 1 addition & 1 deletion src/public/tags/silent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function silent(...args: any[]): (args?: string[]) => Promise<void> {
await exec(command, argv || [], false);
} catch (e) {
const err = error(e);
logger.error(err.message);
logger.warn(err.message);
if (err.root.stack) logger.trace(err.root.stack);
}
};
Expand Down

0 comments on commit f9d8ad3

Please sign in to comment.