forked from espruino/BangleApps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed duplication in node.js apps with 2 new libraries, added an 'a…
…pp test' program - not finished yet
- Loading branch information
1 parent
cf59aac
commit b89a368
Showing
7 changed files
with
392 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* Node.js library with utilities to handle using the app loader from node.js */ | ||
|
||
var DEVICEID = "BANGLEJS2"; | ||
var MINIFY = true; // minify JSON? | ||
var BASE_DIR = __dirname + "/../.."; | ||
var APPSDIR = BASE_DIR+"/apps/"; | ||
|
||
//eval(require("fs").readFileSync(__dirname+"../core/js/utils.js")); | ||
var Espruino = require(__dirname + "/../../core/lib/espruinotools.js"); | ||
//eval(require("fs").readFileSync(__dirname + "/../../core/lib/espruinotools.js").toString()); | ||
//eval(require("fs").readFileSync(__dirname + "/../../core/js/utils.js").toString()); | ||
var AppInfo = require(__dirname+"/../../core/js/appinfo.js"); | ||
|
||
var SETTINGS = { | ||
pretokenise : true | ||
}; | ||
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 apps = []; | ||
|
||
// call with {DEVICEID:"BANGLEJS/BANGLEJS2"} | ||
exports.init = function(options) { | ||
if (options.DEVICEID) | ||
DEVICEID = options.DEVICEID; | ||
// Load app metadata | ||
var dirs = require("fs").readdirSync(APPSDIR, {withFileTypes: true}); | ||
dirs.forEach(dir => { | ||
var appsFile; | ||
if (dir.name.startsWith("_example") || !dir.isDirectory()) | ||
return; | ||
try { | ||
appsFile = require("fs").readFileSync(APPSDIR+dir.name+"/metadata.json").toString(); | ||
} catch (e) { | ||
ERROR(dir.name+"/metadata.json does not exist"); | ||
return; | ||
} | ||
apps.push(JSON.parse(appsFile)); | ||
}); | ||
}; | ||
|
||
exports.AppInfo = AppInfo; | ||
exports.apps = apps; | ||
|
||
// used by getAppFiles | ||
function fileGetter(url) { | ||
url = BASE_DIR+"/"+url; | ||
console.log("Loading "+url) | ||
var data; | ||
if (MINIFY && url.endsWith(".json")) { | ||
var f = url.slice(0,-5); | ||
console.log("MINIFYING JSON "+f); | ||
var j = eval("("+require("fs").readFileSync(url).toString("binary")+")"); | ||
data = JSON.stringify(j); | ||
} else { | ||
var blob = require("fs").readFileSync(url); | ||
if (url.endsWith(".js") || url.endsWith(".json")) | ||
data = blob.toString(); // allow JS/etc to be written in UTF-8 | ||
else | ||
data = blob.toString("binary") | ||
} | ||
return Promise.resolve(data); | ||
} | ||
|
||
exports.getAppFiles = function(app) { | ||
return AppInfo.getFiles(app, { | ||
fileGetter:fileGetter, | ||
settings : SETTINGS, | ||
device : { id : DEVICEID } | ||
}); | ||
}; | ||
|
||
// Get all the files for this app as a string of Storage.write commands | ||
exports.getAppFilesString = function(app) { | ||
return exports.getAppFiles(app).then(files => { | ||
return files.map(f=>f.cmd).join("\n")+"\n" | ||
}) | ||
}; |
Oops, something went wrong.