forked from espruino/BangleApps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appinfo.js
54 lines (52 loc) · 1.86 KB
/
appinfo.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
function toJS(txt) {
return JSON.stringify(txt);
}
var AppInfo = {
getFiles : (app,fileGetter) => {
return new Promise((resolve,reject) => {
// Load all files
Promise.all(app.storage.map(storageFile => {
if (storageFile.content)
return Promise.resolve(storageFile);
else if (storageFile.url)
return fileGetter(`apps/${app.id}/${storageFile.url}`).then(content => {
return {
name : storageFile.name,
content : content,
evaluate : storageFile.evaluate
}});
else return Promise.resolve();
})).then(fileContents => { // now we just have a list of files + contents...
// filter out empty files
fileContents = fileContents.filter(x=>x!==undefined);
// then map each file to a command to load into storage
fileContents.forEach(storageFile => {
// check if this is the JSON file
if (storageFile.name[0]=="+") {
storageFile.evaluate = true;
var json = {};
try {
json = JSON.parse(storageFile.content);
} catch (e) {
reject(storageFile.name+" is not valid JSON");
}
json.files = fileContents.map(storageFile=>storageFile.name).join(",");
storageFile.content = JSON.stringify(json);
}
// format ready for Espruino
var js;
if (storageFile.evaluate) {
js = storageFile.content.trim();
if (js.endsWith(";"))
js = js.slice(0,-1);
} else
js = toJS(storageFile.content);
storageFile.cmd = `\x10require('Storage').write(${toJS(storageFile.name)},${js});`;
});
resolve(fileContents);
}).catch(err => reject(err));
});
},
};
if ("undefined"!=typeof module)
module.exports = AppInfo;