From fd7b218dc31f51d3b6f969a18fe68121bf74eab3 Mon Sep 17 00:00:00 2001 From: Emma Date: Fri, 18 Oct 2024 19:30:05 -0600 Subject: [PATCH] Fixes poor typing on get Adds ensureFile --- deno.json | 3 ++- deno.lock | 1 + store.ts | 12 +++++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/deno.json b/deno.json index 65be207..072f1a1 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@bearmetal/store", - "version": "0.0.1", + "version": "0.0.2", "description": "A simple store for storing data in a JSON file.", "files": [ "mod.ts", @@ -9,6 +9,7 @@ "exports": "./mod.ts", "imports": { "@std/assert": "jsr:@std/assert@^1.0.6", + "@std/fs": "jsr:@std/fs@^1.0.4", "@std/testing": "jsr:@std/testing@^1.0.3" } } diff --git a/deno.lock b/deno.lock index f4a6992..1f4fcb3 100644 --- a/deno.lock +++ b/deno.lock @@ -44,6 +44,7 @@ "workspace": { "dependencies": [ "jsr:@std/assert@^1.0.6", + "jsr:@std/fs@^1.0.4", "jsr:@std/testing@^1.0.3" ] } diff --git a/store.ts b/store.ts index c21ffa9..bf4e3a7 100644 --- a/store.ts +++ b/store.ts @@ -1,5 +1,9 @@ +import { ensureFileSync } from "@std/fs"; + const REFRESH_EVENT = "bm:refresh-store"; +type StoreValue = string | number | boolean; + /** * A no-dep, lightweight, simple store for storing data in a JSON file. * usage: @@ -37,6 +41,8 @@ export class BearMetalStore { constructor(storePath?: string) { this.storePath = storePath || Deno.env.get("BEAR_METAL_STORE_PATH") || "./BearMetal/store.json"; + + ensureFileSync(this.storePath); this.EVENT_NAME = REFRESH_EVENT + this.storePath; this.readIn(); @@ -47,8 +53,8 @@ export class BearMetalStore { * @param key * @returns The value stored at the key, or undefined if the key doesn't exist. */ - public get(key: string): string | number | boolean { - return this.store[key]; + public get(key: string): T { + return this.store[key] as T; } /** @@ -56,7 +62,7 @@ export class BearMetalStore { * @param key * @param value */ - public set(key: string, value: string | number | boolean): void { + public set(key: string, value: StoreValue): void { this.store[key] = value; this.writeOut(); }