Skip to content

Commit

Permalink
Merge pull request #1177 from snyk/fix/unexpected-exit-on-ignore
Browse files Browse the repository at this point in the history
Handle undefined return from CLI command
  • Loading branch information
maxjeffos committed Jun 16, 2020
2 parents df62c5d + 1d71763 commit ea33383
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/cli/args.ts
@@ -1,5 +1,5 @@
import * as abbrev from 'abbrev';
import { CommandResult } from './commands/types';
import { MethodResult } from './commands/types';

import debugModule = require('debug');
import { parseMode, displayModeHelp } from './modes';
Expand Down Expand Up @@ -40,11 +40,9 @@ function dashToCamelCase(dash) {
// Last item is ArgsOptions, the rest are strings (positional arguments, e.g. paths)
export type MethodArgs = Array<string | ArgsOptions>;

export type Method = (...args: MethodArgs) => Promise<CommandResult | string>;

export interface Args {
command: string;
method: Method; // command resolved to a function
method: (...args: MethodArgs) => Promise<MethodResult>; // command resolved to a function
options: ArgsOptions;
}

Expand Down Expand Up @@ -150,7 +148,7 @@ export function args(rawArgv: string[]): Args {
argv._.unshift(tmp.shift()!);
}

let method: () => Promise<CommandResult | string> = cli[command];
let method: () => Promise<MethodResult> = cli[command];

if (!method) {
// if we failed to find a command, then default to an error
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/ignore.ts
Expand Up @@ -6,13 +6,14 @@ import * as authorization from '../../lib/authorization';
import * as auth from './auth/is-authed';
import { apiTokenExists } from '../../lib/api-token';
import { isCI } from '../../lib/is-ci';
import { MethodResult } from './types';

import * as Debug from 'debug';
const debug = Debug('snyk');

import { MisconfiguredAuthInCI } from '../../lib/errors/misconfigured-auth-in-ci-error';

function ignore(options) {
function ignore(options): Promise<MethodResult> {
debug('snyk ignore called with options: %O', options);

return auth
Expand Down
2 changes: 2 additions & 0 deletions src/cli/commands/types.ts
@@ -1,3 +1,5 @@
export type MethodResult = CommandResult | string | void;

export class CommandResult {
result: string;
constructor(result: string) {
Expand Down
10 changes: 6 additions & 4 deletions src/cli/index.ts
Expand Up @@ -10,7 +10,7 @@ import * as analytics from '../lib/analytics';
import * as alerts from '../lib/alerts';
import * as sln from '../lib/sln';
import { args as argsLib, Args } from './args';
import { CommandResult, TestCommandResult } from './commands/types';
import { TestCommandResult } from './commands/types';
import { copy } from './copy';
import spinner = require('../lib/spinner');
import errors = require('../lib/errors/legacy-errors');
Expand Down Expand Up @@ -40,16 +40,18 @@ const EXIT_CODES = {
};

async function runCommand(args: Args) {
const commandResult: CommandResult | string = await args.method(
...args.options._,
);
const commandResult = await args.method(...args.options._);

const res = analytics({
args: args.options._,
command: args.command,
org: args.options.org,
});

if (!commandResult) {
return;
}

const result = commandResult.toString();

if (result && !args.options.quiet) {
Expand Down

0 comments on commit ea33383

Please sign in to comment.