From e50653ff800d2c91efa93c37c932ade1d6f48a71 Mon Sep 17 00:00:00 2001 From: Marius Gundersen Date: Thu, 30 Nov 2023 10:52:52 +0100 Subject: [PATCH 1/2] Made localModules cwd changeable --- plugins/localModules.js | 48 +++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/plugins/localModules.js b/plugins/localModules.js index c406fc5..e862906 100644 --- a/plugins/localModules.js +++ b/plugins/localModules.js @@ -17,16 +17,24 @@ var path = require('path'); function init() { - Espruino.addProcessor("getModule", function (data, callback) { + Espruino.Core.Config.add("MODULES_DIR_NAME", { + section : "Communications", + name : "Espruino modules folder name", + description : "Name of the folder containing espruino modules.", + type : "string", + defaultValue : "modules" + }); + Espruino.Core.Config.add("MODULES_CWD", { + section : "Communications", + name : "Path to modules", + description : "The filesystem path to use when looking for modules. When calling require('./foo.js') it will load the file at MODULES_CWD+'./foo.js'.", + type : "string", + defaultValue : process.cwd() + }); - /** This is the original code **/ - //if (fs.existsSync("modules/"+data.moduleName)) { - // console.log("Loading local module "+"modules/"+data.moduleName); - // data.moduleCode = fs.readFileSync("modules/"+data.moduleName).toString(); - //} else if (fs.existsSync(data.moduleName)) { - // console.log("Loading local module "+data.moduleName); - // data.moduleCode = fs.readFileSync(data.moduleName).toString(); - //} + Espruino.addProcessor("getModule", function (data, callback) { + var espruino_modules = Espruino.Config.MODULES_DIR_NAME; + var cwd = Espruino.Config.MODULES_CWD; /** * Implements the require.resolve() rules as per node @@ -82,21 +90,29 @@ return false; } + function loadModule(x){ + return loadAsFile(x) || loadAsDirectory(x); + } + function loadEspModules(x, start) { - /** currently don't follow paths up to root, only look in cwd() for modules **/ - var y = 'modules'; //todo: make this a user-definable parameter &/or change to 'espruino_modules' - loadAsFile(path.join(y,x)) || loadAsDirectory(path.join(y,x)); + var current; + do { + current = start; + if (loadModule(path.join(current,espruino_modules,x))) return true; + start = path.resolve(current, '..'); + } while (current !== start); + + return false; } /** * todo: could look at boardJSON.info.builtin_modules and handle nicely but not necessary to have work */ - var y = process.cwd(); var x = data.moduleName; - if(x.indexOf('./')==0 || x.indexOf('/')==0 || x.indexOf('../')==0) { - loadAsFile(path.join(y,x)) || loadAsDirectory(path.join(y,x)); + if (x.indexOf('./')==0 || x.indexOf('/')==0 || x.indexOf('../')==0) { + loadModule(path.join(cwd,x)); } else { - loadEspModules(x, y); + loadEspModules(x,cwd); } callback(data); From 15b481bb08a2fc844753288fadb8a13248a273b2 Mon Sep 17 00:00:00 2001 From: Marius Gundersen Date: Thu, 30 Nov 2023 15:30:28 +0100 Subject: [PATCH 2/2] Make cwd resettable back to process.cwd() --- plugins/localModules.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/localModules.js b/plugins/localModules.js index e862906..8b26ec7 100644 --- a/plugins/localModules.js +++ b/plugins/localModules.js @@ -29,12 +29,13 @@ name : "Path to modules", description : "The filesystem path to use when looking for modules. When calling require('./foo.js') it will load the file at MODULES_CWD+'./foo.js'.", type : "string", - defaultValue : process.cwd() + defaultValue : "" }); Espruino.addProcessor("getModule", function (data, callback) { var espruino_modules = Espruino.Config.MODULES_DIR_NAME; var cwd = Espruino.Config.MODULES_CWD; + if (!cwd) cwd = process.cwd(); /** * Implements the require.resolve() rules as per node