From 05260b858bab614b890fe7a8a0d5612012bd9c6c Mon Sep 17 00:00:00 2001 From: BluJ Date: Thu, 27 Apr 2023 12:59:27 -0600 Subject: [PATCH] feat: Only make the .const for the GetWrapperData only available on main --- lib/mainFn/index.ts | 4 ++-- lib/test/wrapperData.test.ts | 18 +++++++++++++++++- lib/util/getWrapperData.ts | 14 ++++++++++++-- lib/util/index.ts | 19 +++++++++++-------- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/mainFn/index.ts b/lib/mainFn/index.ts index fde5af8..36725b6 100644 --- a/lib/mainFn/index.ts +++ b/lib/mainFn/index.ts @@ -26,13 +26,13 @@ export const setupMain = ( fn: (o: { effects: Effects started(onTerm: () => void): null - utils: Utils + utils: Utils }) => Promise>, ): ExpectedExports.main => { return async (options) => { const result = await fn({ ...options, - utils: utils(options.effects), + utils: utils(options.effects), }) await result.build().then((x) => x.wait()) } diff --git a/lib/test/wrapperData.test.ts b/lib/test/wrapperData.test.ts index 4aba8ff..71ab605 100644 --- a/lib/test/wrapperData.test.ts +++ b/lib/test/wrapperData.test.ts @@ -43,21 +43,37 @@ describe("wrapperData", () => { path: "/config/some2Value", value: "someValueIn", }) + ;(await utils(todo()) + .getOwnWrapperData("/config/someValue") + .const()) satisfies string + ;(await utils(todo()) + .getOwnWrapperData("/config") + .const()) satisfies WrapperType["config"] + await utils(todo()) + // @ts-expect-error Path is wrong + .getOwnWrapperData("/config/somdsfeValue") + .const() + /// ;(await utils(todo()) .getOwnWrapperData("/config/someValue") + // @ts-expect-error Const should normally not be callable .const()) satisfies string ;(await utils(todo()) .getOwnWrapperData("/config") + // @ts-expect-error Const should normally not be callable .const()) satisfies WrapperType["config"] await utils(todo()) // @ts-expect-error Path is wrong .getOwnWrapperData("/config/somdsfeValue") + // @ts-expect-error Const should normally not be callable .const() + + /// ;(await utils(todo()) .getOwnWrapperData("/config/someValue") // @ts-expect-error satisfies type is wrong .const()) satisfies number - ;(await utils(todo()) + ;(await utils(todo()) // @ts-expect-error Path is wrong .getOwnWrapperData("/config/") .const()) satisfies WrapperType["config"] diff --git a/lib/util/getWrapperData.ts b/lib/util/getWrapperData.ts index c90e7af..3a8b2bb 100644 --- a/lib/util/getWrapperData.ts +++ b/lib/util/getWrapperData.ts @@ -2,7 +2,7 @@ import { Parser } from "ts-matches" import { Effects, EnsureWrapperDataPath, ExtractWrapperData } from "../types" import { NoAny } from "." -export class WrapperData { +export class GetWrapperData { constructor( readonly effects: Effects, readonly path: Path & EnsureWrapperDataPath, @@ -12,6 +12,9 @@ export class WrapperData { } = {}, ) {} + /** This should be used as the primary method in main since it allows the main to + * restart if the wrapper data changes + */ const() { return this.effects.getWrapperData({ ...this.options, @@ -19,6 +22,10 @@ export class WrapperData { callback: this.effects.restart, }) } + /** + * Returns the wrapper data once and then never again + * Doesn't restart the server when the wrapper data changes + */ once() { return this.effects.getWrapperData({ ...this.options, @@ -26,6 +33,9 @@ export class WrapperData { callback: () => {}, }) } + /** + * Keeps giving the latest wrapper data as it changes + */ async *watch() { while (true) { let callback: () => void @@ -49,5 +59,5 @@ export function getWrapperData( packageId?: string | undefined } = {}, ) { - return new WrapperData(effects, path as any, options) + return new GetWrapperData(effects, path as any, options) } diff --git a/lib/util/index.ts b/lib/util/index.ts index 75265f0..c8418dc 100644 --- a/lib/util/index.ts +++ b/lib/util/index.ts @@ -2,7 +2,7 @@ import { Parser } from "ts-matches" import * as T from "../types" import FileHelper from "./fileHelper" import nullIfEmpty from "./nullIfEmpty" -import { WrapperData, getWrapperData } from "./getWrapperData" +import { GetWrapperData, getWrapperData } from "./getWrapperData" import { CheckResult, checkPortListening, @@ -54,7 +54,7 @@ export type WrapperDataOptionals = { packageId?: string | undefined } -export type Utils = { +export type Utils = { readFile: (fileHelper: FileHelper) => ReturnType["read"]> writeFile: ( fileHelper: FileHelper, @@ -63,10 +63,10 @@ export type Utils = { getWrapperData: ( packageId: string, path: T.EnsureWrapperDataPath, - ) => WrapperData + ) => GetWrapperData & WrapperOverWrite getOwnWrapperData: ( path: T.EnsureWrapperDataPath, - ) => WrapperData + ) => GetWrapperData & WrapperOverWrite setOwnWrapperData: ( path: T.EnsureWrapperDataPath, value: ExtractWrapperData, @@ -94,9 +94,9 @@ export type Utils = { exists: (props: { path: string; volumeId: string }) => Promise nullIfEmpty: typeof nullIfEmpty } -export const utils = ( +export const utils = ( effects: T.Effects, -): Utils => ({ +): Utils => ({ readFile: (fileHelper: FileHelper) => fileHelper.read(effects), writeFile: (fileHelper: FileHelper, data: A) => fileHelper.write(data, effects), @@ -105,10 +105,13 @@ export const utils = ( getWrapperData: ( packageId: string, path: T.EnsureWrapperDataPath, - ) => getWrapperData(effects, path as any, { packageId }), + ) => + getWrapperData(effects, path as any, { + packageId, + }) as any, getOwnWrapperData: ( path: T.EnsureWrapperDataPath, - ) => getWrapperData(effects, path as any), + ) => getWrapperData(effects, path as any) as any, setOwnWrapperData: ( path: T.EnsureWrapperDataPath, value: ExtractWrapperData,