npmextra/ts/npmextra.classes.appdata.ts

60 lines
1.6 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 {
2024-01-25 12:57:55 +00:00
/**
* creates appdata. If no pathArg is given, data will be stored here:
* ${PWD}/.nogit/appdata
* @param pathArg
* @returns
*/
2023-08-26 08:56:20 +00:00
public static async createAndInit(pathArg?: string) {
2023-08-26 07:56:20 +00:00
const appData = new AppData(pathArg);
2024-01-25 12:57:55 +00:00
await appData.readyDeferred.promise;
2023-08-26 07:56:20 +00:00
return appData;
}
// instance
2023-08-24 08:39:47 +00:00
public readyDeferred = plugins.smartpromise.defer();
public dirPathArg: string;
private kvStore: KeyValueStore;
constructor(pathArg?: string) {
2024-01-25 12:57:55 +00:00
this.dirPathArg = pathArg;
2024-01-27 18:03:06 +00:00
this.init();
2023-08-24 08:39:47 +00:00
}
2024-01-25 12:57:55 +00:00
/**
* inits app data
* @param pathArg
*/
2023-08-24 08:39:47 +00:00
private async init(pathArg?: string) {
2024-01-25 12:57:55 +00:00
if (this.dirPathArg) {
// ok, nothing to do here;
2023-08-24 08:39:47 +00:00
} else {
const appDataDir = '/app/data';
const dataDir = '/data';
2023-08-24 20:59:43 +00:00
const nogitAppData = '.nogit/appdata';
2023-08-24 08:39:47 +00:00
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
if (appDataExists) {
this.dirPathArg = appDataDir;
2023-08-24 20:59:43 +00:00
} else if (dataExists) {
2023-08-24 08:39:47 +00:00
this.dirPathArg = dataDir;
2023-08-24 20:59:43 +00:00
} else {
await plugins.smartfile.fs.ensureDir(nogitAppData);
this.dirPathArg = nogitAppData;
2023-08-24 08:39:47 +00:00
}
}
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;
}
}