Skip to content

Commit

Permalink
fix: 修复config.sample.yaml缺失问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-cn committed May 16, 2023
1 parent d2fc853 commit b6f720e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 58 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"start": "node .",
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && cp -r src/config.sample.yaml lib/config.sample.yaml",
"dev": "ts-node-dev -r tsconfig-paths/register ./src/bin.ts -c config.yaml",
"pub": "npm publish --access public",
"docs:dev": "vitepress dev docs --port 8989",
Expand Down Expand Up @@ -54,6 +54,7 @@
],
"dependencies": {
"@koa/router": "^10.1.1",
"@zhinjs/shared": "^0.0.9",
"icqq": "^0.3.6",
"icqq-cq-enable": "^1.0.0",
"js-yaml": "^4.1.0",
Expand Down
96 changes: 39 additions & 57 deletions src/db.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,61 @@
import {writeFileSync, readFileSync, existsSync} from "fs";
import {readFileSync, writeFileSync, existsSync} from "fs";
import {deleteValue, getValue, Keys, setValue, Value} from "@zhinjs/shared";

export class Db {
private data: Record<string, any> = {}
private raw_data: string = '{}'
export class Database<T extends object = object> {
private data: T = {} as any

constructor(private readonly path: string, force_create: boolean = true) {
constructor(private readonly path: string) {
if (!this.path.toLowerCase().endsWith('.json')) this.path = this.path + '.json'
if (!existsSync(this.path) && force_create) {
writeFileSync(this.path, this.raw_data)
}
try {
const raw_data = readFileSync(this.path, 'utf8');
this.raw_data = raw_data || this.raw_data;
this.data = JSON.parse(this.raw_data) as Record<string | symbol, any>
} catch (error) {
const {message} = error as Error;
if (!message.includes('ENOENT: no such file or directory')) {
throw error;
}
if (!existsSync(this.path)) {
writeFileSync(this.path, "", "utf-8")
}
}

get(key: string, escape: boolean = true) {
const func = new Function(`return this.data.${key}`)
const _this = this
let result
sync(defaultValue: T) {
try {
result = escape ? func.apply(this) : this.data[key]
this.data = JSON.parse(readFileSync(this.path, 'utf-8'))
} catch {
throw new Error('不可达的位置:' + key.toString())
this.data = defaultValue
writeFileSync(this.path, JSON.stringify(defaultValue, null, 2), "utf-8")
}
}

get<K extends Keys<T>>(key: K): Value<T, K> {
const value = getValue(this.data, key)
if (typeof value !== 'object') return value as Value<T, K>
const saveValue = () => {
this.set(key, value as Value<T, K>)
}
return result && typeof result === 'object' ? new Proxy(result, {
get(target: any, p: string | symbol, receiver: any): any {
return Reflect.get(target, p, receiver)
return new Proxy(value, {
set(target, k, v) {
const res = Reflect.set(target, k, v)
saveValue()
return res
},
deleteProperty(target, p) {
const res = Reflect.deleteProperty(target, p)
saveValue()
return res
},
set(target: any, p: string | symbol, value: any, receiver: any): boolean {
const res = Reflect.set(target, p, value, receiver)
_this.set(key, result)
defineProperty(target, p, r) {
const res = Reflect.defineProperty(target, p, r)
saveValue()
return res
}
}) : result
}

set(key: string, value: any, escape: boolean = true) {
const func = new Function('value', `return this.data.${key}=value`)
let result = escape ? func.apply(this, [value]) : this.data[key] = value
this.write()
return result
}

has(key: string, escape: boolean = true): boolean {
return escape ? new Function(`return !!this.data.${key}`).apply(this) : !!this.data[key]
}) as Value<T, K>
}

delete(key: string, escape: boolean = true): boolean {
const result: boolean = escape ? new Function(`return delete this.data.${key}`).apply(this) : delete this.data[key]
this.write()
return result
delete<K extends Keys<T>>(key: K) {
return deleteValue(this.data,key)
}

write() {
try {
const raw_data = JSON.stringify(this.data);
if (raw_data !== this.raw_data) {
writeFileSync(this.path, raw_data);
this.raw_data = raw_data;
}
} catch (error) {
this.data = JSON.parse(this.raw_data);
throw error;
}
set<K extends Keys<T>>(key: K, value: Value<T, K>) {
setValue(this.data, key, value)
return writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf-8')
}
}

export namespace Db {
export namespace Database {
export interface Config {
path: string
force?: boolean
Expand Down

0 comments on commit b6f720e

Please sign in to comment.