fix(core): update

This commit is contained in:
2024-06-19 15:07:49 +02:00
parent 6f19c3cc71
commit 937252f99e
5 changed files with 65 additions and 52 deletions

View File

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

View File

@ -7,6 +7,11 @@ export interface IAppDataOptions<T = any> {
dirPath?: string;
requiredKeys?: Array<keyof T>;
/**
* wether keys should be persisted on disk or not
*/
ephermal?: boolean;
/**
* kvStoreKey: 'MY_ENV_VAR'
*/
@ -43,7 +48,7 @@ export class AppData<T = any> {
* @param pathArg
*/
private async init(pathArg?: string) {
if (this.options.dirPath) {
if (this.options.dirPath || this.options.ephermal) {
// ok, nothing to do here;
} else {
const appDataDir = '/app/data';
@ -62,7 +67,7 @@ export class AppData<T = any> {
}
this.kvStore = new KeyValueStore<T>({
typeArg: 'custom',
typeArg: this.options.ephermal ? 'ephemeral' : 'custom',
identityArg: 'appkv',
customPath: this.options.dirPath,
mandatoryKeys: this.options.requiredKeys as Array<keyof T>

View File

@ -3,7 +3,7 @@ import * as paths from './npmextra.paths.js';
import { Task } from '@push.rocks/taskbuffer';
export type TKeyValueStore = 'custom' | 'userHomeDir';
export type TKeyValueStore = 'custom' | 'userHomeDir' | 'ephemeral';
export interface IKvStoreConstructorOptions<T> {
typeArg: TKeyValueStore;
@ -20,7 +20,7 @@ export class KeyValueStore<T = any> {
private deletedObject: Partial<T> = {};
private mandatoryKeys: Set<keyof T> = new Set();
public changeSubject = new plugins.smartrx.rxjs.Subject<Partial<T>>();
private storedStateString: string = '';
public syncTask = new Task({
name: 'syncTask',
@ -28,18 +28,20 @@ export class KeyValueStore<T = any> {
bufferMax: 1,
execDelay: 0,
taskFunction: async () => {
this.dataObject = {
...plugins.smartfile.fs.toObjectSync(this.filePath),
...this.dataObject,
};
for (const key of Object.keys(this.deletedObject) as Array<keyof T>) {
delete this.dataObject[key];
if (this.type !== 'ephemeral') {
this.dataObject = {
...plugins.smartfile.fs.toObjectSync(this.filePath),
...this.dataObject,
};
for (const key of Object.keys(this.deletedObject) as Array<keyof T>) {
delete this.dataObject[key];
}
this.deletedObject = {};
await plugins.smartfile.memory.toFs(
plugins.smartjson.stringifyPretty(this.dataObject),
this.filePath
);
}
this.deletedObject = {};
await plugins.smartfile.memory.toFs(
plugins.smartjson.stringifyPretty(this.dataObject),
this.filePath
);
const newStateString = plugins.smartjson.stringify(this.dataObject);
// change detection
@ -54,6 +56,10 @@ export class KeyValueStore<T = any> {
* computes the identity and filePath
*/
private initFilePath = () => {
if (this.type === 'ephemeral') {
// No file path is needed for ephemeral type
return;
}
if (this.customPath) {
// Use custom path if provided
const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd);
@ -79,7 +85,7 @@ export class KeyValueStore<T = any> {
// if no custom path is provided, try to store at home directory
public type: TKeyValueStore;
public identity: string;
public filePath: string;
public filePath?: string;
private customPath?: string; // Optionally allow custom path
/**
@ -147,7 +153,9 @@ export class KeyValueStore<T = any> {
*/
public async wipe(): Promise<void> {
this.dataObject = {};
await plugins.smartfile.fs.remove(this.filePath);
if (this.type !== 'ephemeral') {
await plugins.smartfile.fs.remove(this.filePath);
}
}
/**