-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathStorage.js
94 lines (86 loc) · 3.31 KB
/
Storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright (c) 2018. by Pablo Klaschka
*/
const storage = require('uxp').storage;
const fs = storage.localFileSystem;
class storageHelper {
/**
* Creates a data file if none was previously existent.
* @return {Promise<storage.File>} The data file
* @private
*/
static async init() {
let dataFolder = await fs.getDataFolder();
try {
const file = await dataFolder.getEntry('data.json');
if (file) { // noinspection JSValidateTypes
return file;
}
else {
throw new Error('Text Area Toolbox file data.json was not a file.');
}
} catch {
console.log('Creating file');
const file = await dataFolder.createEntry('data.json', {type: storage.types.file, overwrite: true});
if (file.isFile) {
await file.write('{}', {append: false});
// noinspection JSValidateTypes
return file;
} else {
throw new Error('Text Area Toolbox file data.json was not a file.');
}
}
}
/**
* Retrieves all the values from storage. Saves default value if none is set.
* @param {*} defaultValue The default value. Gets saved and returned if no value was previously set for the speciefied key.
* @return {Promise<*>} The value retrieved from storage. If none is saved, the `defaultValue` is returned.
*/
static async all(key, defaultValue = {}) {
const dataFile = await this.init();
let object = JSON.parse((await dataFile.read({format: storage.formats.utf8})).toString());
if (object === undefined) {
return defaultValue;
} else {
return object;
}
}
/**
* Saves/Replaces all the values to the storage.
* @param {*} object
* @return {Promise<void>}
*/
static async saveAll(object) {
const dataFile = await this.init();
return await dataFile.write(JSON.stringify(object), {append: false, format: storage.formats.utf8})
}
/**
* Retrieves a value from storage. Saves default value if none is set.
* @param {string} key The identifier
* @param {*} defaultValue The default value. Gets saved and returned if no value was previously set for the speciefied key.
* @return {Promise<*>} The value retrieved from storage. If none is saved, the `defaultValue` is returned.
*/
static async get(key, defaultValue) {
const dataFile = await this.init();
let object = JSON.parse((await dataFile.read({format: storage.formats.utf8})).toString());
if (object[key] === undefined) {
await this.set(key, defaultValue);
return defaultValue;
} else {
return object[key];
}
}
/**
* Saves a certain key-value-pair to the storage.
* @param {string} key The identifier
* @param {*} value
* @return {Promise<void>}
*/
static async set(key, value) {
const dataFile = await this.init();
let object = JSON.parse((await dataFile.read({format: storage.formats.utf8})).toString());
object[key] = value;
return await dataFile.write(JSON.stringify(object), {append: false, format: storage.formats.utf8})
}
}
module.exports = storageHelper;