Skip to content

Commit

Permalink
feat(errors): Added the createFromData helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Nov 8, 2024
1 parent c4147e9 commit c15576d
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 86 deletions.
122 changes: 67 additions & 55 deletions packages/cli/src/program/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

import type { StormConfig } from "@storm-software/config";
import { createStormConfig } from "@storm-software/config-tools";
import { getCauseFromUnknown } from "@storm-stack/errors";
import { StormError } from "@storm-stack/errors";
import { StormLog } from "@storm-stack/logging";
import { titleCase } from "@storm-stack/string-fns";
import { EMPTY_STRING, NEWLINE_STRING } from "@storm-stack/types";
import chalk from "chalk";
import { Argument, Command, Option } from "commander";
import { Table } from "console-table-printer";
import { text } from "figlet";
import type { CLIArgument, CLICommand, CLIConfig, CLIOption } from "../types";
import { writeBanner } from "../utilities/write-banner";
import { registerShutdown } from "./shutdown";

const createCLIArgument = (cliArgument: CLIArgument): Argument => {
Expand Down Expand Up @@ -80,40 +80,25 @@ const createCLICommand = (cliCommand: CLICommand): Command => {

export async function createCLIProgram(cliConfig: CLIConfig): Promise<void> {
const config: StormConfig = createStormConfig();
const logger = StormLog.create(config);

const name = cliConfig.name ?? config.name ?? "Storm CLI";

StormLog.initialize(config, name);
StormLog.info(config);

try {
if (cliConfig.banner?.hide !== true) {
text(
cliConfig.banner?.name ?? cliConfig.name ?? config.name ?? "Storm CLI",
cliConfig.banner?.options ?? {
font: cliConfig.banner?.font ?? "Larry 3D"
},
(err, data) => {
if (err) {
return;
}

console.log(chalk.hex(config.colors.primary)(data));
}
);

if (cliConfig.by?.hide !== true) {
text(
`by ${cliConfig.by?.name ?? config.organization ?? "Storm"}`,
cliConfig.banner?.options ?? { font: cliConfig.by?.font ?? "Doom" },
(err, data) => {
if (err) {
return;
}

console.log(chalk.hex(config.colors.primary)(data));
}
);
writeBanner(
{
name,
...cliConfig.banner
},
{
name: config.organization,
...cliConfig.by
}
}
);

logger.info(
StormLog.info(
`⚡ Starting the ${titleCase(cliConfig.name) ?? "Storm CLI"} application`
);

Expand All @@ -132,18 +117,17 @@ export async function createCLIProgram(cliConfig: CLIConfig): Promise<void> {
: `${config.homepage}/license`
} \n`;

logger.debug(urlDisplay);
logger.info(licenseDisplay);
StormLog.debug(urlDisplay);
StormLog.info(licenseDisplay);

logger.start(titleCase(cliConfig.name) ?? "Storm CLI Application");
const startDateTime = StormLog.start(titleCase(cliConfig.name ?? name));
const program = new Command(cliConfig.name ?? "Storm CLI");
const shutdown = registerShutdown({
logger,
onShutdown: () => {
logger.stopwatch("Storm CLI Application");
StormLog.stopwatch(startDateTime, titleCase(cliConfig.name ?? name));
program.exitOverride();

logger.info("Application is shutting down...");
StormLog.info("Application is shutting down...");
}
});

Expand Down Expand Up @@ -179,24 +163,50 @@ export async function createCLIProgram(cliConfig: CLIConfig): Promise<void> {
const table = new Table({
style: {
headerTop: {
left: chalk.hex(config.colors.background)("╔"),
mid: chalk.hex(config.colors.background)("╦"),
right: chalk.hex(config.colors.background)("╗"),
other: chalk.hex(config.colors.background)("═")
left: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╔"),
mid: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╦"),
right: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╗"),
other: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("═")
},
headerBottom: {
left: chalk.hex(config.colors.background)("╟"),
mid: chalk.hex(config.colors.background)("╬"),
right: chalk.hex(config.colors.background)("╢"),
other: chalk.hex(config.colors.background)("═")
left: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╟"),
mid: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╬"),
right: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╢"),
other: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("═")
},
tableBottom: {
left: chalk.hex(config.colors.background)("╚"),
mid: chalk.hex(config.colors.background)("╩"),
right: chalk.hex(config.colors.background)("╝"),
other: chalk.hex(config.colors.background)("═")
left: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╚"),
mid: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╩"),
right: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("╝"),
other: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("═")
},
vertical: chalk.hex(config.colors.background)("║")
vertical: chalk.hex(
(config.colors as any)?.dark?.background ?? "#FFFFFF"
)("║")
},
title: command.name,
columns: [
Expand Down Expand Up @@ -225,7 +235,9 @@ export async function createCLIProgram(cliConfig: CLIConfig): Promise<void> {
options: option.choices?.join(", ") ?? EMPTY_STRING,
defaultValue: option.default?.value ?? EMPTY_STRING
},
{ color: config.colors.primary }
{
color: (config.colors as any)?.dark?.primary ?? "#FFFFFF"
}
);
}
}
Expand All @@ -238,11 +250,11 @@ export async function createCLIProgram(cliConfig: CLIConfig): Promise<void> {
await program.parseAsync(process.argv);
shutdown();
} catch (innerError) {
logger.fatal(innerError);
shutdown(getCauseFromUnknown(innerError).message);
StormLog.fatal(innerError);
shutdown(StormError.create(innerError).message);
}
} catch (error) {
logger.fatal(error);
StormLog.fatal(error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
Expand Down
31 changes: 15 additions & 16 deletions packages/cli/src/program/shutdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
-------------------------------------------------------------------*/

import type { StormLog } from "@storm-stack/logging";
import { StormLog } from "@storm-stack/logging";
import type { MaybePromise } from "@storm-stack/types";

const errorTypes = ["unhandledRejection", "uncaughtException"];
const signalTraps = ["SIGTERM", "SIGINT", "SIGUSR2"];

export function registerShutdown(config: {
logger: StormLog;
onShutdown(): void | MaybePromise<void>;
}): (reason?: string) => Promise<void> {
let exited = false;
Expand All @@ -31,22 +30,22 @@ export function registerShutdown(config: {
if (exited) {
return;
}
config.logger.info("Shutting down...");
StormLog.info("Shutting down...");
exited = true;
await config.onShutdown();
}

errorTypes.map(type => {
process.on(type, async e => {
try {
config.logger.info(`process.on ${type}`);
config.logger.error(e);
StormLog.info(`process.on ${type}`);
StormLog.error(e);
await shutdown();
config.logger.info("Shutdown process complete, exiting with code 0");
StormLog.info("Shutdown process complete, exiting with code 0");
process.exit(0);
} catch (error_) {
config.logger.warn("Shutdown process failed, exiting with code 1");
config.logger.error(error_);
StormLog.warn("Shutdown process failed, exiting with code 1");
StormLog.error(error_);
process.exit(1);
}
});
Expand All @@ -55,28 +54,28 @@ export function registerShutdown(config: {
signalTraps.map(type => {
process.once(type, async () => {
try {
config.logger.info(`process.on ${type}`);
StormLog.info(`process.on ${type}`);
await shutdown();
config.logger.info("Shutdown process complete, exiting with code 0");
StormLog.info("Shutdown process complete, exiting with code 0");
process.exit(0);
} catch (error_) {
config.logger.warn("Shutdown process failed, exiting with code 1");
config.logger.error(error_);
StormLog.warn("Shutdown process failed, exiting with code 1");
StormLog.error(error_);
process.exit(1);
}
});
});

return async (reason?: string) => {
try {
config.logger.info(`Manual shutdown ${reason ? `(${reason})` : ""}`);
StormLog.info(`Manual shutdown ${reason ? `(${reason})` : ""}`);
await shutdown();
config.logger.info("Shutdown process complete, exiting with code 0");
StormLog.info("Shutdown process complete, exiting with code 0");
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
} catch (error_) {
config.logger.warn("Shutdown process failed, exiting with code 1");
config.logger.error(error_);
StormLog.warn("Shutdown process failed, exiting with code 1");
StormLog.error(error_);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from "./create-cli-options";
export * from "./execute";
export * from "./is-ci";
export * from "./is-interactive";
export * from "./write-banner";
39 changes: 39 additions & 0 deletions packages/cli/src/utilities/write-banner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { titleCase } from "@storm-stack/string-fns/title-case";
import chalk from "chalk";
import { text } from "figlet";
import { CLITitle } from "../types";

export const writeBanner = (
bannerOpts: CLITitle,
byOpts: CLITitle,
color?: string
) => {
if (bannerOpts?.hide !== true) {
text(
titleCase(bannerOpts?.name ?? "Storm CLI")!,
bannerOpts?.options ?? {
font: bannerOpts?.font ?? "Larry 3D"
},
(err, data) => {
if (err) {
return;
}

console.log(chalk.hex(color || "#FFF")(data));
}
);

if (byOpts?.hide !== true) {
text(
`by ${titleCase(byOpts?.name ?? "Storm")}`,
byOpts?.options ?? { font: byOpts?.font ?? "Doom" },
(err, data) => {
if (err) {
return;
}
console.log(chalk.hex(color || "#adbac7")(data));
}
);
}
}
};
2 changes: 1 addition & 1 deletion packages/date-time/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This package is part of the ⚡<b>Storm Stack</b> monorepo. Storm Stack packages

<h3 align="center">💻 Visit <a href="https://stormsoftware.com" target="_blank">stormsoftware.com</a> to stay up to date with this developer</h3><br />

[![Version](https://img.shields.io/badge/version-1.38.1-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)
[![Version](https://img.shields.io/badge/version-1.38.2-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)


> [!IMPORTANT]
Expand Down
2 changes: 1 addition & 1 deletion packages/errors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This package is part of the ⚡<b>Storm Stack</b> monorepo. Storm Stack packages

<h3 align="center">💻 Visit <a href="https://stormsoftware.com" target="_blank">stormsoftware.com</a> to stay up to date with this developer</h3><br />

[![Version](https://img.shields.io/badge/version-1.32.2-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)
[![Version](https://img.shields.io/badge/version-1.32.3-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)


> [!IMPORTANT]
Expand Down
26 changes: 26 additions & 0 deletions packages/errors/src/storm-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,32 @@ class StormError<
return getCauseFromUnknown(error, type, data);
}

/**
* Creates a new Exception StormError instance
*
* @param error - The validation details
* @param data - The options to use
* @returns The newly created StormError
*/
public static createException<TData = undefined>(
error?: unknown,
data: TData = undefined as TData
): StormError<string, typeof ErrorType.EXCEPTION, TData> {
return StormError.create(error, ErrorType.EXCEPTION, data);
}

/**
* Creates a new Exception StormError instance
*
* @param data - The options to use
* @returns The newly created StormError
*/
public static createFromData<TData = undefined>(
data: TData = undefined as TData
): StormError<string, typeof ErrorType.EXCEPTION, TData> {
return StormError.createException(undefined, data);
}

/**
* Creates a new Validation StormError instance
*
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This package is part of the ⚡<b>Storm Stack</b> monorepo. Storm Stack packages

<h3 align="center">💻 Visit <a href="https://stormsoftware.com" target="_blank">stormsoftware.com</a> to stay up to date with this developer</h3><br />

[![Version](https://img.shields.io/badge/version-0.15.1-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)
[![Version](https://img.shields.io/badge/version-0.15.2-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)


> [!IMPORTANT]
Expand Down
Loading

0 comments on commit c15576d

Please sign in to comment.