BREAKING CHANGE(scope): change to @pushrocks scope
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
import * as plugins from './npmextra.plugins'
|
||||
import * as paths from './npmextra.paths'
|
||||
import * as plugins from './npmextra.plugins';
|
||||
import * as paths from './npmextra.paths';
|
||||
|
||||
import { Task, TaskOnce } from 'taskbuffer'
|
||||
import { Task, TaskOnce } from '@pushrocks/taskbuffer';
|
||||
|
||||
export type TKeyValueStore = 'path' | 'gitProject' | 'custom'
|
||||
export type TKeyValueStore = 'path' | 'gitProject' | 'custom';
|
||||
|
||||
/**
|
||||
* kvStore is a simple key vlaue store to store data about projects between runs
|
||||
*/
|
||||
export class KeyValueStore {
|
||||
dataObject: any
|
||||
deletedObject: any = {}
|
||||
dataObject: any;
|
||||
deletedObject: any = {};
|
||||
initialReadTask = new TaskOnce({
|
||||
taskFunction: async () => {
|
||||
this.dataObject = plugins.smartfile.fs.toObjectSync(this.filePath)
|
||||
this.dataObject = plugins.smartfile.fs.toObjectSync(this.filePath);
|
||||
}
|
||||
})
|
||||
});
|
||||
syncTask = new Task({
|
||||
buffered: true,
|
||||
bufferMax: 2,
|
||||
@@ -25,21 +25,18 @@ export class KeyValueStore {
|
||||
{},
|
||||
plugins.smartfile.fs.toObjectSync(this.filePath),
|
||||
this.dataObject
|
||||
)
|
||||
);
|
||||
for (let key in this.deletedObject) {
|
||||
delete this.dataObject[key]
|
||||
delete this.dataObject[key];
|
||||
}
|
||||
this.deletedObject = {}
|
||||
await plugins.smartfile.memory.toFs(
|
||||
JSON.stringify(this.dataObject),
|
||||
this.filePath
|
||||
)
|
||||
this.deletedObject = {};
|
||||
await plugins.smartfile.memory.toFs(JSON.stringify(this.dataObject), this.filePath);
|
||||
},
|
||||
name: 'syncTask'
|
||||
})
|
||||
type: TKeyValueStore // the type of the kvStore
|
||||
identity: string // the identity of the kvStore
|
||||
filePath: string // the filePath of the kvStore
|
||||
});
|
||||
type: TKeyValueStore; // the type of the kvStore
|
||||
identity: string; // the identity of the kvStore
|
||||
filePath: string; // the filePath of the kvStore
|
||||
|
||||
/**
|
||||
* the constructor of keyvalue store
|
||||
@@ -48,79 +45,76 @@ export class KeyValueStore {
|
||||
*/
|
||||
constructor(typeArg: TKeyValueStore, customStringArg: string) {
|
||||
// set kvStoreType
|
||||
this.type = typeArg
|
||||
this.identity = customStringArg
|
||||
this.initFilePath()
|
||||
this.type = typeArg;
|
||||
this.identity = customStringArg;
|
||||
this.initFilePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* reads all keyValue pairs at once and returns them
|
||||
*/
|
||||
async readAll () {
|
||||
await this.initialReadTask.trigger()
|
||||
this.syncTask.trigger()
|
||||
return this.dataObject
|
||||
async readAll() {
|
||||
await this.initialReadTask.trigger();
|
||||
this.syncTask.trigger();
|
||||
return this.dataObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* reads a keyValueFile from disk
|
||||
*/
|
||||
async readKey (keyArg: string) {
|
||||
let data = await this.readAll()
|
||||
return data[keyArg]
|
||||
async readKey(keyArg: string) {
|
||||
let data = await this.readAll();
|
||||
return data[keyArg];
|
||||
}
|
||||
|
||||
/**
|
||||
* writes a specific key to the keyValueStore
|
||||
*/
|
||||
async writeKey (keyArg: string, valueArg: any) {
|
||||
let writeObject: any = {}
|
||||
writeObject[keyArg] = valueArg
|
||||
this.writeAll(writeObject)
|
||||
async writeKey(keyArg: string, valueArg: any) {
|
||||
let writeObject: any = {};
|
||||
writeObject[keyArg] = valueArg;
|
||||
this.writeAll(writeObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* writes all keyValue pairs in the object argument
|
||||
*/
|
||||
async writeAll (keyValueObject) {
|
||||
plugins.smartlodash.merge(this.dataObject, keyValueObject)
|
||||
this.syncTask.trigger()
|
||||
async writeAll(keyValueObject) {
|
||||
plugins.smartlodash.merge(this.dataObject, keyValueObject);
|
||||
this.syncTask.trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* wipes a key value store from disk
|
||||
*/
|
||||
async wipe () {
|
||||
async wipe() {
|
||||
for (let key in this.dataObject) {
|
||||
this.deletedObject[key] = this.dataObject[key]
|
||||
this.deletedObject[key] = this.dataObject[key];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* updates a value
|
||||
*/
|
||||
async update (keyObject) {
|
||||
|
||||
}
|
||||
async update(keyObject) {}
|
||||
|
||||
/**
|
||||
* computes the identity
|
||||
*/
|
||||
private initFilePath () {
|
||||
private initFilePath() {
|
||||
// determine the right base directory
|
||||
let baseDir: string
|
||||
let baseDir: string;
|
||||
if (this.type === 'custom') {
|
||||
baseDir = paths.kvCustomDir
|
||||
baseDir = paths.kvCustomDir;
|
||||
} else if (this.type === 'gitProject') {
|
||||
baseDir = paths.kvGitDir
|
||||
baseDir = paths.kvGitDir;
|
||||
} else if (this.type === 'path') {
|
||||
|
||||
baseDir = paths.kvPathDir
|
||||
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, '{}')
|
||||
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, '{}');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user