Skip to content

Commit

Permalink
feat(monorepo): Code improvements and linting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Jul 28, 2024
1 parent 85067dd commit cc573bb
Show file tree
Hide file tree
Showing 43 changed files with 282 additions and 304 deletions.
3 changes: 2 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default storm({
"unicorn/no-useless-switch-case": 0,
"react/jsx-closing-bracket-location": 0,
"no-undef": 0,
"unicorn/consistent-function-scoping": 0
"unicorn/consistent-function-scoping": 0,
"class-methods-use-this": 0
}
});
8 changes: 3 additions & 5 deletions packages/cli/src/program/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import type { StormConfig } from "@storm-software/config";
import { createStormConfig } from "@storm-software/config-tools";
import { getCauseFromUnknown } from "@storm-stack/errors";
import { StormLog } from "@storm-stack/logging";
import {
EMPTY_STRING,
NEWLINE_STRING,
titleCase
} from "@storm-stack/utilities";
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";
Expand Down Expand Up @@ -229,6 +226,7 @@ export async function createCLIProgram(cliConfig: CLIConfig): Promise<void> {
}
} catch (error) {
logger.fatal(error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
}
16 changes: 9 additions & 7 deletions packages/cli/src/program/shutdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StormLog } from "@storm-stack/logging";
import type { MaybePromise } from "@storm-stack/utilities";
import type { MaybePromise } from "@storm-stack/types";

const errorTypes = ["unhandledRejection", "uncaughtException"];
const signalTraps = ["SIGTERM", "SIGINT", "SIGUSR2"];
Expand Down Expand Up @@ -27,9 +27,9 @@ export function registerShutdown(config: {
await shutdown();
config.logger.info("Shutdown process complete, exiting with code 0");
process.exit(0);
} catch (e) {
} catch (error_) {
config.logger.warn("Shutdown process failed, exiting with code 1");
config.logger.error(e);
config.logger.error(error_);
process.exit(1);
}
});
Expand All @@ -42,9 +42,9 @@ export function registerShutdown(config: {
await shutdown();
config.logger.info("Shutdown process complete, exiting with code 0");
process.exit(0);
} catch (e) {
} catch (error_) {
config.logger.warn("Shutdown process failed, exiting with code 1");
config.logger.error(e);
config.logger.error(error_);
process.exit(1);
}
});
Expand All @@ -55,10 +55,12 @@ export function registerShutdown(config: {
config.logger.info(`Manual shutdown ${reason ? `(${reason})` : ""}`);
await shutdown();
config.logger.info("Shutdown process complete, exiting with code 0");
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
} catch (e) {
} catch (error_) {
config.logger.warn("Shutdown process failed, exiting with code 1");
config.logger.error(e);
config.logger.error(error_);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MaybePromise } from "@storm-stack/utilities";
import { MaybePromise } from "@storm-stack/types";
import { Command } from "commander";
import { Fonts, Options } from "figlet";

Expand Down
22 changes: 11 additions & 11 deletions packages/cli/src/utilities/execute.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { StormLog } from "@storm-stack/logging";
import { StormParser } from "@storm-stack/serialization";
import { isEmptyObject } from "@storm-stack/utilities";
import { isEmptyObject } from "@storm-stack/types";
import {
ExecOptions,
StdioOptions,
execSync as extExecSync
} from "child_process";
} from "node:child_process";
import { Readable } from "node:stream";
import { promisify } from "node:util";

Expand All @@ -28,26 +28,26 @@ export const execute = (
try {
StormLog.info(
`Executing command: "${command}"${
!isEmptyObject(options)
? `, options: ${StormParser.stringify(options)}`
: ""
}${!isEmptyObject(env) ? `, env: ${StormParser.stringify(env)}` : ""}${
!stdio ? `, stdio: ${stdio}` : ""
isEmptyObject(options)
? ""
: `, options: ${StormParser.stringify(options)}`
}${isEmptyObject(env) ? "" : `, env: ${StormParser.stringify(env)}`}${
stdio ? "" : `, stdio: ${stdio}`
}`
);

return extExecSync(command, {
encoding: "utf-8",
encoding: "utf8",
env: { ...process.env, ...env },
stdio,
...options
});
} catch (e) {
} catch (error_) {
StormLog.error(`An error occurred executing command: "${command}"`);
StormLog.error(e);
StormLog.error(error_);

return (
(e as any)?.message ?? "Exception occurred while processing request "
(error_ as any)?.message ?? "Exception occurred while processing request "
);
}
};
Expand Down
6 changes: 3 additions & 3 deletions packages/date-time/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const RFC_3339_DATETIME_REGEX =
/^(\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60))(\.\d{1,})?(([Z])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
/^(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d|60))(\.\d+)?((Z)|([+|-]([01]\d|2[0-3]):[0-5]\d))$/;

export const RFC_3339_DATE_REGEX =
/^(\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))$/;
/^(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/;

export const RFC_3339_TIME_REGEX =
/^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(\.\d{1,})?(([Z])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
/^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(\.\d+)?((Z)|([+|-]([01]\d|2[0-3]):[0-5]\d))$/;
16 changes: 8 additions & 8 deletions packages/date-time/src/storm-date-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
isObject,
isSet,
isSetString
} from "@storm-stack/utilities";
} from "@storm-stack/types";
import { RFC_3339_DATETIME_REGEX } from "./constants";
import { isInstant } from "./utilities/is-instant";

Expand Down Expand Up @@ -72,6 +72,7 @@ export function deserializeStormDateTime(utcString: JsonValue): StormDateTime {
* A wrapper of the and Date class used by Storm Software to provide Date-Time values
*
* @decorator `@Serializable()`
* @class StormDateTime
*/
@Serializable()
export class StormDateTime extends Date {
Expand Down Expand Up @@ -202,11 +203,11 @@ export class StormDateTime extends Date {
if (instant && this.validate(_dateTime, options)) {
this.#instant = instant;

const timeZone = options?.timeZone
? options?.timeZone
: process.env.TZ
? process.env.TZ
: Temporal.Now.timeZoneId();
const timeZone =
options?.timeZone ||
process.env.STORM_TIMEZONE ||
process.env.TZ ||
Temporal.Now.timeZoneId();
this.#zonedDateTime = options?.calendar
? this.#instant.toZonedDateTime({
timeZone,
Expand Down Expand Up @@ -309,8 +310,7 @@ export class StormDateTime extends Date {

let datetime: string | undefined;
if (isDate(value) || isNumber(value) || isBigInt(value)) {
let date!: Date;
date =
const date =
isNumber(value) || isBigInt(value) ? new Date(Number(value)) : value;

if (Number.isNaN(date.getTime())) {
Expand Down
32 changes: 14 additions & 18 deletions packages/date-time/src/storm-date.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Temporal } from "@js-temporal/polyfill";
import { type JsonValue, Serializable } from "@storm-stack/serialization";
import {
isBigInt,
isDate,
isNumber,
isSetString
} from "@storm-stack/utilities";
import { isBigInt, isDate, isNumber, isSetString } from "@storm-stack/types";
import { RFC_3339_DATE_REGEX } from "./constants";
import type { DateTimeInput, DateTimeOptions } from "./storm-date-time";
import { StormDateTime } from "./storm-date-time";
Expand Down Expand Up @@ -38,6 +33,7 @@ export function deserializeStormDate(utcString: JsonValue): StormDate {
* A wrapper of the and Date class used by Storm Software to provide Date-Time values
*
* @decorator `@Serializable()`
* @class StormDate
*/
@Serializable()
export class StormDate extends StormDateTime {
Expand All @@ -63,7 +59,7 @@ export class StormDate extends StormDateTime {
* @returns A new instance of StormDateTime with the maximum date and time.
*/
public static override minimum(): StormDate {
return StormDate.create(new Date(-8640000000000000));
return StormDate.create(new Date(-8_640_000_000_000_000));
}

/**
Expand All @@ -72,7 +68,7 @@ export class StormDate extends StormDateTime {
* @returns A new instance of StormDateTime with the maximum date and time.
*/
public static override maximum(): StormDate {
return StormDate.create(new Date(8640000000000000));
return StormDate.create(new Date(8_640_000_000_000_000));
}

/**
Expand Down Expand Up @@ -141,12 +137,8 @@ export class StormDate extends StormDateTime {

let datetime: string | undefined;
if (isDate(value) || isNumber(value) || isBigInt(value)) {
let date!: Date;
if (isNumber(value) || isBigInt(value)) {
date = new Date(Number(value));
} else {
date = value;
}
const date =
isNumber(value) || isBigInt(value) ? new Date(Number(value)) : value;

if (Number.isNaN(date.getTime())) {
return false;
Expand Down Expand Up @@ -174,23 +166,27 @@ export class StormDate extends StormDateTime {
case 7:
case 8:
case 10:
case 12:
case 12: {
return createdDateTime.zonedDateTime.day > 31;
}

case 2:
case 2: {
return (
createdDateTime.zonedDateTime.day >
(createdDateTime.zonedDateTime.inLeapYear ? 29 : 28)
);
}

case 4:
case 6:
case 9:
case 11:
case 11: {
return createdDateTime.zonedDateTime.day > 30;
}

default:
default: {
return true;
}
}
}

Expand Down
16 changes: 4 additions & 12 deletions packages/date-time/src/storm-time.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Temporal } from "@js-temporal/polyfill";
import { type JsonValue, Serializable } from "@storm-stack/serialization";
import {
isBigInt,
isDate,
isNumber,
isSetString
} from "@storm-stack/utilities";
import { isBigInt, isDate, isNumber, isSetString } from "@storm-stack/types";
import { RFC_3339_TIME_REGEX } from "./constants";
import type { DateTimeInput, DateTimeOptions } from "./storm-date-time";
import { StormDateTime } from "./storm-date-time";
Expand Down Expand Up @@ -38,6 +33,7 @@ export function deserializeStormTime(utcString: JsonValue): StormTime {
* A wrapper of the and Date class used by Storm Software to provide Date-Time values
*
* @decorator `@Serializable()`
* @class StormTime
*/
@Serializable()
export class StormTime extends StormDateTime {
Expand Down Expand Up @@ -117,12 +113,8 @@ export class StormTime extends StormDateTime {

let datetime: string | undefined;
if (isDate(value) || isNumber(value) || isBigInt(value)) {
let date!: Date;
if (isNumber(value) || isBigInt(value)) {
date = new Date(Number(value));
} else {
date = value;
}
const date =
isNumber(value) || isBigInt(value) ? new Date(Number(value)) : value;

if (Number.isNaN(date.getTime())) {
return false;
Expand Down
29 changes: 18 additions & 11 deletions packages/date-time/src/utilities/format-date-time-iso.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Temporal } from "@js-temporal/polyfill";
import { EMPTY_STRING } from "@storm-stack/utilities";
import { EMPTY_STRING } from "@storm-stack/types";
import type { StormDateTime } from "../storm-date-time";

/**
Expand All @@ -11,16 +11,23 @@ import type { StormDateTime } from "../storm-date-time";
*/
export const formatDateTimeISO = (
dateTime?: StormDateTime | null,
options: Partial<Temporal.ZonedDateTimeToStringOptions> = {
smallestUnit: "milliseconds",
roundingMode: "ceil",
calendarName: "never",
timeZoneName: "never",
offset: "never"
}
): string =>
dateTime
options?: Partial<Temporal.ZonedDateTimeToStringOptions>
): string => {
const smallestUnit = options?.smallestUnit || "millisecond";
const roundingMode = options?.roundingMode || "ceil";
const calendarName = options?.calendarName || "never";
const timeZoneName = options?.timeZoneName || "never";
const offset = options?.offset || "never";

return dateTime
? `${dateTime.instant
.toZonedDateTimeISO(dateTime.timeZoneId ?? process.env.TZ ?? "UTC")
.toString(options)}`
.toString({
smallestUnit,
roundingMode,
calendarName,
timeZoneName,
offset
})}`
: EMPTY_STRING;
};
31 changes: 20 additions & 11 deletions packages/date-time/src/utilities/format-date-time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Temporal } from "@js-temporal/polyfill";
import { EMPTY_STRING } from "@storm-stack/utilities";
import { EMPTY_STRING } from "@storm-stack/types";
import { StormDateTime } from "../storm-date-time";

/**
Expand All @@ -11,14 +11,23 @@ import { StormDateTime } from "../storm-date-time";
*/
export const formatDateTime = (
dateTime: StormDateTime = StormDateTime.current(),
options: Partial<Temporal.ZonedDateTimeToStringOptions> = {
smallestUnit: "millisecond",
roundingMode: "ceil",
calendarName: "never",
timeZoneName: "never",
offset: "never"
}
): string =>
dateTime
? `${dateTime.zonedDateTime.toString(options).replaceAll("T", " ")}`
options?: Partial<Temporal.ZonedDateTimeToStringOptions>
): string => {
const smallestUnit = options?.smallestUnit || "millisecond";
const roundingMode = options?.roundingMode || "ceil";
const calendarName = options?.calendarName || "never";
const timeZoneName = options?.timeZoneName || "never";
const offset = options?.offset || "never";

return dateTime
? `${dateTime.zonedDateTime
.toString({
smallestUnit,
roundingMode,
calendarName,
timeZoneName,
offset
})
.replaceAll("T", " ")}`
: EMPTY_STRING;
};
Loading

0 comments on commit cc573bb

Please sign in to comment.