mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-04 22:39:46 +00:00
sdk input spec improvements (#2785)
* sdk input spec improvements * more sdk changes * fe changes * alpha.14 * fix tests * separate validator in filehelper * use deeppartial for getinput * fix union type and update ts-matches * alpha.15 * alpha.16 * alpha.17 --------- Co-authored-by: Matt Hill <mattnine@protonmail.com>
This commit is contained in:
@@ -80,7 +80,8 @@ export class FileHelper<A> {
|
||||
protected constructor(
|
||||
readonly path: string,
|
||||
readonly writeData: (dataIn: A) => string,
|
||||
readonly readData: (stringValue: string) => A,
|
||||
readonly readData: (stringValue: string) => unknown,
|
||||
readonly validate: (value: unknown) => A,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -97,10 +98,7 @@ export class FileHelper<A> {
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the file from disk and converts it to structured data.
|
||||
*/
|
||||
private async readOnce(): Promise<A | null> {
|
||||
private async readFile(): Promise<unknown> {
|
||||
if (!(await exists(this.path))) {
|
||||
return null
|
||||
}
|
||||
@@ -109,6 +107,15 @@ export class FileHelper<A> {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the file from disk and converts it to structured data.
|
||||
*/
|
||||
private async readOnce(): Promise<A | null> {
|
||||
const data = await this.readFile()
|
||||
if (!data) return null
|
||||
return this.validate(data)
|
||||
}
|
||||
|
||||
private async readConst(effects: T.Effects): Promise<A | null> {
|
||||
const watch = this.readWatch()
|
||||
const res = await watch.next()
|
||||
@@ -156,22 +163,22 @@ export class FileHelper<A> {
|
||||
* Accepts full structured data and performs a merge with the existing file on disk if it exists.
|
||||
*/
|
||||
async write(data: A) {
|
||||
const fileData = (await this.readOnce()) || {}
|
||||
const fileData = (await this.readFile()) || {}
|
||||
const mergeData = merge({}, fileData, data)
|
||||
return await this.writeFile(mergeData)
|
||||
return await this.writeFile(this.validate(mergeData))
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts partial structured data and performs a merge with the existing file on disk.
|
||||
*/
|
||||
async merge(data: Partial<A>) {
|
||||
async merge(data: T.DeepPartial<A>) {
|
||||
const fileData =
|
||||
(await this.readOnce()) ||
|
||||
(await this.readFile()) ||
|
||||
(() => {
|
||||
throw new Error(`${this.path}: does not exist`)
|
||||
})()
|
||||
const mergeData = merge({}, fileData, data)
|
||||
return await this.writeFile(mergeData)
|
||||
return await this.writeFile(this.validate(mergeData))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,7 +186,7 @@ export class FileHelper<A> {
|
||||
* Like one behaviour of another dependency or something similar.
|
||||
*/
|
||||
withPath(path: string) {
|
||||
return new FileHelper<A>(path, this.writeData, this.readData)
|
||||
return new FileHelper<A>(path, this.writeData, this.readData, this.validate)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,9 +197,10 @@ export class FileHelper<A> {
|
||||
static raw<A>(
|
||||
path: string,
|
||||
toFile: (dataIn: A) => string,
|
||||
fromFile: (rawData: string) => A,
|
||||
fromFile: (rawData: string) => unknown,
|
||||
validate: (data: unknown) => A,
|
||||
) {
|
||||
return new FileHelper<A>(path, toFile, fromFile)
|
||||
return new FileHelper<A>(path, toFile, fromFile, validate)
|
||||
}
|
||||
/**
|
||||
* Create a File Helper for a .json file.
|
||||
@@ -200,12 +208,9 @@ export class FileHelper<A> {
|
||||
static json<A>(path: string, shape: matches.Validator<unknown, A>) {
|
||||
return new FileHelper<A>(
|
||||
path,
|
||||
(inData) => {
|
||||
return JSON.stringify(inData, null, 2)
|
||||
},
|
||||
(inString) => {
|
||||
return shape.unsafeCast(JSON.parse(inString))
|
||||
},
|
||||
(inData) => JSON.stringify(inData, null, 2),
|
||||
(inString) => JSON.parse(inString),
|
||||
(data) => shape.unsafeCast(data),
|
||||
)
|
||||
}
|
||||
/**
|
||||
@@ -217,12 +222,9 @@ export class FileHelper<A> {
|
||||
) {
|
||||
return new FileHelper<A>(
|
||||
path,
|
||||
(inData) => {
|
||||
return TOML.stringify(inData as any)
|
||||
},
|
||||
(inString) => {
|
||||
return shape.unsafeCast(TOML.parse(inString))
|
||||
},
|
||||
(inData) => TOML.stringify(inData as any),
|
||||
(inString) => TOML.parse(inString),
|
||||
(data) => shape.unsafeCast(data),
|
||||
)
|
||||
}
|
||||
/**
|
||||
@@ -234,12 +236,9 @@ export class FileHelper<A> {
|
||||
) {
|
||||
return new FileHelper<A>(
|
||||
path,
|
||||
(inData) => {
|
||||
return YAML.stringify(inData, null, 2)
|
||||
},
|
||||
(inString) => {
|
||||
return shape.unsafeCast(YAML.parse(inString))
|
||||
},
|
||||
(inData) => YAML.stringify(inData, null, 2),
|
||||
(inString) => YAML.parse(inString),
|
||||
(data) => shape.unsafeCast(data),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user