npmextra/ts/npmextra.classes.appdata.ts

103 lines
2.7 KiB
TypeScript
Raw Permalink 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';
2024-02-07 20:44:00 +00:00
export interface IAppDataOptions {
dirPath?: string;
requiredKeys?: string[];
2024-02-09 14:57:32 +00:00
/**
* kvStoreKey: 'MY_ENV_VAR'
*/
envMapping?: {
[key: string]: string;
};
2024-02-07 20:44:00 +00:00
}
2023-08-24 08:39:47 +00:00
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
2024-02-09 14:57:32 +00:00
* @returns
2024-01-25 12:57:55 +00:00
*/
2024-02-07 20:44:00 +00:00
public static async createAndInit(optionsArg: IAppDataOptions = {}) {
const appData = new AppData(optionsArg);
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();
2024-02-07 20:44:00 +00:00
public options: IAppDataOptions;
2023-08-24 08:39:47 +00:00
private kvStore: KeyValueStore;
2024-02-07 20:44:00 +00:00
constructor(optionsArg: IAppDataOptions = {}) {
this.options = optionsArg;
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-02-07 20:44:00 +00:00
if (this.options.dirPath) {
2024-01-25 12:57:55 +00:00
// 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) {
2024-02-07 20:44:00 +00:00
this.options.dirPath = appDataDir;
2023-08-24 20:59:43 +00:00
} else if (dataExists) {
2024-02-07 20:44:00 +00:00
this.options.dirPath = dataDir;
2023-08-24 20:59:43 +00:00
} else {
await plugins.smartfile.fs.ensureDir(nogitAppData);
2024-02-07 20:44:00 +00:00
this.options.dirPath = nogitAppData;
2023-08-24 08:39:47 +00:00
}
}
2024-02-09 14:57:32 +00:00
this.kvStore = new KeyValueStore(
'custom',
'appkv',
this.options.dirPath,
this.options.requiredKeys
);
if (this.options.envMapping) {
const qenvInstance = new plugins.qenv.Qenv(process.cwd(), '~/.cloudlyrc');
for (const key in this.options.envMapping) {
const envValue = await qenvInstance.getEnvVarOnDemand(key);
if (envValue) {
await this.kvStore.writeKey(key, envValue);
}
}
}
2023-08-24 08:39:47 +00:00
this.readyDeferred.resolve();
}
/**
* returns a kvtore that resides in appdata
*/
public async getKvStore() {
await this.readyDeferred.promise;
return this.kvStore;
}
2024-02-09 10:52:30 +00:00
public async logMissingKeys() {
const kvStore = await this.getKvStore();
const missingMandatoryKeys = kvStore.getMissingMandatoryKeys();
if (missingMandatoryKeys.length > 0) {
2024-02-09 14:57:32 +00:00
console.log(
`The following mandatory keys are missing in the appdata:\n -> ${missingMandatoryKeys.join(
',\n -> '
)}`
);
2024-02-09 10:52:30 +00:00
} else {
console.log('All mandatory keys are present in the appdata');
}
}
2024-02-09 14:57:32 +00:00
}