From 684b0dc059ba61c99d543749ed72e97cd405be1e Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Mon, 10 Jun 2024 03:02:17 -0400 Subject: [PATCH] Support configurable mod locations --- package-lock.json | 6 ++++ package.json | 1 + packages/backend/src/Kernel.js | 4 ++- .../backend/src/boot/RuntimeEnvironment.js | 29 +++++++++++++++---- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 628ee3da..9f235ef8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ ], "dependencies": { "json-colorizer": "^3.0.1", + "string-template": "^1.0.0", "uuid": "^9.0.1" }, "devDependencies": { @@ -10783,6 +10784,11 @@ "url": "https://github.com/chalk/strip-ansi?sponsor=1" } }, + "node_modules/string-template": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/string-template/-/string-template-1.0.0.tgz", + "integrity": "sha512-SLqR3GBUXuoPP5MmYtD7ompvXiG87QjT6lzOszyXjTM86Uu7At7vNnt2xgyTLq5o9T4IxTYFyGxcULqpsmsfdg==" + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", diff --git a/package.json b/package.json index 892c71c2..16474695 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ }, "dependencies": { "json-colorizer": "^3.0.1", + "string-template": "^1.0.0", "uuid": "^9.0.1" } } diff --git a/packages/backend/src/Kernel.js b/packages/backend/src/Kernel.js index a544cd9a..362c5243 100644 --- a/packages/backend/src/Kernel.js +++ b/packages/backend/src/Kernel.js @@ -198,7 +198,9 @@ class Kernel extends AdvancedBase { const mod_dirnames = fs.readdirSync(mods_dirpath); for ( const mod_dirname of mod_dirnames ) { const mod_path = path_.join(mods_dirpath, mod_dirname); - if ( ! fs.lstatSync(mod_path).isDirectory() ) { + + const stat = fs.statSync(mod_path); + if ( ! stat.isDirectory() ) { continue; } diff --git a/packages/backend/src/boot/RuntimeEnvironment.js b/packages/backend/src/boot/RuntimeEnvironment.js index 67c41766..4fbf07a4 100644 --- a/packages/backend/src/boot/RuntimeEnvironment.js +++ b/packages/backend/src/boot/RuntimeEnvironment.js @@ -180,7 +180,7 @@ const mod_paths = ({ path_checks }) => ({ path_ }) => [ }, { get path () { - return path_.join(original_cwd, 'mods'); + return path_.join(path_.dirname(require.main.filename), '../mods'); }, checks: [ path_checks.skip_if_not_exists ], }, @@ -191,6 +191,7 @@ class RuntimeEnvironment extends AdvancedBase { fs: require('node:fs'), path_: require('node:path'), crypto: require('node:crypto'), + format: require('string-template'), } constructor ({ logger }) { @@ -213,6 +214,12 @@ class RuntimeEnvironment extends AdvancedBase { } init_ () { + // This variable, called "environment", will be passed back to Kernel + // with some helpful values. A partial-population of this object later + // in this function will be used when evaluating configured paths. + const environment = {}; + environment.source = this.modules.path_.dirname(require.main.filename); + const config_path_entry = this.get_first_suitable_path_( { pathFor: 'configuration' }, this.config_paths, @@ -299,14 +306,26 @@ class RuntimeEnvironment extends AdvancedBase { // console.log({ ...config.services }); const mod_paths = []; + environment.mod_paths = mod_paths; - if ( mods_path_entry ) { + // TODO: implement `get_all_suitable_paths_` so we can load mods + // from multiple locations. Note: we'll need to carefully consider + // how this is configured. + if ( false ) if ( mods_path_entry ) { mod_paths.push(mods_path_entry.path); } - return { - mod_paths, - }; + // If configured, add a user-specified mod path + if ( config.mod_directories ) { + for ( const dir of config.mod_directories ) { + const mods_directory = this.modules.format( + dir, environment, + ); + mod_paths.push(mods_directory); + } + } + + return environment; } get_first_suitable_path_ (meta, paths, last_checks) {