fix(core): update

This commit is contained in:
Philipp Kunz 2023-08-24 10:44:42 +02:00
parent 8fa3fd8ac1
commit 072ee31c3f
3 changed files with 29 additions and 6 deletions

View File

@ -9,6 +9,10 @@ tap.test('should create a keyValueStore', async () => {
expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore); expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore);
}); });
tap.test('should reset the keyValueStore', async () => {
await myKeyValueStore.reset();
});
tap.test('expect result to be empty', async () => { tap.test('expect result to be empty', async () => {
let result = await myKeyValueStore.readAll(); let result = await myKeyValueStore.readAll();
expect(JSON.stringify(result)).toEqual('{}'); expect(JSON.stringify(result)).toEqual('{}');

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/npmextra', name: '@push.rocks/npmextra',
version: '3.0.12', version: '3.0.13',
description: 'do more with npm' description: 'do more with npm'
} }

View File

@ -25,7 +25,10 @@ export class KeyValueStore {
delete this.dataObject[key]; delete this.dataObject[key];
} }
this.deletedObject = {}; 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
);
}, },
}); });
@ -33,8 +36,9 @@ export class KeyValueStore {
* computes the identity and filePath * computes the identity and filePath
*/ */
private initFilePath = () => { private initFilePath = () => {
if (this.customPath) { // Use custom path if provided if (this.customPath) {
const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd) // Use custom path if provided
const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd);
this.filePath = absolutePath; this.filePath = absolutePath;
if (plugins.smartfile.fs.isDirectorySync(this.filePath)) { if (plugins.smartfile.fs.isDirectorySync(this.filePath)) {
this.filePath = plugins.path.join(this.filePath, this.identity + '.json'); this.filePath = plugins.path.join(this.filePath, this.identity + '.json');
@ -124,4 +128,19 @@ export class KeyValueStore {
this.dataObject = {}; this.dataObject = {};
await plugins.smartfile.fs.remove(this.filePath); 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
}
} }