2018-08-30 23:11:09 +00:00
|
|
|
import * as plugins from './npmextra.plugins';
|
|
|
|
import * as paths from './npmextra.paths';
|
2016-08-28 12:51:04 +00:00
|
|
|
|
2019-09-16 11:19:42 +00:00
|
|
|
import { Task } from '@pushrocks/taskbuffer';
|
2017-07-12 13:30:49 +00:00
|
|
|
|
2018-08-30 23:11:09 +00:00
|
|
|
export type TKeyValueStore = 'path' | 'gitProject' | 'custom';
|
2016-09-24 14:44:48 +00:00
|
|
|
|
2017-07-09 17:05:03 +00:00
|
|
|
/**
|
|
|
|
* kvStore is a simple key vlaue store to store data about projects between runs
|
|
|
|
*/
|
2016-09-24 14:44:48 +00:00
|
|
|
export class KeyValueStore {
|
2019-09-16 11:18:45 +00:00
|
|
|
private dataObject: any = {};
|
|
|
|
private deletedObject: any = {};
|
2019-05-10 15:05:04 +00:00
|
|
|
public syncTask = new Task({
|
2019-09-16 11:18:45 +00:00
|
|
|
name: 'syncTask',
|
2017-07-12 15:13:29 +00:00
|
|
|
buffered: true,
|
2021-01-27 21:00:49 +00:00
|
|
|
bufferMax: 1,
|
|
|
|
execDelay: 0,
|
2017-07-12 15:13:29 +00:00
|
|
|
taskFunction: async () => {
|
2019-05-10 15:03:07 +00:00
|
|
|
this.dataObject = {
|
|
|
|
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
2021-01-27 21:00:49 +00:00
|
|
|
...this.dataObject,
|
2019-05-10 15:03:07 +00:00
|
|
|
};
|
|
|
|
for (const key of Object.keys(this.deletedObject)) {
|
2018-08-30 23:11:09 +00:00
|
|
|
delete this.dataObject[key];
|
2017-07-12 15:22:22 +00:00
|
|
|
}
|
2018-08-30 23:11:09 +00:00
|
|
|
this.deletedObject = {};
|
|
|
|
await plugins.smartfile.memory.toFs(JSON.stringify(this.dataObject), this.filePath);
|
2021-01-27 21:00:49 +00:00
|
|
|
},
|
2018-08-30 23:11:09 +00:00
|
|
|
});
|
2019-09-16 11:18:45 +00:00
|
|
|
/**
|
|
|
|
* computes the identity
|
|
|
|
*/
|
|
|
|
private initFilePath = () => {
|
|
|
|
// determine the right base directory
|
|
|
|
let baseDir: string;
|
|
|
|
if (this.type === 'custom') {
|
|
|
|
baseDir = paths.kvCustomDir;
|
|
|
|
} else if (this.type === 'gitProject') {
|
|
|
|
baseDir = paths.kvGitDir;
|
|
|
|
} else if (this.type === 'path') {
|
|
|
|
baseDir = paths.kvPathDir;
|
|
|
|
}
|
|
|
|
this.filePath = plugins.path.join(baseDir, this.identity + '.json');
|
|
|
|
plugins.smartfile.fs.ensureDirSync(paths.kvCustomDir);
|
|
|
|
plugins.smartfile.fs.ensureDirSync(paths.kvGitDir);
|
|
|
|
plugins.smartfile.fs.ensureDirSync(paths.kvPathDir);
|
|
|
|
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
|
2019-09-16 11:19:42 +00:00
|
|
|
};
|
2019-09-16 11:18:45 +00:00
|
|
|
|
2019-05-10 15:05:04 +00:00
|
|
|
public type: TKeyValueStore; // the type of the kvStore
|
|
|
|
public identity: string; // the identity of the kvStore
|
|
|
|
public filePath: string; // the filePath of the kvStore
|
2017-07-12 15:13:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the constructor of keyvalue store
|
|
|
|
* @param typeArg
|
|
|
|
* @param customStringArg
|
|
|
|
*/
|
|
|
|
constructor(typeArg: TKeyValueStore, customStringArg: string) {
|
2017-03-18 15:23:47 +00:00
|
|
|
// set kvStoreType
|
2018-08-30 23:11:09 +00:00
|
|
|
this.type = typeArg;
|
|
|
|
this.identity = customStringArg;
|
|
|
|
this.initFilePath();
|
2017-03-18 15:23:47 +00:00
|
|
|
}
|
2016-09-24 19:49:53 +00:00
|
|
|
|
2017-03-18 15:23:47 +00:00
|
|
|
/**
|
2017-07-12 13:30:49 +00:00
|
|
|
* reads all keyValue pairs at once and returns them
|
2017-03-18 15:23:47 +00:00
|
|
|
*/
|
2019-05-10 15:05:04 +00:00
|
|
|
public async readAll() {
|
2019-09-16 11:18:45 +00:00
|
|
|
await this.syncTask.trigger();
|
2018-08-30 23:11:09 +00:00
|
|
|
return this.dataObject;
|
2017-07-12 13:30:49 +00:00
|
|
|
}
|
2016-08-28 12:51:04 +00:00
|
|
|
|
2017-07-12 13:30:49 +00:00
|
|
|
/**
|
|
|
|
* reads a keyValueFile from disk
|
|
|
|
*/
|
2019-05-10 15:05:04 +00:00
|
|
|
public async readKey(keyArg: string) {
|
2019-09-16 11:18:45 +00:00
|
|
|
await this.syncTask.trigger();
|
|
|
|
return this.dataObject[keyArg];
|
2017-03-18 15:23:47 +00:00
|
|
|
}
|
2016-08-28 12:51:04 +00:00
|
|
|
|
2017-03-18 15:23:47 +00:00
|
|
|
/**
|
2017-08-16 16:25:45 +00:00
|
|
|
* writes a specific key to the keyValueStore
|
2017-03-18 15:23:47 +00:00
|
|
|
*/
|
2019-09-16 11:18:45 +00:00
|
|
|
public async writeKey(keyArg: string, valueArg: any) {
|
|
|
|
await this.writeAll({
|
2021-01-27 21:00:49 +00:00
|
|
|
[keyArg]: valueArg,
|
2019-09-16 11:18:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async deleteKey(keyArg: string) {
|
|
|
|
this.deletedObject[keyArg] = this.dataObject[keyArg];
|
|
|
|
await this.syncTask.trigger();
|
2017-08-16 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* writes all keyValue pairs in the object argument
|
|
|
|
*/
|
2019-05-10 15:03:07 +00:00
|
|
|
public async writeAll(keyValueObject) {
|
2019-09-16 11:19:42 +00:00
|
|
|
this.dataObject = { ...this.dataObject, ...keyValueObject };
|
2019-09-16 11:18:45 +00:00
|
|
|
await this.syncTask.trigger();
|
2017-03-18 15:23:47 +00:00
|
|
|
}
|
2016-08-28 12:51:04 +00:00
|
|
|
|
2017-03-18 15:23:47 +00:00
|
|
|
/**
|
|
|
|
* wipes a key value store from disk
|
|
|
|
*/
|
2019-05-10 15:03:07 +00:00
|
|
|
public async wipe() {
|
2019-09-16 11:18:45 +00:00
|
|
|
this.dataObject = {};
|
|
|
|
await plugins.smartfile.fs.remove(this.filePath);
|
2017-03-18 15:23:47 +00:00
|
|
|
}
|
2016-09-16 20:28:38 +00:00
|
|
|
}
|