fix(core): update

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

View File

@ -22,20 +22,20 @@
"homepage": "https://code.foss.global/push.rocks/npmextra", "homepage": "https://code.foss.global/push.rocks/npmextra",
"dependencies": { "dependencies": {
"@push.rocks/qenv": "^6.0.5", "@push.rocks/qenv": "^6.0.5",
"@push.rocks/smartfile": "^11.0.4", "@push.rocks/smartfile": "^11.0.20",
"@push.rocks/smartjson": "^5.0.10", "@push.rocks/smartjson": "^5.0.20",
"@push.rocks/smartlog": "^3.0.2", "@push.rocks/smartlog": "^3.0.7",
"@push.rocks/smartpath": "^5.0.11", "@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"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.1.66", "@git.zone/tsbuild": "^2.1.80",
"@git.zone/tsrun": "^1.2.44", "@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.77", "@git.zone/tstest": "^1.0.90",
"@push.rocks/tapbundle": "^5.0.15", "@push.rocks/tapbundle": "^5.0.23",
"@types/node": "^20.11.17" "@types/node": "^20.14.2"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

File diff suppressed because it is too large Load Diff

View File

@ -2,10 +2,14 @@ import { expect, tap } from '@push.rocks/tapbundle';
import * as npmextra from '../ts/index.js'; import * as npmextra from '../ts/index.js';
let myKeyValueStore: npmextra.KeyValueStore; let myKeyValueStore: npmextra.KeyValueStore<any>;
tap.test('should create a keyValueStore', async () => { tap.test('should create a keyValueStore', async () => {
myKeyValueStore = new npmextra.KeyValueStore('custom', 'test', 'test/somekv.json'); myKeyValueStore = new npmextra.KeyValueStore<any>({
typeArg: 'custom',
identityArg: 'test',
customPath: 'test/somekv.json',
});
expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore); expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore);
}); });

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/npmextra', name: '@push.rocks/npmextra',
version: '5.0.13', version: '5.0.14',
description: 'Enhances npm with additional configuration and tool management capabilities, including a key-value store for project setups.' description: 'Enhances npm with additional configuration and tool management capabilities, including a key-value store for project setups.'
} }

View File

@ -3,15 +3,15 @@ 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'; import { env } from 'process';
export interface IAppDataOptions { export interface IAppDataOptions<T = any> {
dirPath?: string; dirPath?: string;
requiredKeys?: string[]; requiredKeys?: Array<keyof T>;
/** /**
* kvStoreKey: 'MY_ENV_VAR' * kvStoreKey: 'MY_ENV_VAR'
*/ */
envMapping?: { envMapping?: {
[key: string]: string | object; [key in keyof T]?: string | object;
}; };
} }
@ -22,17 +22,18 @@ export class AppData<T = any> {
* @param pathArg * @param pathArg
* @returns * @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); const appData = new AppData<T>(optionsArg);
await appData.readyDeferred.promise; await appData.readyDeferred.promise;
return appData; return appData;
} }
// instance // instance
public readyDeferred = plugins.smartpromise.defer(); public readyDeferred = plugins.smartpromise.defer<void>();
public options: IAppDataOptions; public options: IAppDataOptions<T>;
private kvStore: KeyValueStore<T>; private kvStore: KeyValueStore<T>;
constructor(optionsArg: IAppDataOptions = {}) {
constructor(optionsArg: IAppDataOptions<T> = {}) {
this.options = optionsArg; this.options = optionsArg;
this.init(); this.init();
} }
@ -59,28 +60,29 @@ export class AppData<T = any> {
this.options.dirPath = nogitAppData; this.options.dirPath = nogitAppData;
} }
} }
this.kvStore = new KeyValueStore({
this.kvStore = new KeyValueStore<T>({
typeArg: 'custom', typeArg: 'custom',
identityArg: 'appkv', identityArg: 'appkv',
customPath: this.options.dirPath, customPath: this.options.dirPath,
mandatoryKeys: this.options.requiredKeys 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: string, mappingValue: any, parentKey: string = ''): 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; let envValue: string | T[keyof T];
if (mappingValue.startsWith('hard:')) { if (mappingValue.startsWith('hard:')) {
envValue = mappingValue.replace('hard:', ''); envValue = mappingValue.replace('hard:', '') as T[keyof T];
} else { } else {
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue); envValue = await qenvInstance.getEnvVarOnDemand(mappingValue) as T[keyof T];
} }
if (envValue) { if (envValue) {
if (mappingValue.endsWith('_JSON')) { if (typeof envValue === 'string' && mappingValue.endsWith('_JSON')) {
envValue = JSON.parse(envValue); envValue = JSON.parse(envValue) as T[keyof T];
} }
if (!parentKey) { if (!parentKey) {
await this.kvStore.writeKey(key, envValue); await this.kvStore.writeKey(key, envValue);
@ -91,16 +93,16 @@ export class AppData<T = any> {
return undefined; return undefined;
} }
} else if (typeof mappingValue === 'object' && mappingValue !== null) { } else if (typeof mappingValue === 'object' && mappingValue !== null) {
const resultObject = {}; const resultObject: Partial<T> = {};
for (const innerKey in mappingValue) { for (const innerKey in mappingValue) {
const nestedValue = mappingValue[innerKey]; const nestedValue = mappingValue[innerKey];
// For nested objects, call recursively but do not immediately write to kvStore // For nested objects, call recursively but do not immediately write to kvStore
const nestedResult = await processEnvMapping(innerKey, nestedValue, key); const nestedResult = await processEnvMapping(innerKey as keyof T, nestedValue, key);
resultObject[innerKey] = nestedResult; resultObject[innerKey as keyof T] = nestedResult;
} }
if (parentKey === '') { if (parentKey === '') {
// Only write to kvStore if at the top level // 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 { } else {
// For nested objects, return the constructed object instead of writing to kvStore // For nested objects, return the constructed object instead of writing to kvStore
return resultObject; return resultObject;
@ -109,24 +111,22 @@ export class AppData<T = any> {
}; };
for (const key in this.options.envMapping) { 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(); 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; await this.readyDeferred.promise;
return this.kvStore; return this.kvStore;
} }
public async logMissingKeys() { public async logMissingKeys(): Promise<Array<keyof T>> {
const kvStore = await this.getKvStore(); const kvStore = await this.getKvStore();
const missingMandatoryKeys = await kvStore.getMissingMandatoryKeys(); const missingMandatoryKeys = await kvStore.getMissingMandatoryKeys();
if (missingMandatoryKeys.length > 0) { if (missingMandatoryKeys.length > 0) {
@ -141,9 +141,9 @@ export class AppData<T = any> {
return missingMandatoryKeys; 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.readyDeferred.promise;
await this.kvStore.waitForKeysPresent([keyArg]); await this.kvStore.waitForKeysPresent([keyArg]);
return this.kvStore.readKey[keyArg]; return this.kvStore.readKey(keyArg);
} }
} }

View File

@ -5,11 +5,11 @@ import { Task } from '@push.rocks/taskbuffer';
export type TKeyValueStore = 'custom' | 'userHomeDir'; export type TKeyValueStore = 'custom' | 'userHomeDir';
export interface IKvStoreConstructorOptions { export interface IKvStoreConstructorOptions<T> {
typeArg: TKeyValueStore; typeArg: TKeyValueStore;
identityArg: string; identityArg: string;
customPath?: string; customPath?: string;
mandatoryKeys?: string[]; mandatoryKeys?: Array<keyof T>;
} }
/** /**
@ -17,9 +17,9 @@ export interface IKvStoreConstructorOptions {
*/ */
export class KeyValueStore<T = any> { export class KeyValueStore<T = any> {
private dataObject: Partial<T> = {}; private dataObject: Partial<T> = {};
private deletedObject: any = {}; private deletedObject: Partial<T> = {};
private mandatoryKeys: Set<string> = new Set(); private mandatoryKeys: Set<keyof T> = new Set();
public changeSubject = new plugins.smartrx.rxjs.Subject(); public changeSubject = new plugins.smartrx.rxjs.Subject<Partial<T>>();
private storedStateString: string = ''; private storedStateString: string = '';
public syncTask = new Task({ public syncTask = new Task({
@ -28,12 +28,11 @@ export class KeyValueStore<T = any> {
bufferMax: 1, bufferMax: 1,
execDelay: 0, execDelay: 0,
taskFunction: async () => { taskFunction: async () => {
this.dataObject = { this.dataObject = {
...plugins.smartfile.fs.toObjectSync(this.filePath), ...plugins.smartfile.fs.toObjectSync(this.filePath),
...this.dataObject, ...this.dataObject,
}; };
for (const key of Object.keys(this.deletedObject)) { for (const key of Object.keys(this.deletedObject) as Array<keyof T>) {
delete this.dataObject[key]; delete this.dataObject[key];
} }
this.deletedObject = {}; this.deletedObject = {};
@ -89,7 +88,7 @@ export class KeyValueStore<T = any> {
* @param identityArg * @param identityArg
* @param customPath Optional custom path for the keyValue store * @param customPath Optional custom path for the keyValue store
*/ */
constructor(optionsArg: IKvStoreConstructorOptions) { constructor(optionsArg: IKvStoreConstructorOptions<T>) {
if (optionsArg.customPath && optionsArg.typeArg !== 'custom') { if (optionsArg.customPath && optionsArg.typeArg !== 'custom') {
throw new Error('customPath can only be provided if typeArg is custom'); throw new Error('customPath can only be provided if typeArg is custom');
} }
@ -108,7 +107,7 @@ export class KeyValueStore<T = any> {
/** /**
* reads all keyValue pairs at once and returns them * reads all keyValue pairs at once and returns them
*/ */
public async readAll() { public async readAll(): Promise<Partial<T>> {
await this.syncTask.trigger(); await this.syncTask.trigger();
return this.dataObject; return this.dataObject;
} }
@ -116,21 +115,21 @@ export class KeyValueStore<T = any> {
/** /**
* reads a keyValueFile from disk * reads a keyValueFile from disk
*/ */
public async readKey(keyArg: string) { public async readKey<K extends keyof T>(keyArg: K): Promise<T[K]> {
await this.syncTask.trigger(); await this.syncTask.trigger();
return this.dataObject[keyArg]; return this.dataObject[keyArg] as T[K];
} }
/** /**
* writes a specific key to the keyValueStore * writes a specific key to the keyValueStore
*/ */
public async writeKey(keyArg: string, valueArg: any) { public async writeKey<K extends keyof T>(keyArg: K, valueArg: T[K]): Promise<void> {
await this.writeAll({ await this.writeAll({
[keyArg]: valueArg, [keyArg]: valueArg,
}); } as unknown as Partial<T>);
} }
public async deleteKey(keyArg: string) { public async deleteKey<K extends keyof T>(keyArg: K): Promise<void> {
this.deletedObject[keyArg] = this.dataObject[keyArg]; this.deletedObject[keyArg] = this.dataObject[keyArg];
await this.syncTask.trigger(); await this.syncTask.trigger();
} }
@ -138,7 +137,7 @@ export class KeyValueStore<T = any> {
/** /**
* writes all keyValue pairs in the object argument * writes all keyValue pairs in the object argument
*/ */
public async writeAll(keyValueObject: { [key: string]: any }) { public async writeAll(keyValueObject: Partial<T>): Promise<void> {
this.dataObject = { ...this.dataObject, ...keyValueObject }; this.dataObject = { ...this.dataObject, ...keyValueObject };
await this.syncTask.trigger(); await this.syncTask.trigger();
} }
@ -146,7 +145,7 @@ export class KeyValueStore<T = any> {
/** /**
* wipes a key value store from disk * wipes a key value store from disk
*/ */
public async wipe() { public async wipe(): Promise<void> {
this.dataObject = {}; this.dataObject = {};
await plugins.smartfile.fs.remove(this.filePath); await plugins.smartfile.fs.remove(this.filePath);
} }
@ -154,11 +153,11 @@ export class KeyValueStore<T = any> {
/** /**
* resets the KeyValueStore to the initial state by syncing first, deleting all keys, and then triggering a sync again * resets the KeyValueStore to the initial state by syncing first, deleting all keys, and then triggering a sync again
*/ */
public async reset() { public async reset(): Promise<void> {
await this.syncTask.trigger(); // Sync to get the latest state await this.syncTask.trigger(); // Sync to get the latest state
// Delete all keys from the dataObject and add them to deletedObject // Delete all keys from the dataObject and add them to deletedObject
for (const key of Object.keys(this.dataObject)) { for (const key of Object.keys(this.dataObject) as Array<keyof T>) {
this.deletedObject[key] = this.dataObject[key]; this.deletedObject[key] = this.dataObject[key];
delete this.dataObject[key]; delete this.dataObject[key];
} }
@ -166,21 +165,21 @@ export class KeyValueStore<T = any> {
await this.syncTask.trigger(); // Sync again to reflect the deletion await this.syncTask.trigger(); // Sync again to reflect the deletion
} }
private setMandatoryKeys(keys: string[]) { private setMandatoryKeys(keys: Array<keyof T>) {
keys.forEach(key => this.mandatoryKeys.add(key)); keys.forEach(key => this.mandatoryKeys.add(key));
} }
public async getMissingMandatoryKeys(): Promise<string[]> { public async getMissingMandatoryKeys(): Promise<Array<keyof T>> {
await this.readAll(); await this.readAll();
return Array.from(this.mandatoryKeys).filter(key => !(key in this.dataObject)); return Array.from(this.mandatoryKeys).filter(key => !(key in this.dataObject));
} }
public async waitForKeysPresent(keysArg: string[]): Promise<void> { public async waitForKeysPresent<K extends keyof T>(keysArg: K[]): Promise<void> {
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]); const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
if (missingKeys.length === 0) { if (missingKeys.length === 0) {
return; return;
} }
return new Promise((resolve, reject) => { return new Promise<void>((resolve, reject) => {
const subscription = this.changeSubject.subscribe(() => { const subscription = this.changeSubject.subscribe(() => {
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]); const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
if (missingKeys.length === 0) { if (missingKeys.length === 0) {
@ -190,4 +189,9 @@ export class KeyValueStore<T = any> {
}); });
}); });
} }
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
await this.waitForKeysPresent([keyArg]);
return this.readKey(keyArg);
}
} }