Skip to content

Commit

Permalink
separate validator in filehelper
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bonez committed Nov 13, 2024
1 parent 51d09c0 commit e535141
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions sdk/package/lib/util/fileHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}

/**
Expand All @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -156,30 +163,30 @@ 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: 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))
}

/**
* We wanted to be able to have a fileHelper, and just modify the path later in time.
* 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)
}

/**
Expand All @@ -190,22 +197,20 @@ 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.
*/
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),
)
}
/**
Expand All @@ -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),
)
}
/**
Expand All @@ -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),
)
}
}
Expand Down

0 comments on commit e535141

Please sign in to comment.