Skip to content

Commit

Permalink
fix: overwrites error constructors and helpers for locally imported k…
Browse files Browse the repository at this point in the history
…po instances
  • Loading branch information
rafamel committed May 3, 2019
1 parent 2b7478a commit 0a420d4
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/bin/kpo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import main from './main';
import core from '~/core';
import { error } from 'cli-belt';
import logger from '~/utils/logger';
import { OpenError } from '~/utils/errors';
import errors from '~/utils/errors';
import { ensure } from 'errorish';

main(process.argv.slice(2)).catch(async (err) => {
err = ensure(err);
const isOpen = err instanceof OpenError;
const isOpen = err instanceof errors.OpenError;

return error(err, {
exit: (await core.get('silent').catch(() => false)) ? 0 : 1,
Expand Down
2 changes: 2 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function cache<T>(fn: () => T): () => T {
return _cache(() => options.id, fn);
}

// Core changes might constitute major version changes even if internal, as
// core is used overwritten for different kpo instances on load (requireLocal)
const core = {
get options() {
return options;
Expand Down
12 changes: 8 additions & 4 deletions src/core/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import fs from 'fs-extra';
import yaml from 'js-yaml';
import { rejects } from 'errorish';
import { open } from '~/utils/errors';
import errors from '~/utils/errors';
import { ILoaded, IPaths } from './types';
import { IOfType, IPackageOptions, TCoreOptions } from '~/types';
import options from './options';
Expand Down Expand Up @@ -88,7 +88,7 @@ export async function requireLocal(
kpoPath = require.resolve('kpo', { paths: [file] });
} catch (_) {}
if (kpoPath) {
const local = open.throws(() => require(kpoPath as string));
const local = errors.open.throws(() => require(kpoPath as string));

if (!local || !local.core || !local.core.version) {
throw Error(
Expand All @@ -105,12 +105,16 @@ export async function requireLocal(
(verDiff && version[0] === '0')
) {
throw Error(
`Locally imported kpo version (${localVersion}) doesn't match executing instance version (${version})`
`Locally imported kpo version (${localVersion})` +
` doesn't match executing instance version (${version})`
);
}

// Overwrite options
local.core.options.setBase(raw, 'post');
// Overwrite error constructors and helpers
Object.assign(local.errors, errors);
}

return open.throws(() => require(file));
return errors.open.throws(() => require(file));
}
2 changes: 2 additions & 0 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { DEFAULT_LOG_LEVEL } from '~/constants';
import { setLevel } from '~/utils/logger';
import hash from 'object-hash';

// Option changes should constitute major version changes even if internal,
// as they're overwritten for different kpo instances on load (requireLocal)
export const state = {
base: {
force: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/core/paths/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import { IPaths } from '../types';
import getPaths from './paths';
import { WrappedError } from '~/utils/errors';
import errors from '~/utils/errors';
import { absolute } from '~/utils/file';

/**
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function getRootPaths(directories: {
return await getPaths({ directory }, Boolean(root));
} catch (err) {
if (!root) return null;
throw new WrappedError(
throw new errors.WrappedError(
`root scope couldn't be retrieved: ${root}`,
null,
err
Expand Down
4 changes: 2 additions & 2 deletions src/core/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logger from '~/utils/logger';
import { TScript } from '~/types';
import { open } from '~/utils/errors';
import errors from '~/utils/errors';

export default async function run(
script: TScript,
Expand All @@ -23,7 +23,7 @@ export default async function run(
try {
res = await runner(script);
} catch (err) {
throw open.ensure(err);
throw errors.open.ensure(err);
}
await run(res, runner);
} else if (Array.isArray(script)) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/tasks/from-kpo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ITask } from '../types';
import { IScripts } from '~/types';
import purePath from './pure-path';
import { CustomError } from '~/utils/errors';
import errors from '~/utils/errors';

export default function getFromKpo(path: string, kpo: IScripts): ITask {
const task = trunk(purePath(path).split('.'), kpo, '');
Expand Down Expand Up @@ -42,7 +42,7 @@ export function trunk(arr: string[], obj: any, path: string): ITask {
);
}
if (!props.length) {
throw new CustomError(
throw new errors.CustomError(
`There are no tasks matching ${purePath(path ? `${path}.${key}` : key)}`,
{ type: 'NotFound' }
);
Expand Down
6 changes: 4 additions & 2 deletions src/core/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import getFromKpo from './from-kpo';
import getFromPackage from './from-package';
import { ITask, ITasks } from '../types';
import recursiveFields from './recursive-fields';
import { CustomError } from '~/utils/errors';
import errors from '~/utils/errors';

export function getTask(
path: string,
Expand All @@ -12,7 +12,9 @@ export function getTask(
): ITask {
try {
if (kpo) return getFromKpo(path, kpo);
throw new CustomError(`Task ${path} not found`, { type: 'NotFound' });
throw new errors.CustomError(`Task ${path} not found`, {
type: 'NotFound'
});
} catch (err) {
if (!pkg || !err.data || !err.data.type || err.data.type !== 'NotFound') {
throw err;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './public';
export { default as core } from './core';
export { default as errors } from './utils/errors';
export * from './types';
4 changes: 2 additions & 2 deletions src/public/exec/parallel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import core from '~/core';
import { IOfType, IMultiExecOptions } from '~/types';
import logger from '~/utils/logger';
import { WrappedError } from '~/utils/errors';
import errors from '~/utils/errors';
import expose from '~/utils/expose';
import join from 'command-join';
import { CONCURRENTLY_PATH } from '~/constants';
Expand Down Expand Up @@ -62,7 +62,7 @@ export function create(): IParallel {
try {
await core.exec(CONCURRENTLY_PATH, argv, true, options);
} catch (e) {
const err = new WrappedError(
const err = new errors.WrappedError(
'Parallel commands execution failed',
null,
e
Expand Down
8 changes: 6 additions & 2 deletions src/public/exec/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NODE_PATH, KPO_PATH } from '~/constants';
import { IMultiExecOptions } from '~/types';
import chalk from 'chalk';
import { oneLine } from 'common-tags';
import { WrappedError } from '~/utils/errors';
import errors from '~/utils/errors';

/**
* Options taken by `stream`
Expand Down Expand Up @@ -89,7 +89,11 @@ function stream(
),
{ ...options, cwd: undefined }
)(args).catch(async (err) => {
throw new WrappedError('Series commands execution failed', null, err);
throw new errors.WrappedError(
'Series commands execution failed',
null,
err
);
}));
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/public/kpo/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { options as _options } from '~/core';
import { IScopeOptions } from '~/types';
import { wrap } from '~/utils/errors';
import errors from '~/utils/errors';

/**
* Programmatically sets *kpo* options.
*/
export default function options(opts: IScopeOptions): void {
return wrap.throws(() => _options.setScope(opts));
return errors.wrap.throws(() => _options.setScope(opts));
}
2 changes: 1 addition & 1 deletion src/public/prompts/confirm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
// force import of transpiled prompts
import prompts from 'prompts/dist';
import { TScript } from '~/types';
import { status } from 'promist';
Expand Down
21 changes: 16 additions & 5 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import { scope, Errorish } from 'errorish';

export class CustomError<T> extends Errorish<T> {}
// Error changes might constitute major version changes even if internal,
// as they're overwritten for different kpo instances on load (requireLocal)

export class OpenError<T> extends CustomError<T> {
class CustomError<T> extends Errorish<T> {}

class OpenError<T> extends CustomError<T> {
public get name(): string {
return 'OpenError';
}
}

export class WrappedError<T> extends CustomError<T> {
class WrappedError<T> extends CustomError<T> {
public get name(): string {
return 'WrappedError';
}
}

export const open = scope.set('_kpo_open_', {
const open = scope.set('_kpo_open_', {
Error: CustomError,
Errorish: OpenError
});

export const wrap = scope.set('_kpo_wrap_', {
const wrap = scope.set('_kpo_wrap_', {
Error: CustomError,
Errorish: WrappedError
});

export default {
CustomError,
OpenError,
WrappedError,
open,
wrap
};
4 changes: 2 additions & 2 deletions src/utils/expose.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TScriptFn } from '~/types';
import { wrap } from './errors';
import errors from './errors';

export type TExposedOverload<
T extends (...args: any[]) => TScriptFn,
Expand All @@ -21,7 +21,7 @@ export default function expose<T extends (...args: any[]) => TScriptFn>(
): TExposed<T> {
const exposed = function(...args: any[]): any {
return (argv?: string[]) => {
return wrap.throws(() => fn(...args)(argv));
return errors.wrap.throws(() => fn(...args)(argv));
};
};
exposed.fn = function(...args: any[]) {
Expand Down

0 comments on commit 0a420d4

Please sign in to comment.