fix(core): update

This commit is contained in:
Philipp Kunz 2024-06-19 19:03:26 +02:00
parent b81ab9d9b2
commit db90714a81
2 changed files with 66 additions and 25 deletions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/npmextra', name: '@push.rocks/npmextra',
version: '5.0.22', version: '5.0.23',
description: 'A utility to enhance npm with additional configuration, tool management capabilities, and a key-value store for project setups.' description: 'A utility to enhance npm with additional configuration, tool management capabilities, and a key-value store for project setups.'
} }

View File

@ -2,7 +2,6 @@ import * as plugins from './npmextra.plugins.js';
import * as paths from './npmextra.paths.js'; import * as paths from './npmextra.paths.js';
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js'; import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
export interface IAppDataOptions<T = any> { export interface IAppDataOptions<T = any> {
dirPath?: string; dirPath?: string;
requiredKeys?: Array<keyof T>; requiredKeys?: Array<keyof T>;
@ -15,7 +14,7 @@ export interface IAppDataOptions<T = any> {
/** /**
* kvStoreKey: 'MY_ENV_VAR' * kvStoreKey: 'MY_ENV_VAR'
*/ */
envMapping?: plugins.tsclass.typeFest.PartialDeep<T> envMapping?: plugins.tsclass.typeFest.PartialDeep<T>;
} }
export class AppData<T = any> { export class AppData<T = any> {
@ -25,7 +24,9 @@ export class AppData<T = any> {
* @param pathArg * @param pathArg
* @returns * @returns
*/ */
public static async createAndInit<T = any>(optionsArg: IAppDataOptions<T> = {}): Promise<AppData<T>> { public static async createAndInit<T = any>(
optionsArg: IAppDataOptions<T> = {}
): Promise<AppData<T>> {
const appData = new AppData<T>(optionsArg); const appData = new AppData<T>(optionsArg);
await appData.readyDeferred.promise; await appData.readyDeferred.promise;
return appData; return appData;
@ -68,41 +69,81 @@ export class AppData<T = any> {
typeArg: this.options.ephermal ? 'ephemeral' : 'custom', typeArg: this.options.ephermal ? 'ephemeral' : 'custom',
identityArg: 'appkv', identityArg: 'appkv',
customPath: this.options.dirPath, customPath: this.options.dirPath,
mandatoryKeys: this.options.requiredKeys as Array<keyof T> mandatoryKeys: this.options.requiredKeys as Array<keyof T>,
}); });
if (this.options.envMapping) { if (this.options.envMapping) {
const qenvInstance = new plugins.qenv.Qenv(process.cwd(), plugins.path.join(process.cwd(), '.nogit')); const qenvInstance = new plugins.qenv.Qenv(
process.cwd(),
plugins.path.join(process.cwd(), '.nogit')
);
// Recursive function to handle nested objects, now includes key parameter // Recursive function to handle nested objects, now includes key parameter
const processEnvMapping = async (key: keyof T, mappingValue: any, parentKey: keyof T | '' = ''): Promise<any> => { const processEnvMapping = async (
key: keyof T,
mappingValue: any,
parentKey: keyof T | '' = ''
): Promise<any> => {
if (typeof mappingValue === 'string') { if (typeof mappingValue === 'string') {
let envValue: string | boolean | T[keyof T]; let envValue: string | boolean | T[keyof T];
let convert: 'none' | 'json' | 'base64' | 'boolean' = 'none' let convert: 'none' | 'json' | 'base64' | 'boolean' = 'none';
if (mappingValue.startsWith('hard:')) { switch (true) {
envValue = mappingValue.replace('hard:', '') as T[keyof T]; case mappingValue.startsWith('hard:'):
} else if (mappingValue.startsWith('boolean:')) { envValue = mappingValue.replace('hard:', '') as T[keyof T];
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue.replace('boolean:', '')) as T[keyof T]; break;
convert = 'boolean'; case mappingValue.startsWith('hard_boolean:'):
} else if(mappingValue.startsWith('json')) { envValue = mappingValue.replace('hard_boolean:', '') === 'true';
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue.replace('json:', '')) as T[keyof T]; convert = 'boolean';
convert = 'json'; break;
} else if (mappingValue.startsWith('base64')) { case mappingValue.startsWith('hard_json:'):
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue.replace('base64:', '')) as T[keyof T]; envValue = JSON.parse(mappingValue.replace('hard_json:', '')) as T[keyof T];
convert = 'base64'; convert = 'json';
} else { break;
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue) as T[keyof T]; case mappingValue.startsWith('hard_base64:'):
envValue = Buffer.from(
mappingValue.replace('hard_base64:', ''),
'base64'
).toString() as T[keyof T];
convert = 'base64';
break;
case mappingValue.startsWith('boolean:'):
envValue = (await qenvInstance.getEnvVarOnDemand(
mappingValue.replace('boolean:', '')
)) as T[keyof T];
convert = 'boolean';
break;
case mappingValue.startsWith('json:'):
envValue = (await qenvInstance.getEnvVarOnDemand(
mappingValue.replace('json:', '')
)) as T[keyof T];
convert = 'json';
break;
case mappingValue.startsWith('base64:'):
envValue = (await qenvInstance.getEnvVarOnDemand(
mappingValue.replace('base64:', '')
)) as T[keyof T];
convert = 'base64';
break;
default:
envValue = (await qenvInstance.getEnvVarOnDemand(mappingValue)) as T[keyof T];
break;
} }
// lets format the env value // lets format the env value
if (envValue) { if (envValue) {
if (convert === 'boolean') { if (typeof envValue === 'string' && convert === 'boolean') {
envValue = envValue === 'true'; envValue = envValue === 'true';
} }
if (typeof envValue === 'string' && (mappingValue.endsWith('_JSON') || convert === 'json')) { if (
typeof envValue === 'string' &&
(mappingValue.endsWith('_JSON') || convert === 'json')
) {
envValue = JSON.parse(envValue as string) as T[keyof T]; envValue = JSON.parse(envValue as string) as T[keyof T];
} }
if (typeof envValue === 'string' && (mappingValue.endsWith('_BASE64') || convert === 'base64')) { if (
typeof envValue === 'string' &&
(mappingValue.endsWith('_BASE64') || convert === 'base64')
) {
envValue = Buffer.from(envValue as string, 'base64').toString(); envValue = Buffer.from(envValue as string, 'base64').toString();
} }
if (!parentKey) { if (!parentKey) {