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

View File

@ -2,19 +2,28 @@ function toJS(txt) {
return JSON.stringify(txt); return JSON.stringify(txt);
} }
if ("undefined"!=typeof module)
Espruino = require("./espruinotools.js");
var AppInfo = { 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) => { return new Promise((resolve,reject) => {
// Load all files // Load all files
Promise.all(app.storage.map(storageFile => { Promise.all(app.storage.map(storageFile => {
if (storageFile.content) if (storageFile.content)
return Promise.resolve(storageFile); return Promise.resolve(storageFile);
else if (storageFile.url) 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'... if (storageFile.url.endsWith(".js") && !storageFile.url.endsWith(".min.js")) { // if original file ends in '.js'...
return Espruino.transform(content, { return Espruino.transform(content, {
SET_TIME_ON_WRITE : false, 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 //MINIFICATION_LEVEL : "ESPRIMA", // disable due to https://github.com/espruino/BangleApps/pull/355#issuecomment-620124162
builtinModules : "Flash,Storage,heatshrink,tensorflow,locale" 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`) uploadApp : (app,skipReset) => { // expects an apps.json structure (i.e. with `storage`)
Progress.show({title:`Uploading ${app.name}`,sticky:true}); 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) => { return new Promise((resolve,reject) => {
console.log("uploadApp",fileContents.map(f=>f.name).join(", ")); console.log("uploadApp",fileContents.map(f=>f.name).join(", "));
var maxBytes = fileContents.reduce((b,f)=>b+f.content.length, 0)||1; 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 // 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) */ /** Add a processor function of type function(data,callback) */
function addProcessor(eventType, processor) { function addProcessor(eventType, processor) {
@ -4122,7 +4123,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
} else if (isIn(chNum,ch)) { // NUMBER } else if (isIn(chNum,ch)) { // NUMBER
type = "NUMBER"; type = "NUMBER";
var chRange = chNum; var chRange = chNum;
if (ch=="0") { // Handle if (ch=="0") { // Handle
s+=ch; s+=ch;
nextCh(); nextCh();
if ("xXoObB".indexOf(ch)>=0) { if ("xXoObB".indexOf(ch)>=0) {
@ -4131,12 +4132,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
if (ch=="x" || ch=="X") chRange="0123456789ABCDEFabcdef"; if (ch=="x" || ch=="X") chRange="0123456789ABCDEFabcdef";
s+=ch; s+=ch;
nextCh(); nextCh();
} }
} }
while (isIn(chRange,ch) || ch==".") { while (isIn(chRange,ch) || ch==".") {
s+=ch; s+=ch;
nextCh(); nextCh();
} }
} else if (isIn(chQuotes,ch)) { // STRING } else if (isIn(chQuotes,ch)) { // STRING
type = "STRING"; type = "STRING";
var q = ch; var q = ch;
@ -4727,39 +4728,39 @@ Object.defineProperty(exports, '__esModule', { value: true });
This Source Code is subject to the terms of the Mozilla Public This Source Code is subject to the terms of the Mozilla Public
License, v2.0. If a copy of the MPL was not distributed with this License, v2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/. file, You can obtain one at http://mozilla.org/MPL/2.0/.
------------------------------------------------------------------ ------------------------------------------------------------------
Central place to store and retrieve Options Central place to store and retrieve Options
To use this, on your plugin's `init` function, do something like the To use this, on your plugin's `init` function, do something like the
following: following:
Espruino.Core.Config.add("MAX_FOOBARS", { Espruino.Core.Config.add("MAX_FOOBARS", {
section : "Communications", // Heading this will come under in the config screen section : "Communications", // Heading this will come under in the config screen
name : "Foobars", // Nice name name : "Foobars", // Nice name
description : "How many foobars?", // More detail about this description : "How many foobars?", // More detail about this
type : "int"/"boolean"/"string"/{ value1:niceName, value2:niceName }, type : "int"/"boolean"/"string"/{ value1:niceName, value2:niceName },
defaultValue : 20, defaultValue : 20,
onChange : function(newValue) { ... } onChange : function(newValue) { ... }
}); });
* onChange will be called whenever the value changes from the default * onChange will be called whenever the value changes from the default
(including when it is loaded) (including when it is loaded)
Then use: Then use:
Espruino.Config.MAX_FOOBARS in your code Espruino.Config.MAX_FOOBARS in your code
------------------------------------------------------------------ ------------------------------------------------------------------
**/ **/
"use strict"; "use strict";
(function() { (function() {
/** See addSection and getSections */ /** See addSection and getSections */
var builtinSections = {}; var builtinSections = {};
function _get(callback) { function _get(callback) {
if (typeof chrome !== 'undefined' && chrome.storage) { if (typeof chrome !== 'undefined' && chrome.storage) {
chrome.storage.sync.get( "CONFIGS", function (data) { chrome.storage.sync.get( "CONFIGS", function (data) {
var value = data["CONFIGS"]; var value = data["CONFIGS"];
console.log("GET chrome.storage.sync = "+JSON.stringify(value)); console.log("GET chrome.storage.sync = "+JSON.stringify(value));
callback(value); callback(value);
@ -4770,55 +4771,55 @@ Object.defineProperty(exports, '__esModule', { value: true });
if (cookie!==undefined && cookie.indexOf("CONFIG=")>=0) { if (cookie!==undefined && cookie.indexOf("CONFIG=")>=0) {
cookie = cookie.substring(cookie.indexOf("CONFIG=")+7); cookie = cookie.substring(cookie.indexOf("CONFIG=")+7);
cookie = cookie.substring(0,cookie.indexOf(";")); cookie = cookie.substring(0,cookie.indexOf(";"));
try { try {
var json = atob(cookie); var json = atob(cookie);
data = JSON.parse(json); data = JSON.parse(json);
} catch (e) { } catch (e) {
console.log("Got ", e, " while reading info"); console.log("Got ", e, " while reading info");
} }
} }
callback(data); callback(data);
} else { } else {
callback({}); callback({});
} }
} }
function _set(data) { function _set(data) {
if (typeof chrome !== 'undefined' && chrome.storage) { if (typeof chrome !== 'undefined' && chrome.storage) {
console.log("SET chrome.storage.sync = "+JSON.stringify(data)); console.log("SET chrome.storage.sync = "+JSON.stringify(data));
chrome.storage.sync.set({ CONFIGS : data }); chrome.storage.sync.set({ CONFIGS : data });
} else if (typeof document != "undefined") { } else if (typeof document != "undefined") {
document.cookie = "CONFIG="+btoa(JSON.stringify(data)); document.cookie = "CONFIG="+btoa(JSON.stringify(data));
} }
} }
function loadConfiguration(callback) { function loadConfiguration(callback) {
_get(function (value) { _get(function (value) {
for (var key in value) { for (var key in value) {
if (key=="set") continue; if (key=="set") continue;
Espruino.Config[key] = value[key]; Espruino.Config[key] = value[key];
if (Espruino.Core.Config.data[key] !== undefined && if (Espruino.Core.Config.data[key] !== undefined &&
Espruino.Core.Config.data[key].onChange !== undefined) Espruino.Core.Config.data[key].onChange !== undefined)
Espruino.Core.Config.data[key].onChange(value[key]); Espruino.Core.Config.data[key].onChange(value[key]);
} }
if (callback!==undefined) if (callback!==undefined)
callback(); callback();
}); });
} }
function init() { function init() {
addSection("General", { sortOrder:100, description: "General Web IDE Settings" }); addSection("General", { sortOrder:100, description: "General Web IDE Settings" });
addSection("Communications", { sortOrder:200, description: "Settings for communicating with the Espruino Board" }); addSection("Communications", { sortOrder:200, description: "Settings for communicating with the Espruino Board" });
addSection("Board", { sortOrder:300, description: "Settings for the Espruino Board itself" }); addSection("Board", { sortOrder:300, description: "Settings for the Espruino Board itself" });
} }
function add(name, options) { function add(name, options) {
Espruino.Core.Config.data[name] = options; Espruino.Core.Config.data[name] = options;
if (Espruino.Config[name] === undefined) if (Espruino.Config[name] === undefined)
Espruino.Config[name] = options.defaultValue; Espruino.Config[name] = options.defaultValue;
} }
/** Add a section (or information on the page). /** Add a section (or information on the page).
* options = { * options = {
* sortOrder : int, // a number used for sorting * sortOrder : int, // a number used for sorting
@ -4830,7 +4831,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
options.name = name; options.name = name;
builtinSections[name] = options; builtinSections[name] = options;
} }
/** Get an object containing the information on a section used in configs */ /** Get an object containing the information on a section used in configs */
function getSection(name) { function getSection(name) {
if (builtinSections[name]!==undefined) if (builtinSections[name]!==undefined)
@ -4840,7 +4841,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
name : name name : name
}; };
} }
/** Get an object containing information on all 'sections' used in all the configs */ /** Get an object containing information on all 'sections' used in all the configs */
function getSections() { function getSections() {
var sections = []; var sections = [];
@ -4850,26 +4851,26 @@ Object.defineProperty(exports, '__esModule', { value: true });
// add other sections // add other sections
for (var i in Espruino.Core.Config.data) { for (var i in Espruino.Core.Config.data) {
var c = Espruino.Core.Config.data[i]; var c = Espruino.Core.Config.data[i];
var found = false; var found = false;
for (var s in sections) for (var s in sections)
if (sections[s].name == c.section) if (sections[s].name == c.section)
found = true; found = true;
if (!found) { if (!found) {
console.warn("Section named "+c.section+" was not added with Config.addSection"); console.warn("Section named "+c.section+" was not added with Config.addSection");
sections[c.section] = { sections[c.section] = {
name : c.section, name : c.section,
sortOrder : 0 sortOrder : 0
}; };
} }
} }
// Now sort by sortOrder // Now sort by sortOrder
sections.sort(function (a,b) { return a.sortOrder - b.sortOrder; }); sections.sort(function (a,b) { return a.sortOrder - b.sortOrder; });
return sections; return sections;
} }
Espruino.Config = {}; Espruino.Config = {};
Espruino.Config.set = function (key, value) { Espruino.Config.set = function (key, value) {
if (Espruino.Config[key] != value) { if (Espruino.Config[key] != value) {
@ -4882,11 +4883,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
var data = {}; var data = {};
for (var key in Espruino.Config) for (var key in Espruino.Config)
if (key != "set") if (key != "set")
data[key] = Espruino.Config[key]; data[key] = Espruino.Config[key];
_set(data); _set(data);
} }
}; };
function clearAll() { // clear all settings function clearAll() { // clear all settings
_set({}); _set({});
for (var name in Espruino.Core.Config.data) { for (var name in Espruino.Core.Config.data) {
@ -4894,22 +4895,22 @@ Object.defineProperty(exports, '__esModule', { value: true });
Espruino.Config[name] = options.defaultValue; Espruino.Config[name] = options.defaultValue;
} }
} }
Espruino.Core.Config = { Espruino.Core.Config = {
loadConfiguration : loadConfiguration, // special - called before init loadConfiguration : loadConfiguration, // special - called before init
init : init, init : init,
add : add, add : add,
data : {}, data : {},
addSection : addSection, addSection : addSection,
getSection : getSection, getSection : getSection,
getSections : getSections, getSections : getSections,
clearAll : clearAll, // clear all settings clearAll : clearAll, // clear all settings
}; };
})(); })();
/* /*
Gordon Williams (gw@pur3.co.uk) Gordon Williams (gw@pur3.co.uk)
@ -5076,7 +5077,7 @@ To add a new serial device, you must add an object to
} }
connectionInfo = undefined; connectionInfo = undefined;
flowControlXOFF = false; flowControlXOFF = false;
currentDevice = portToDevice[serialPort]; currentDevice = portToDevice[serialPort];
currentDevice.open(serialPort, function(cInfo) { // CONNECT currentDevice.open(serialPort, function(cInfo) { // CONNECT
if (!cInfo) { if (!cInfo) {
@ -5117,7 +5118,7 @@ To add a new serial device, you must add an object to
// Just call connectCallback(undefined), don't bother sending disconnect // Just call connectCallback(undefined), don't bother sending disconnect
connectCallback(undefined); connectCallback(undefined);
return; return;
} }
connectionInfo = undefined; connectionInfo = undefined;
if (writeTimeout!==undefined) if (writeTimeout!==undefined)
clearTimeout(writeTimeout); clearTimeout(writeTimeout);
@ -6435,18 +6436,18 @@ To add a new serial device, you must add an object to
This Source Code is subject to the terms of the Mozilla Public This Source Code is subject to the terms of the Mozilla Public
License, v2.0. If a copy of the MPL was not distributed with this License, v2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/. file, You can obtain one at http://mozilla.org/MPL/2.0/.
------------------------------------------------------------------ ------------------------------------------------------------------
Try and get any URLS that are from GitHub Try and get any URLS that are from GitHub
------------------------------------------------------------------ ------------------------------------------------------------------
**/ **/
"use strict"; "use strict";
(function(){ (function(){
function init() { function init() {
Espruino.addProcessor("getURL", getGitHub); Espruino.addProcessor("getURL", getGitHub);
} }
function getGitHub(data, callback) { function getGitHub(data, callback) {
var match = data.url.match(/^https?:\/\/github.com\/([^\/]+)\/([^\/]+)\/blob\/([^\/]+)\/(.*)$/); var match = data.url.match(/^https?:\/\/github.com\/([^\/]+)\/([^\/]+)\/blob\/([^\/]+)\/(.*)$/);
if (match) { if (match) {
@ -6456,7 +6457,7 @@ To add a new serial device, you must add an object to
branch : match[3], branch : match[3],
path : match[4] path : match[4]
}; };
var url = "https://raw.githubusercontent.com/"+git.owner+"/"+git.repo+"/"+git.branch+"/"+git.path; var url = "https://raw.githubusercontent.com/"+git.owner+"/"+git.repo+"/"+git.branch+"/"+git.path;
console.log("Found GitHub", JSON.stringify(git)); console.log("Found GitHub", JSON.stringify(git));
callback({url: url}); callback({url: url});
@ -6801,3 +6802,5 @@ Espruino.transform = function(code, options) {
}); });
}; };
if ("undefined"!=typeof module)
module.exports = Espruino;