diff options
Diffstat (limited to 'deno/base')
| -rw-r--r-- | deno/base/config.ts | 6 | ||||
| -rw-r--r-- | deno/base/deno.json | 3 | ||||
| -rw-r--r-- | deno/base/lib.ts | 24 | ||||
| -rw-r--r-- | deno/base/log.ts | 60 |
4 files changed, 27 insertions, 66 deletions
diff --git a/deno/base/config.ts b/deno/base/config.ts index a5f5d86..96cc869 100644 --- a/deno/base/config.ts +++ b/deno/base/config.ts @@ -1,4 +1,4 @@ -import { camelCaseToKebabCase } from "./lib.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/deno.json b/deno/base/deno.json index dabc02a..582f0f6 100644 --- a/deno/base/deno.json +++ b/deno/base/deno.json @@ -4,7 +4,6 @@ "exports": { ".": "./lib.ts", "./config": "./config.ts", - "./cron": "./cron.ts", - "./log": "./log.ts" + "./cron": "./cron.ts" } } diff --git a/deno/base/lib.ts b/deno/base/lib.ts index a5e4a6a..af75115 100644 --- a/deno/base/lib.ts +++ b/deno/base/lib.ts @@ -1,10 +1,30 @@ -export function camelCaseToKebabCase(str: string): string { +function camelCaseToKebabCase(str: string): string { return str.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase()); } -export function toFileNameString(date: Date, dateOnly?: boolean): string { +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 940f569..0000000 --- a/deno/base/log.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { join } from "@std/path"; - -import { toFileNameString } from "./lib.ts"; - -export interface ExternalLogStream extends Disposable { - stream: WritableStream; -} - -export class LogFileProvider { - #directory: string; - - constructor(directory: string) { - this.#directory = directory; - Deno.mkdirSync(directory, { recursive: true }); - } - - async createExternalLogStream( - name: string, - options?: { - noTime?: boolean; - }, - ): Promise<ExternalLogStream> { - if (name.includes("/")) { - throw new Error(`External log stream's name (${name}) contains '/'.`); - } - - const logPath = join( - this.#directory, - 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](); - }, - }; - } -} |
