60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import * as plugins from './npmextra.plugins.js';
|
|
import * as paths from './npmextra.paths.js';
|
|
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
|
|
|
|
export class AppData {
|
|
/**
|
|
* creates appdata. If no pathArg is given, data will be stored here:
|
|
* ${PWD}/.nogit/appdata
|
|
* @param pathArg
|
|
* @returns
|
|
*/
|
|
public static async createAndInit(pathArg?: string) {
|
|
const appData = new AppData(pathArg);
|
|
await appData.readyDeferred.promise;
|
|
return appData;
|
|
}
|
|
|
|
// instance
|
|
public readyDeferred = plugins.smartpromise.defer();
|
|
public dirPathArg: string;
|
|
private kvStore: KeyValueStore;
|
|
constructor(pathArg?: string) {
|
|
this.dirPathArg = pathArg;
|
|
this.init();
|
|
}
|
|
|
|
/**
|
|
* inits app data
|
|
* @param pathArg
|
|
*/
|
|
private async init(pathArg?: string) {
|
|
if (this.dirPathArg) {
|
|
// ok, nothing to do here;
|
|
} else {
|
|
const appDataDir = '/app/data';
|
|
const dataDir = '/data';
|
|
const nogitAppData = '.nogit/appdata';
|
|
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
|
|
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
|
|
if (appDataExists) {
|
|
this.dirPathArg = appDataDir;
|
|
} else if (dataExists) {
|
|
this.dirPathArg = dataDir;
|
|
} else {
|
|
await plugins.smartfile.fs.ensureDir(nogitAppData);
|
|
this.dirPathArg = nogitAppData;
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
} |