npmextra/ts/npmextra.classes.appdata.ts
2024-04-14 02:10:29 +02:00

150 lines
4.6 KiB
TypeScript

import * as plugins from './npmextra.plugins.js';
import * as paths from './npmextra.paths.js';
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
import { env } from 'process';
export interface IAppDataOptions {
dirPath?: string;
requiredKeys?: string[];
/**
* kvStoreKey: 'MY_ENV_VAR'
*/
envMapping?: {
[key: string]: string | object;
};
}
export class AppData<T = any> {
/**
* creates appdata. If no pathArg is given, data will be stored here:
* ${PWD}/.nogit/appdata
* @param pathArg
* @returns
*/
public static async createAndInit<T = any>(optionsArg: IAppDataOptions = {}): Promise<AppData<T>> {
const appData = new AppData<T>(optionsArg);
await appData.readyDeferred.promise;
return appData;
}
// instance
public readyDeferred = plugins.smartpromise.defer();
public options: IAppDataOptions;
private kvStore: KeyValueStore<T>;
constructor(optionsArg: IAppDataOptions = {}) {
this.options = optionsArg;
this.init();
}
/**
* inits app data
* @param pathArg
*/
private async init(pathArg?: string) {
if (this.options.dirPath) {
// 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.options.dirPath = appDataDir;
} else if (dataExists) {
this.options.dirPath = dataDir;
} else {
await plugins.smartfile.fs.ensureDir(nogitAppData);
this.options.dirPath = nogitAppData;
}
}
this.kvStore = new KeyValueStore({
typeArg: 'custom',
identityArg: 'appkv',
customPath: this.options.dirPath,
mandatoryKeys: this.options.requiredKeys
});
if (this.options.envMapping) {
const qenvInstance = new plugins.qenv.Qenv(process.cwd(), plugins.path.join(process.cwd(), '.nogit'));
// Recursive function to handle nested objects, now includes key parameter
const processEnvMapping = async (key: string, mappingValue: any, parentKey: string = ''): Promise<any> => {
if (typeof mappingValue === 'string') {
let envValue: string;
if (mappingValue.startsWith('hard:')) {
envValue = mappingValue.replace('hard:', '');
} else {
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue);
}
if (envValue) {
if (mappingValue.endsWith('_JSON')) {
envValue = JSON.parse(envValue);
}
if (!parentKey) {
await this.kvStore.writeKey(key, envValue);
} else {
return envValue;
}
} else {
return undefined;
}
} else if (typeof mappingValue === 'object' && mappingValue !== null) {
const resultObject = {};
for (const innerKey in mappingValue) {
const nestedValue = mappingValue[innerKey];
// For nested objects, call recursively but do not immediately write to kvStore
const nestedResult = await processEnvMapping(innerKey, nestedValue, key);
resultObject[innerKey] = nestedResult;
}
if (parentKey === '') {
// Only write to kvStore if at the top level
await this.kvStore.writeKey(key, resultObject);
} else {
// For nested objects, return the constructed object instead of writing to kvStore
return resultObject;
}
}
};
for (const key in this.options.envMapping) {
await processEnvMapping(key, this.options.envMapping[key]);
}
}
this.readyDeferred.resolve();
}
/**
* returns a kvtore that resides in appdata
*/
public async getKvStore() {
await this.readyDeferred.promise;
return this.kvStore;
}
public async logMissingKeys() {
const kvStore = await this.getKvStore();
const missingMandatoryKeys = await kvStore.getMissingMandatoryKeys();
if (missingMandatoryKeys.length > 0) {
console.log(
`The following mandatory keys are missing in the appdata:\n -> ${missingMandatoryKeys.join(
',\n -> '
)}`
);
} else {
console.log('All mandatory keys are present in the appdata');
}
return missingMandatoryKeys;
}
public async waitForAndGetKey(keyArg: string) {
await this.readyDeferred.promise;
await this.kvStore.waitForKeysPresent([keyArg]);
return this.kvStore.readKey[keyArg];
}
}