sdk improvements (#2877)

This commit is contained in:
Aiden McClelland
2025-04-16 12:53:10 -06:00
committed by GitHub
parent 89f3fdc05f
commit 47b6509f70
11 changed files with 207 additions and 144 deletions

View File

@@ -1,6 +1,7 @@
import * as matches from "ts-matches"
import * as YAML from "yaml"
import * as TOML from "@iarna/toml"
import * as INI from "ini"
import * as T from "../../../base/lib/types"
import * as fs from "node:fs/promises"
import { asError, partialDiff } from "../../../base/lib/util"
@@ -285,6 +286,7 @@ export class FileHelper<A> {
) {
return new FileHelper<A>(path, toFile, fromFile, validate)
}
/**
* Create a File Helper for a .json file.
*/
@@ -296,6 +298,7 @@ export class FileHelper<A> {
(data) => shape.unsafeCast(data),
)
}
/**
* Create a File Helper for a .toml file
*/
@@ -310,6 +313,7 @@ export class FileHelper<A> {
(data) => shape.unsafeCast(data),
)
}
/**
* Create a File Helper for a .yaml file
*/
@@ -324,6 +328,41 @@ export class FileHelper<A> {
(data) => shape.unsafeCast(data),
)
}
static ini<A extends Record<string, unknown>>(
path: string,
shape: matches.Validator<unknown, A>,
options?: INI.EncodeOptions & INI.DecodeOptions,
) {
return new FileHelper<A>(
path,
(inData) => INI.stringify(inData, options),
(inString) => INI.parse(inString, options),
(data) => shape.unsafeCast(data),
)
}
static env<A extends Record<string, string>>(
path: string,
shape: matches.Validator<unknown, A>,
) {
return new FileHelper<A>(
path,
(inData) =>
Object.entries(inData)
.map(([k, v]) => `${k}=${v}`)
.join("\n"),
(inString) =>
Object.fromEntries(
inString
.split("\n")
.map((line) => line.trim())
.filter((line) => !line.startsWith("#") && line.includes("="))
.map((line) => line.split("=", 2)),
),
(data) => shape.unsafeCast(data),
)
}
}
export default FileHelper