mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-04 22:39:46 +00:00
sdk improvements (#2877)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user