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