npmextra/ts/npmextra.classes.appdata.ts

37 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-08-24 08:39:47 +00:00
import * as plugins from './npmextra.plugins.js';
import * as paths from './npmextra.paths.js';
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
export class AppData {
public readyDeferred = plugins.smartpromise.defer();
public dirPathArg: string;
private kvStore: KeyValueStore;
constructor(pathArg?: string) {
this.init(pathArg);
}
private async init(pathArg?: string) {
if (pathArg) {
this.dirPathArg = pathArg;
} else {
const appDataDir = '/app/data';
const dataDir = '/data';
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
if (appDataExists) {
this.dirPathArg = appDataDir;
this.dirPathArg = dataDir;
}
}
this.kvStore = new KeyValueStore('custom', 'appkv', this.dirPathArg);
this.readyDeferred.resolve();
}
/**
* returns a kvtore that resides in appdata
*/
public async getKvStore() {
await this.readyDeferred.promise;
return this.kvStore;
}
}