BangleApps/bin/firmwaremaker.js

84 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-11-10 10:22:33 +00:00
#!/usr/bin/nodejs
/*
Mashes together a bunch of different apps to make
a single firmware JS file which can be uploaded.
*/
var SETTINGS = {
pretokenise : true
};
2019-11-10 10:22:33 +00:00
var path = require('path');
var ROOTDIR = path.join(__dirname, '..');
var APPDIR = ROOTDIR+'/apps';
var OUTFILE = ROOTDIR+'/firmware.js';
var DEVICE = "BANGLEJS";
2019-11-10 10:22:33 +00:00
var APPS = [ // IDs of apps to install
2020-03-09 17:06:13 +00:00
"boot","launch","mclock","setting",
"about","alarm","widbat","widbt","welcome"
2019-11-10 10:22:33 +00:00
];
2020-03-09 17:06:13 +00:00
var MINIFY = true;
2019-11-10 10:22:33 +00:00
var fs = require("fs");
2020-09-22 14:33:55 +00:00
global.Const = {
/* Are we only putting a single app on a device? If so
apps should all be saved as .bootcde and we write info
about the current app into app.info */
SINGLE_APP_ONLY : false,
};
var AppInfo = require(ROOTDIR+"/core/js/appinfo.js");
2019-11-10 10:22:33 +00:00
var appfiles = [];
function fileGetter(url) {
console.log("Loading "+url)
2020-03-09 17:06:13 +00:00
if (MINIFY) {
2020-03-10 15:33:41 +00:00
/*if (url.endsWith(".js")) {
2020-03-09 17:06:13 +00:00
var f = url.slice(0,-3);
console.log("MINIFYING "+f);
const execSync = require('child_process').execSync;
2020-03-10 15:33:41 +00:00
// --config PRETOKENISE=true
// --minify
code = execSync(`espruino --config SET_TIME_ON_WRITE=false --minify --board BANGLEJS ${f}.js -o ${f}.min.js`);
2020-03-09 17:06:13 +00:00
console.log(code.toString());
url = f+".min.js";
2020-03-10 15:33:41 +00:00
}*/
2020-03-09 17:06:13 +00:00
if (url.endsWith(".json")) {
var f = url.slice(0,-5);
console.log("MINIFYING JSON "+f);
var j = eval("("+fs.readFileSync(url).toString("binary")+")");
2020-03-09 17:06:13 +00:00
var code = JSON.stringify(j);
//console.log(code);
url = f+".min.json";
fs.writeFileSync(url, code);
}
}
return Promise.resolve(fs.readFileSync(url).toString("binary"));
2019-11-10 10:22:33 +00:00
}
Promise.all(APPS.map(appid => {
2022-01-19 15:52:52 +00:00
try {
var app = JSON.parse(fs.readFileSync(APPDIR + "/" + appid + "/metadata.json").toString());
2022-01-19 15:52:52 +00:00
} catch (e) {
throw new Error(`App ${appid} not found`);
}
return AppInfo.getFiles(app, {
fileGetter : fileGetter,
settings : SETTINGS,
device : { id : DEVICE }
}).then(files => {
2019-11-10 10:22:33 +00:00
appfiles = appfiles.concat(files);
});
})).then(() => {
//console.log(appfiles);
2020-03-11 16:30:36 +00:00
var js = "// Generated by BangleApps/bin/firmwaremaker.js\n";
2019-11-10 10:22:33 +00:00
appfiles.forEach((file) => {
js += file.cmd+"\n";
2020-03-10 15:33:41 +00:00
/*if (file.crc && file.evaluate!==true) {
js += `\x10if (E.CRC32(require('Storage').read(${JSON.stringify(file.name)}))!=${file.crc}){console.log("${file.name} invalid");FAIL++}\n`;
}*/
2019-11-10 10:22:33 +00:00
});
// js = js.replace(/\x10/g,""); // remove the echo-off characters (for testing only)
2019-11-10 10:22:33 +00:00
fs.writeFileSync(OUTFILE, js);
console.log("Output written to "+OUTFILE);
});