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);
});
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('{}');

View File

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

View File

@ -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
}
}