Fix firmwaremaker.js regression under Node.js

pull/379/head
Gordon Williams 2020-05-04 14:07:17 +01:00
parent 11b9069416
commit 8c9233d27f
4 changed files with 78 additions and 57 deletions

View File

@ -3,6 +3,9 @@
Mashes together a bunch of different apps to make
a single firmware JS file which can be uploaded.
*/
var SETTINGS = {
pretokenise : true
};
var path = require('path');
var ROOTDIR = path.join(__dirname, '..');
@ -16,7 +19,7 @@ var APPS = [ // IDs of apps to install
var MINIFY = true;
var fs = require("fs");
var AppInfo = require(ROOTDIR+"/appinfo.js");
var AppInfo = require(ROOTDIR+"/js/appinfo.js");
var appjson = JSON.parse(fs.readFileSync(APPJSON).toString());
var appfiles = [];
@ -49,7 +52,10 @@ function fileGetter(url) {
Promise.all(APPS.map(appid => {
var app = appjson.find(app=>app.id==appid);
if (app===undefined) throw new Error(`App ${appid} not found`);
return AppInfo.getFiles(app, fileGetter).then(files => {
return AppInfo.getFiles(app, {
fileGetter : fileGetter,
settings : SETTINGS
}).then(files => {
appfiles = appfiles.concat(files);
});
})).then(() => {

View File

@ -2,19 +2,28 @@ function toJS(txt) {
return JSON.stringify(txt);
}
if ("undefined"!=typeof module)
Espruino = require("./espruinotools.js");
var AppInfo = {
getFiles : (app,fileGetter) => {
/* Get files needed for app.
options = {
fileGetter : callback for getting URL,
settings : global settings object
}
*/
getFiles : (app,options) => {
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 options.fileGetter(`apps/${app.id}/${storageFile.url}`).then(content => {
if (storageFile.url.endsWith(".js") && !storageFile.url.endsWith(".min.js")) { // if original file ends in '.js'...
return Espruino.transform(content, {
SET_TIME_ON_WRITE : false,
PRETOKENISE : SETTINGS.pretokenise,
PRETOKENISE : options.settings.pretokenise,
//MINIFICATION_LEVEL : "ESPRIMA", // disable due to https://github.com/espruino/BangleApps/pull/355#issuecomment-620124162
builtinModules : "Flash,Storage,heatshrink,tensorflow,locale"
});

View File

@ -10,7 +10,10 @@ reset : (opt) => new Promise((resolve,reject) => {
}),
uploadApp : (app,skipReset) => { // expects an apps.json structure (i.e. with `storage`)
Progress.show({title:`Uploading ${app.name}`,sticky:true});
return AppInfo.getFiles(app, httpGet).then(fileContents => {
return AppInfo.getFiles(app, {
fileGetter : httpGet,
settings : SETTINGS
}).then(fileContents => {
return new Promise((resolve,reject) => {
console.log("uploadApp",fileContents.map(f=>f.name).join(", "));
var maxBytes = fileContents.reduce((b,f)=>b+f.content.length, 0)||1;

View File

@ -66,7 +66,8 @@ var Espruino;
}
// Automatically start up when all is loaded
document.addEventListener("DOMContentLoaded", init);
if (typeof document!=="undefined")
document.addEventListener("DOMContentLoaded", init);
/** Add a processor function of type function(data,callback) */
function addProcessor(eventType, processor) {
@ -6801,3 +6802,5 @@ Espruino.transform = function(code, options) {
});
};
if ("undefined"!=typeof module)
module.exports = Espruino;