Skip to content

Commit

Permalink
refactor: avoid array mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Nov 6, 2023
1 parent 0e626c7 commit 864d6f7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
31 changes: 16 additions & 15 deletions src/errorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export const errorIsGack = (error: Error | SfError): boolean => {
);
};

/** identifies TypeError. Searches the error message, stack, and recursively checks the cause chain */
export const errorIsTypeError = (error: Error | SfError): boolean =>
error instanceof TypeError ||
error.name === 'TypeError' ||
error.message.includes('TypeError') ||
Boolean(error.stack?.includes('TypeError')) ||
('cause' in error && error.cause instanceof Error && errorIsTypeError(error.cause));

/**
* Format errors and actions for human consumption. Adds 'Error (<ErrorCode>):',
* When there are actions, we add 'Try this:' in blue
Expand All @@ -65,21 +73,14 @@ export const formatError = (error: SfCommand.Error): string => {
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');

const colorizedArgs: string[] = [];
const errorCode = typeof error.code === 'string' || typeof error.code === 'number' ? ` (${error.code})` : '';
const errorPrefix = `${StandardColors.error(messages.getMessage('error.prefix', [errorCode]))}`;
colorizedArgs.push(`${errorPrefix} ${error.message}`);
colorizedArgs.push(...formatActions(error.actions ?? []));
if (error.stack && envVars.getString(SfCommand.SF_ENV) === Mode.DEVELOPMENT) {
colorizedArgs.push(StandardColors.info(`\n*** Internal Diagnostic ***\n\n${error.stack}\n******\n`));
}
return colorizedArgs.join('\n');
return [`${errorPrefix} ${error.message}`]
.concat(formatActions(error.actions))
.concat(
error.stack && envVars.getString(SfCommand.SF_ENV) === Mode.DEVELOPMENT
? [`\n*** Internal Diagnostic ***\n\n${error.stack}\n******\n`]
: []
)
.join('\n');
};

/** identifies TypeError. Searches the error message, stack, and recursively checks the cause chain */
export const errorIsTypeError = (error: Error | SfError): boolean =>
error instanceof TypeError ||
error.name === 'TypeError' ||
error.message.includes('TypeError') ||
Boolean(error.stack?.includes('TypeError')) ||
('cause' in error && error.cause instanceof Error && errorIsTypeError(error.cause));
20 changes: 8 additions & 12 deletions src/formatActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ import * as chalk from 'chalk';
import { StandardColors, messages } from './sfCommand';

export const formatActions = (
actions: string[],
actions: string[] = [],
options: { actionColor: chalk.Chalk } = { actionColor: StandardColors.info }
): string[] => {
const colorizedArgs: string[] = [];
// Format any actions.
if (actions?.length) {
colorizedArgs.push(`\n${StandardColors.info(messages.getMessage('actions.tryThis'))}\n`);
actions.forEach((action) => {
colorizedArgs.push(`${options.actionColor(action)}`);
});
}
return colorizedArgs;
};
): string[] =>
actions.length
? [
`\n${StandardColors.info(messages.getMessage('actions.tryThis'))}\n`,
...actions.map((action) => `${options.actionColor(action)}`),
]
: [];
31 changes: 15 additions & 16 deletions src/sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,12 @@ export abstract class SfCommand<T> extends Command {
* @param input {@link SfCommand.Warning} The message to log.
*/
public warn(input: SfCommand.Warning): SfCommand.Warning {
const colorizedArgs: string[] = [];
this.warnings.push(input);
const message = typeof input === 'string' ? input : input.message;

colorizedArgs.push(`${StandardColors.warning(messages.getMessage('warning.prefix'))} ${message}`);
colorizedArgs.push(
...formatActions(typeof input === 'string' ? [] : input.actions ?? [], { actionColor: StandardColors.info })
this.logToStderr(
[`${StandardColors.warning(messages.getMessage('warning.prefix'))} ${parseMessage(input)}`]
.concat(formatActions(typeof input === 'string' ? [] : input.actions))
.join(os.EOL)
);

this.logToStderr(colorizedArgs.join(os.EOL));
return input;
}

Expand All @@ -229,15 +225,11 @@ export abstract class SfCommand<T> extends Command {
* @param input {@link SfCommand.Info} The message to log.
*/
public info(input: SfCommand.Info): void {
const colorizedArgs: string[] = [];
const message = typeof input === 'string' ? input : input.message;

colorizedArgs.push(`${StandardColors.info(message)}`);
colorizedArgs.push(
...formatActions(typeof input === 'string' ? [] : input.actions ?? [], { actionColor: StandardColors.info })
this.log(
[`${StandardColors.info(parseMessage(input))}`]
.concat(formatActions(typeof input === 'string' ? [] : input.actions))
.join(os.EOL)
);

this.log(colorizedArgs.join(os.EOL));
}

/**
Expand Down Expand Up @@ -402,6 +394,10 @@ export abstract class SfCommand<T> extends Command {
};
}

/**
*
* @deprecated. This should not have been exposeed for extension and should be removed in the next major version
*/
// eslint-disable-next-line class-methods-use-this
protected async assignProject(): Promise<SfProject> {
try {
Expand Down Expand Up @@ -508,3 +504,6 @@ function removeEmpty(obj: Record<string, unknown>): Record<string, unknown> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null));
}

const parseMessage = (input: SfCommand.Info | SfCommand.Warning): string =>
typeof input === 'string' ? input : input.message;

0 comments on commit 864d6f7

Please sign in to comment.