now write files element to app json

pull/7/head
Gordon Williams 2019-11-07 21:08:33 +00:00
parent f28c141f22
commit 00aa3b499e
1 changed files with 30 additions and 9 deletions

View File

@ -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) => {