From 072ee31c3f846aa546b92184443f2e03723054ed Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 24 Aug 2023 10:44:42 +0200 Subject: [PATCH] fix(core): update --- test/test.kvstore.ts | 4 ++++ ts/00_commitinfo_data.ts | 2 +- ts/npmextra.classes.keyvaluestore.ts | 29 +++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/test/test.kvstore.ts b/test/test.kvstore.ts index feec52a..7964c26 100644 --- a/test/test.kvstore.ts +++ b/test/test.kvstore.ts @@ -9,6 +9,10 @@ tap.test('should create a keyValueStore', async () => { expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore); }); +tap.test('should reset the keyValueStore', async () => { + await myKeyValueStore.reset(); +}); + tap.test('expect result to be empty', async () => { let result = await myKeyValueStore.readAll(); expect(JSON.stringify(result)).toEqual('{}'); diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index fcc5413..44e8ffb 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/npmextra', - version: '3.0.12', + version: '3.0.13', description: 'do more with npm' } diff --git a/ts/npmextra.classes.keyvaluestore.ts b/ts/npmextra.classes.keyvaluestore.ts index 5535dba..65f4ba1 100644 --- a/ts/npmextra.classes.keyvaluestore.ts +++ b/ts/npmextra.classes.keyvaluestore.ts @@ -25,16 +25,20 @@ export class KeyValueStore { delete this.dataObject[key]; } this.deletedObject = {}; - await plugins.smartfile.memory.toFs(plugins.smartjson.stringify(this.dataObject), this.filePath); + await plugins.smartfile.memory.toFs( + plugins.smartjson.stringify(this.dataObject), + this.filePath + ); }, }); - + /** * computes the identity and filePath */ private initFilePath = () => { - if (this.customPath) { // Use custom path if provided - const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd) + if (this.customPath) { + // Use custom path if provided + const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd); this.filePath = absolutePath; if (plugins.smartfile.fs.isDirectorySync(this.filePath)) { this.filePath = plugins.path.join(this.filePath, this.identity + '.json'); @@ -42,7 +46,7 @@ export class KeyValueStore { plugins.smartfile.fs.ensureFileSync(this.filePath, '{}'); return; } - + let baseDir: string; if (this.type === 'userHomeDir') { baseDir = paths.kvUserHomeDirBase; @@ -124,4 +128,19 @@ export class KeyValueStore { this.dataObject = {}; await plugins.smartfile.fs.remove(this.filePath); } + + /** + * resets the KeyValueStore to the initial state by syncing first, deleting all keys, and then triggering a sync again + */ + public async reset() { + await this.syncTask.trigger(); // Sync to get the latest state + + // Delete all keys from the dataObject and add them to deletedObject + for (const key of Object.keys(this.dataObject)) { + this.deletedObject[key] = this.dataObject[key]; + delete this.dataObject[key]; + } + + await this.syncTask.trigger(); // Sync again to reflect the deletion + } }