-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathdataManager.js
50 lines (45 loc) · 1.24 KB
/
dataManager.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
const fs = require("uxp").storage.localFileSystem;
let customDataPath = "customData.json";
let initJson = `{
"sectionName": "CUSTOM",
"values": []
}`;
//Adapted from xd-storage-helper
async function getCustomDataFile() {
const dataFolder = await fs.getDataFolder();
try {
let returnFile = await dataFolder.getEntry(customDataPath);
console.log("data folder is at: " + fs.getNativePath(returnFile));
return returnFile;
} catch (e) {
const file = await dataFolder.createFile(customDataPath);
if(file.isFile) {
await file.write(initJson);
return file;
}
else {
throw new Error('Storage file customData.json was not a file.');
}
}
};
async function getJsonFromFile(file) {
console.log("getJsonFromFile()");
try {
let jsonData = JSON.parse((await file.read()).toString());
return jsonData;
} catch (e){
throw new Error('Could not get JSON from file');
}
}
async function getFileContents(path){
console.log(`getFileContents(${path})`);
const pluginFolder = await fs.getPluginFolder();
const file = await pluginFolder.getEntry(path);
const fileContent = await file.read();
return fileContent;
};
module.exports = {
getCustomDataFile,
getJsonFromFile,
getFileContents
}