@fusionauth/cli
Version:
112 lines (111 loc) • 3.19 kB
JavaScript
import chalk from 'chalk';
/**
* Checks if the response is a client response
* @param response
*/
export const isClientResponse = (response) => {
return response.wasSuccessful !== undefined;
};
/**
* Checks if the response is an error response
* @param response
*/
export const isErrors = (response) => {
return response.fieldErrors !== undefined || response.generalErrors !== undefined;
};
/**
* Reports an error to the console
* @param msg The message to report
* @param error The error to report
*/
export const reportError = (msg, error) => {
console.error(chalk.red(msg));
if (!error) {
return;
}
if (isClientResponse(error) && error.exception) {
error = error.exception;
}
if (isErrors(error)) {
const { fieldErrors, generalErrors } = error;
if (fieldErrors) {
Object.entries(fieldErrors)
.forEach(([field, fieldError]) => {
console.error(chalk.red(chalk.underline(field) + ': ' + fieldError
.map((fieldError) => fieldError.message)
.join(', ')));
});
}
if (generalErrors) {
generalErrors.forEach((generalError) => {
console.error(chalk.red(generalError.message));
});
}
return;
}
if (typeof error === 'string') {
console.error(chalk.red(error));
return;
}
if ('message' in error) {
console.error(chalk.red(error.message));
return;
}
console.error(chalk.red(toJson(error)));
};
/**
* Gets the locale from a path
* @param path
*/
export const getLocaleFromLocalizedMessageFileName = (path) => {
const matches = path.match(/localizedMessages\.([a-z]{2}(?:_[A-Z]{2})?)\.txt/);
if (!matches)
return;
return matches[1];
};
/**
* Returns the error message for a given email template id
* @param action
* @param emailTemplateId
*/
export const getEmailErrorMessage = (action, emailTemplateId) => {
let templateIdMessage = 'templates';
if (emailTemplateId) {
templateIdMessage = `template ${emailTemplateId}`;
}
return `Error ${action} email ${templateIdMessage}`;
};
/**
* Returns the success message for a given email template id
* @param emailTemplateId
* @param output
*/
export const getEmailSuccessMessage = (emailTemplateId, output) => {
let templateIdMessage = 'templates';
if (emailTemplateId) {
templateIdMessage = `template ${emailTemplateId}`;
}
return `Successfully downloaded email ${templateIdMessage} to ${output}`;
};
/**
* Returns the given item if it is not undefined, otherwise returns an empty string
*/
export function toString(item) {
return item !== null && item !== void 0 ? item : '';
}
/**
* Returns the given object as a JSON string
* @param item The item to convert to JSON
*/
export function toJson(item) {
return JSON.stringify(item !== null && item !== void 0 ? item : {}, null, 4);
}
/**
* Reports an error and exits the process
* @param message
* @param error
*/
export function errorAndExit(message, error) {
reportError(message, error);
process.exit(1);
}