mirror of
https://github.com/cupcakearmy/memiens.git
synced 2024-12-22 07:56:26 +00:00
don't make the app crash when the config file is already present but empty.
This commit is contained in:
parent
52ba538e82
commit
ba0c7fa9de
18
src/index.ts
18
src/index.ts
@ -1,14 +1,17 @@
|
|||||||
import { readFileSync, writeFileSync, lstatSync } from 'fs'
|
import { readFileSync, writeFileSync } from 'fs'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import yaml from 'yaml'
|
import * as yaml from 'yaml'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type AllowedYamlTypes = string | number | boolean
|
type AllowedYamlTypes = string | number | boolean
|
||||||
type AllowedTypes = AllowedYamlTypes | AllowedYamlTypes[]
|
type AllowedTypes = AllowedYamlTypes | AllowedYamlTypes[]
|
||||||
|
|
||||||
export default class Memento {
|
export default class Memiens {
|
||||||
private readonly file: string
|
private readonly file: string
|
||||||
private state: any
|
private state: any
|
||||||
|
|
||||||
|
|
||||||
constructor(file: string) {
|
constructor(file: string) {
|
||||||
this.file = resolve(file)
|
this.file = resolve(file)
|
||||||
try {
|
try {
|
||||||
@ -19,7 +22,8 @@ export default class Memento {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public get<AllowedTypes>(setting: string, defaultValue: AllowedTypes | undefined = undefined): AllowedTypes {
|
|
||||||
|
public get<T extends AllowedTypes>(setting: string, defaultValue: T | undefined = undefined): T {
|
||||||
const props = setting.split('.')
|
const props = setting.split('.')
|
||||||
let root = this.state
|
let root = this.state
|
||||||
try {
|
try {
|
||||||
@ -29,13 +33,13 @@ export default class Memento {
|
|||||||
}
|
}
|
||||||
if (root) return root
|
if (root) return root
|
||||||
if (defaultValue) {
|
if (defaultValue) {
|
||||||
// @ts-ignore
|
|
||||||
this.set(setting, defaultValue)
|
this.set(setting, defaultValue)
|
||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
throw new Error('Could not load the setting')
|
throw new Error('Could not load the setting')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public set(setting: string, value: AllowedTypes): void {
|
public set(setting: string, value: AllowedTypes): void {
|
||||||
const props = setting.split('.')
|
const props = setting.split('.')
|
||||||
let root = this.state
|
let root = this.state
|
||||||
@ -47,10 +51,12 @@ export default class Memento {
|
|||||||
this.save()
|
this.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private read() {
|
private read() {
|
||||||
this.state = yaml.parse(readFileSync(this.file, 'utf-8'))
|
this.state = yaml.parse(readFileSync(this.file, 'utf-8')) || {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private save() {
|
private save() {
|
||||||
writeFileSync(this.file, yaml.stringify(this.state), 'utf-8')
|
writeFileSync(this.file, yaml.stringify(this.state), 'utf-8')
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user