fix(core): update

This commit is contained in:
2024-06-12 20:04:04 +02:00
parent 40ad00e75a
commit bb72e1c2c0
6 changed files with 5012 additions and 3602 deletions

View File

@@ -3,15 +3,15 @@ import * as paths from './npmextra.paths.js';
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
import { env } from 'process';
export interface IAppDataOptions {
export interface IAppDataOptions<T = any> {
dirPath?: string;
requiredKeys?: string[];
requiredKeys?: Array<keyof T>;
/**
* kvStoreKey: 'MY_ENV_VAR'
*/
envMapping?: {
[key: string]: string | object;
[key in keyof T]?: string | object;
};
}
@@ -22,17 +22,18 @@ export class AppData<T = any> {
* @param pathArg
* @returns
*/
public static async createAndInit<T = any>(optionsArg: IAppDataOptions = {}): Promise<AppData<T>> {
public static async createAndInit<T = any>(optionsArg: IAppDataOptions<T> = {}): Promise<AppData<T>> {
const appData = new AppData<T>(optionsArg);
await appData.readyDeferred.promise;
return appData;
}
// instance
public readyDeferred = plugins.smartpromise.defer();
public options: IAppDataOptions;
public readyDeferred = plugins.smartpromise.defer<void>();
public options: IAppDataOptions<T>;
private kvStore: KeyValueStore<T>;
constructor(optionsArg: IAppDataOptions = {}) {
constructor(optionsArg: IAppDataOptions<T> = {}) {
this.options = optionsArg;
this.init();
}
@@ -59,28 +60,29 @@ export class AppData<T = any> {
this.options.dirPath = nogitAppData;
}
}
this.kvStore = new KeyValueStore({
this.kvStore = new KeyValueStore<T>({
typeArg: 'custom',
identityArg: 'appkv',
customPath: this.options.dirPath,
mandatoryKeys: this.options.requiredKeys
mandatoryKeys: this.options.requiredKeys as Array<keyof T>
});
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> => {
const processEnvMapping = async (key: keyof T, mappingValue: any, parentKey: keyof T | '' = ''): Promise<any> => {
if (typeof mappingValue === 'string') {
let envValue: string;
let envValue: string | T[keyof T];
if (mappingValue.startsWith('hard:')) {
envValue = mappingValue.replace('hard:', '');
envValue = mappingValue.replace('hard:', '') as T[keyof T];
} else {
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue);
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue) as T[keyof T];
}
if (envValue) {
if (mappingValue.endsWith('_JSON')) {
envValue = JSON.parse(envValue);
if (typeof envValue === 'string' && mappingValue.endsWith('_JSON')) {
envValue = JSON.parse(envValue) as T[keyof T];
}
if (!parentKey) {
await this.kvStore.writeKey(key, envValue);
@@ -91,42 +93,40 @@ export class AppData<T = any> {
return undefined;
}
} else if (typeof mappingValue === 'object' && mappingValue !== null) {
const resultObject = {};
const resultObject: Partial<T> = {};
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;
const nestedResult = await processEnvMapping(innerKey as keyof T, nestedValue, key);
resultObject[innerKey as keyof T] = nestedResult;
}
if (parentKey === '') {
// Only write to kvStore if at the top level
await this.kvStore.writeKey(key, resultObject);
await this.kvStore.writeKey(key, resultObject as T[keyof T]);
} 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]);
await processEnvMapping(key as keyof T, this.options.envMapping[key]);
}
}
this.readyDeferred.resolve();
}
/**
* returns a kvtore that resides in appdata
* returns a kvstore that resides in appdata
*/
public async getKvStore() {
public async getKvStore(): Promise<KeyValueStore<T>> {
await this.readyDeferred.promise;
return this.kvStore;
}
public async logMissingKeys() {
public async logMissingKeys(): Promise<Array<keyof T>> {
const kvStore = await this.getKvStore();
const missingMandatoryKeys = await kvStore.getMissingMandatoryKeys();
if (missingMandatoryKeys.length > 0) {
@@ -141,9 +141,9 @@ export class AppData<T = any> {
return missingMandatoryKeys;
}
public async waitForAndGetKey(keyArg: string) {
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
await this.readyDeferred.promise;
await this.kvStore.waitForKeysPresent([keyArg]);
return this.kvStore.readKey[keyArg];
return this.kvStore.readKey(keyArg);
}
}
}