Compare commits

...

10 Commits

Author SHA1 Message Date
3a53938e8e 5.0.23 2024-06-19 19:03:26 +02:00
db90714a81 fix(core): update 2024-06-19 19:03:26 +02:00
b81ab9d9b2 5.0.22 2024-06-19 18:50:01 +02:00
5ec9124d29 fix(core): update 2024-06-19 18:50:00 +02:00
f7f035e878 5.0.21 2024-06-19 17:53:28 +02:00
3caf300544 fix(core): update 2024-06-19 17:53:27 +02:00
6a70af9b6f 5.0.20 2024-06-19 17:53:11 +02:00
2ad3da85a9 fix(core): update 2024-06-19 17:53:10 +02:00
09e8b8b94c 5.0.19 2024-06-19 17:43:56 +02:00
265f4df8b3 fix(core): update 2024-06-19 17:43:55 +02:00
6 changed files with 122 additions and 28 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/npmextra", "name": "@push.rocks/npmextra",
"version": "5.0.18", "version": "5.0.23",
"private": false, "private": false,
"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.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -28,7 +28,8 @@
"@push.rocks/smartpath": "^5.0.18", "@push.rocks/smartpath": "^5.0.18",
"@push.rocks/smartpromise": "^4.0.2", "@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smartrx": "^3.0.7", "@push.rocks/smartrx": "^3.0.7",
"@push.rocks/taskbuffer": "^3.1.7" "@push.rocks/taskbuffer": "^3.1.7",
"@tsclass/tsclass": "^4.0.59"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.1.80", "@git.zone/tsbuild": "^2.1.80",

19
pnpm-lock.yaml generated
View File

@ -32,6 +32,9 @@ importers:
'@push.rocks/taskbuffer': '@push.rocks/taskbuffer':
specifier: ^3.1.7 specifier: ^3.1.7
version: 3.1.7 version: 3.1.7
'@tsclass/tsclass':
specifier: ^4.0.59
version: 4.0.59
devDependencies: devDependencies:
'@git.zone/tsbuild': '@git.zone/tsbuild':
specifier: ^2.1.80 specifier: ^2.1.80
@ -625,8 +628,8 @@ packages:
'@tsclass/tsclass@3.0.48': '@tsclass/tsclass@3.0.48':
resolution: {integrity: sha512-hC65UvDlp9qvsl6OcIZXz0JNiWZ0gyzsTzbXpg215sGxopgbkOLCr6E0s4qCTnweYm95gt2AdY95uP7M7kExaQ==} resolution: {integrity: sha512-hC65UvDlp9qvsl6OcIZXz0JNiWZ0gyzsTzbXpg215sGxopgbkOLCr6E0s4qCTnweYm95gt2AdY95uP7M7kExaQ==}
'@tsclass/tsclass@4.0.55': '@tsclass/tsclass@4.0.59':
resolution: {integrity: sha512-zg774JF90/3/rJ7xk4LyGgxcUzxdKIQcwtBVxez4LhvegESxvHiFmX42WL105iBpE53ISJ8sctLWlwG1JQZdlA==} resolution: {integrity: sha512-zSdNX/qzuekfCFG81k4e1X2tj1S+rbs2tM4CFAzT+aLiu2/AqdS1iPYwH/sYpY8vm+vBfllks9rlsuRSA2xTeg==}
'@tsconfig/node10@1.0.11': '@tsconfig/node10@1.0.11':
resolution: {integrity: sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==} resolution: {integrity: sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==}
@ -3060,7 +3063,7 @@ snapshots:
'@push.rocks/taskbuffer': 3.1.7 '@push.rocks/taskbuffer': 3.1.7
'@push.rocks/webrequest': 3.0.37 '@push.rocks/webrequest': 3.0.37
'@push.rocks/webstore': 2.0.20 '@push.rocks/webstore': 2.0.20
'@tsclass/tsclass': 4.0.55 '@tsclass/tsclass': 4.0.59
'@types/express': 4.17.21 '@types/express': 4.17.21
body-parser: 1.20.2 body-parser: 1.20.2
cors: 2.8.5 cors: 2.8.5
@ -3565,7 +3568,7 @@ snapshots:
'@push.rocks/smartlog-interfaces@3.0.2': '@push.rocks/smartlog-interfaces@3.0.2':
dependencies: dependencies:
'@api.global/typedrequest-interfaces': 2.0.2 '@api.global/typedrequest-interfaces': 2.0.2
'@tsclass/tsclass': 4.0.55 '@tsclass/tsclass': 4.0.59
'@push.rocks/smartlog@3.0.7': '@push.rocks/smartlog@3.0.7':
dependencies: dependencies:
@ -3645,7 +3648,7 @@ snapshots:
'@push.rocks/smartpromise': 4.0.3 '@push.rocks/smartpromise': 4.0.3
'@push.rocks/smartpuppeteer': 2.0.2 '@push.rocks/smartpuppeteer': 2.0.2
'@push.rocks/smartunique': 3.0.9 '@push.rocks/smartunique': 3.0.9
'@tsclass/tsclass': 4.0.55 '@tsclass/tsclass': 4.0.59
'@types/express': 4.17.21 '@types/express': 4.17.21
express: 4.19.2 express: 4.19.2
pdf-lib: 1.17.1 pdf-lib: 1.17.1
@ -3703,7 +3706,7 @@ snapshots:
'@push.rocks/smartxml': 1.0.8 '@push.rocks/smartxml': 1.0.8
'@push.rocks/smartyaml': 2.0.5 '@push.rocks/smartyaml': 2.0.5
'@push.rocks/webrequest': 3.0.37 '@push.rocks/webrequest': 3.0.37
'@tsclass/tsclass': 4.0.55 '@tsclass/tsclass': 4.0.59
'@push.rocks/smartsocket@2.0.27': '@push.rocks/smartsocket@2.0.27':
dependencies: dependencies:
@ -3837,7 +3840,7 @@ snapshots:
dependencies: dependencies:
'@pushrocks/smartdelay': 3.0.1 '@pushrocks/smartdelay': 3.0.1
'@pushrocks/smartpromise': 4.0.2 '@pushrocks/smartpromise': 4.0.2
'@tsclass/tsclass': 4.0.55 '@tsclass/tsclass': 4.0.59
'@push.rocks/webstore@2.0.20': '@push.rocks/webstore@2.0.20':
dependencies: dependencies:
@ -4013,7 +4016,7 @@ snapshots:
dependencies: dependencies:
type-fest: 2.19.0 type-fest: 2.19.0
'@tsclass/tsclass@4.0.55': '@tsclass/tsclass@4.0.59':
dependencies: dependencies:
type-fest: 4.20.0 type-fest: 4.20.0

27
test/test.appdata.ts Normal file
View File

@ -0,0 +1,27 @@
import { expect, tap } from '@push.rocks/tapbundle';
import path = require('path');
// module to test
import * as npmextra from '../ts/index.js';
interface ITestOptions {
hi: string;
deep: {
deep1: string;
deep2: string;
};
}
let testAppdata: npmextra.AppData<ITestOptions>;
tap.test('should create a valid AppData', async () => {
testAppdata = new npmextra.AppData<ITestOptions>({
envMapping: {
deep: {
deep1: '',
},
},
});
});
export default tap.start();

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/npmextra', name: '@push.rocks/npmextra',
version: '5.0.18', 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

@ -1,7 +1,6 @@
import * as plugins from './npmextra.plugins.js'; 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';
import { env } from 'process';
export interface IAppDataOptions<T = any> { export interface IAppDataOptions<T = any> {
dirPath?: string; dirPath?: string;
@ -15,9 +14,7 @@ export interface IAppDataOptions<T = any> {
/** /**
* kvStoreKey: 'MY_ENV_VAR' * kvStoreKey: 'MY_ENV_VAR'
*/ */
envMapping?: { envMapping?: plugins.tsclass.typeFest.PartialDeep<T>;
[key in keyof T]?: string | object;
};
} }
export class AppData<T = any> { export class AppData<T = any> {
@ -27,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;
@ -70,27 +69,85 @@ 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 | T[keyof T]; let envValue: string | boolean | T[keyof T];
if (mappingValue.startsWith('hard:')) { let convert: 'none' | 'json' | 'base64' | 'boolean' = 'none';
envValue = mappingValue.replace('hard:', '') as T[keyof T]; switch (true) {
} else { case mappingValue.startsWith('hard:'):
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue) as T[keyof T]; envValue = mappingValue.replace('hard:', '') as T[keyof T];
break;
case mappingValue.startsWith('hard_boolean:'):
envValue = mappingValue.replace('hard_boolean:', '') === 'true';
convert = 'boolean';
break;
case mappingValue.startsWith('hard_json:'):
envValue = JSON.parse(mappingValue.replace('hard_json:', '')) as T[keyof T];
convert = 'json';
break;
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
if (envValue) { if (envValue) {
if (typeof envValue === 'string' && mappingValue.endsWith('_JSON')) { if (typeof envValue === 'string' && convert === 'boolean') {
envValue = JSON.parse(envValue) as T[keyof T]; envValue = envValue === 'true';
}
if (
typeof envValue === 'string' &&
(mappingValue.endsWith('_JSON') || convert === 'json')
) {
envValue = JSON.parse(envValue as string) as T[keyof T];
}
if (
typeof envValue === 'string' &&
(mappingValue.endsWith('_BASE64') || convert === 'base64')
) {
envValue = Buffer.from(envValue as string, 'base64').toString();
} }
if (!parentKey) { if (!parentKey) {
await this.kvStore.writeKey(key, envValue); await this.kvStore.writeKey(key, envValue as any);
} else { } else {
return envValue; return envValue;
} }

View File

@ -1,3 +1,9 @@
import * as tsclass from '@tsclass/tsclass';
export {
tsclass
}
import * as qenv from '@push.rocks/qenv'; import * as qenv from '@push.rocks/qenv';
import * as smartlog from '@push.rocks/smartlog'; import * as smartlog from '@push.rocks/smartlog';
import * as path from 'path'; import * as path from 'path';