diff --git a/comms.js b/comms.js index 78841bc6e..9442187a5 100644 --- a/comms.js +++ b/comms.js @@ -18,18 +18,39 @@ uploadApp : app => { return new Promise((resolve,reject) => { // Load all files Promise.all(app.storage.map(storageFile => { - var promise; if (storageFile.content) - promise = Promise.resolve(storageFile.content); + return Promise.resolve(storageFile); else if (storageFile.url) - promise = httpGet("apps/"+storageFile.url); - else promise = Promise.resolve(); + return httpGet("apps/"+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 - return promise.then(contents => - contents?`\x10require('Storage').write(${toJS(storageFile.name)},${storageFile.evaluate ? contents.trim() : toJS(contents)});`:"") - })) // now we just have a list of commands... - .then((fileContents) => { - fileContents = fileContents.join("\n")+"\n"; + 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 = 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); // reset to ensure we have enough memory to upload what we need to Puck.write("\x03reset();\n", (result) => {