diff options
Diffstat (limited to 'deno/base')
| -rw-r--r-- | deno/base/config.ts | 6 | ||||
| -rw-r--r-- | deno/base/date.ts | 6 | ||||
| -rw-r--r-- | deno/base/deno.json | 6 | ||||
| -rw-r--r-- | deno/base/lib.ts | 30 | ||||
| -rw-r--r-- | deno/base/log.ts | 112 | ||||
| -rw-r--r-- | deno/base/text.ts | 3 |
6 files changed, 36 insertions, 127 deletions
diff --git a/deno/base/config.ts b/deno/base/config.ts index 8fce1d8..96cc869 100644 --- a/deno/base/config.ts +++ b/deno/base/config.ts @@ -1,4 +1,4 @@ -import { camelCaseToKebabCase } from "./text.ts"; +import { StringUtils } from "./lib.ts"; export interface ConfigDefinitionItem { readonly description: string; @@ -29,7 +29,9 @@ export class ConfigProvider<K extends string> { for (const [key, def] of Object.entries(definition as ConfigDefinition)) { map[key] = { ...def, - env: `${this.#prefix}-${camelCaseToKebabCase(key as string)}` + env: `${this.#prefix}-${ + StringUtils.camelCaseToKebabCase(key as string) + }` .replaceAll("-", "_") .toUpperCase(), }; diff --git a/deno/base/date.ts b/deno/base/date.ts deleted file mode 100644 index e65691e..0000000 --- a/deno/base/date.ts +++ /dev/null @@ -1,6 +0,0 @@ -export function toFileNameString(date: Date, dateOnly?: boolean): string { - const str = date.toISOString(); - return dateOnly === true - ? str.slice(0, str.indexOf("T")) - : str.replaceAll(/:|\./g, "-"); -} diff --git a/deno/base/deno.json b/deno/base/deno.json index 2c2d550..582f0f6 100644 --- a/deno/base/deno.json +++ b/deno/base/deno.json @@ -2,10 +2,8 @@ "name": "@crupest/base", "version": "0.1.0", "exports": { + ".": "./lib.ts", "./config": "./config.ts", - "./cron": "./cron.ts", - "./date": "./date.ts", - "./text": "./text.ts", - "./log": "./log.ts" + "./cron": "./cron.ts" } } diff --git a/deno/base/lib.ts b/deno/base/lib.ts new file mode 100644 index 0000000..af75115 --- /dev/null +++ b/deno/base/lib.ts @@ -0,0 +1,30 @@ +function camelCaseToKebabCase(str: string): string { + return str.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase()); +} + +function prependNonEmpty<T>( + object: T | null | undefined, + prefix: string = " ", +): string { + if (object == null) return ""; + const string = typeof object === "string" ? object : String(object); + return string.length === 0 ? "" : prefix + string; +} + +export const StringUtils = Object.freeze({ + camelCaseToKebabCase, + prependNonEmpty, +}); + +function toFileNameString(date: Date, dateOnly?: boolean): string { + const str = date.toISOString(); + return dateOnly === true + ? str.slice(0, str.indexOf("T")) + : str.replaceAll(/:|\./g, "-"); +} + +export const DateUtils = Object.freeze( + { + toFileNameString, + } as const, +); diff --git a/deno/base/log.ts b/deno/base/log.ts deleted file mode 100644 index cc71dfa..0000000 --- a/deno/base/log.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { join } from "@std/path"; - -import { toFileNameString } from "./date.ts"; - -export type LogLevel = "error" | "warn" | "info"; - -export interface LogOptions { - level?: LogLevel; - cause?: unknown; -} - -export interface ExternalLogStream extends Disposable { - stream: WritableStream; -} - -export class Logger { - #defaultLevel = "info" as const; - #externalLogDir?: string; - - get externalLogDir() { - return this.#externalLogDir; - } - - set externalLogDir(value: string | undefined) { - this.#externalLogDir = value; - if (value != null) { - Deno.mkdirSync(value, { - recursive: true, - }); - } - } - - write(message: string, options?: LogOptions): void { - const logFunction = console[options?.level ?? this.#defaultLevel]; - if (options?.cause != null) { - logFunction.call(console, message, options.cause); - } else { - logFunction.call(console, message); - } - } - - info(message: string) { - this.write(message, { level: "info" }); - } - - tagInfo(tag: string, message: string) { - this.info(tag + " " + message); - } - - warn(message: string) { - this.write(message, { level: "warn" }); - } - - tagWarn(tag: string, message: string) { - this.warn(tag + " " + message); - } - - error(message: string, cause?: unknown) { - this.write(message, { level: "info", cause }); - } - - tagError(tag: string, message: string, cause?: unknown) { - this.error(tag + " " + message, cause); - } - - async createExternalLogStream( - name: string, - options?: { - noTime?: boolean; - }, - ): Promise<ExternalLogStream> { - if (name.includes("/")) { - throw new Error(`External log stream's name (${name}) contains '/'.`); - } - if (this.#externalLogDir == null) { - throw new Error("External log directory is not set."); - } - - const logPath = join( - this.#externalLogDir, - options?.noTime === true - ? name - : `${name}-${toFileNameString(new Date())}`, - ); - - const file = await Deno.open(logPath, { - read: false, - write: true, - append: true, - create: true, - }); - return { - stream: file.writable, - [Symbol.dispose]: file[Symbol.dispose].bind(file), - }; - } - - async createExternalLogStreamsForProgram( - program: string, - ): Promise<{ stdout: WritableStream; stderr: WritableStream } & Disposable> { - const stdout = await this.createExternalLogStream(`${program}-stdout`); - const stderr = await this.createExternalLogStream(`${program}-stderr`); - return { - stdout: stdout.stream, - stderr: stderr.stream, - [Symbol.dispose]: () => { - stdout[Symbol.dispose](); - stderr[Symbol.dispose](); - }, - }; - } -} diff --git a/deno/base/text.ts b/deno/base/text.ts deleted file mode 100644 index f3e4020..0000000 --- a/deno/base/text.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function camelCaseToKebabCase(str: string): string { - return str.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase()); -} |
