mirror of https://github.com/espruino/BangleApps
now write files element to app json
parent
f28c141f22
commit
00aa3b499e
39
comms.js
39
comms.js
|
@ -18,18 +18,39 @@ uploadApp : app => {
|
||||||
return new Promise((resolve,reject) => {
|
return new Promise((resolve,reject) => {
|
||||||
// Load all files
|
// Load all files
|
||||||
Promise.all(app.storage.map(storageFile => {
|
Promise.all(app.storage.map(storageFile => {
|
||||||
var promise;
|
|
||||||
if (storageFile.content)
|
if (storageFile.content)
|
||||||
promise = Promise.resolve(storageFile.content);
|
return Promise.resolve(storageFile);
|
||||||
else if (storageFile.url)
|
else if (storageFile.url)
|
||||||
promise = httpGet("apps/"+storageFile.url);
|
return httpGet("apps/"+storageFile.url).then(content => {
|
||||||
else promise = Promise.resolve();
|
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
|
// then map each file to a command to load into storage
|
||||||
return promise.then(contents =>
|
fileContents.forEach(storageFile => {
|
||||||
contents?`\x10require('Storage').write(${toJS(storageFile.name)},${storageFile.evaluate ? contents.trim() : toJS(contents)});`:"")
|
// check if this is the JSON file
|
||||||
})) // now we just have a list of commands...
|
if (storageFile.name[0]=="+") {
|
||||||
.then((fileContents) => {
|
storageFile.evaluate = true;
|
||||||
fileContents = fileContents.join("\n")+"\n";
|
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 = storageFile.evaluate ? storageFile.content.trim() : toJS(storageFile.content);
|
||||||
|
storageFile.cmd = `\x10require('Storage').write(${toJS(storageFile.name)},${js});`;
|
||||||
|
});
|
||||||
|
|
||||||
|
fileContents = fileContents.map(storageFile=>storageFile.cmd).join("\n")+"\n";
|
||||||
console.log("uploadApp",fileContents);
|
console.log("uploadApp",fileContents);
|
||||||
// reset to ensure we have enough memory to upload what we need to
|
// reset to ensure we have enough memory to upload what we need to
|
||||||
Puck.write("\x03reset();\n", (result) => {
|
Puck.write("\x03reset();\n", (result) => {
|
||||||
|
|
Loading…
Reference in New Issue