From c412f53f115b5b5259d903a07f04ebfe8319c744 Mon Sep 17 00:00:00 2001 From: pan93412 Date: Wed, 26 Jan 2022 14:42:50 +0800 Subject: [PATCH] feat(server): allow printing out module paths --- server.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index 54c03cc..4d8e4b0 100644 --- a/server.js +++ b/server.js @@ -54,14 +54,20 @@ const VERSION_CHECK_RESULT = { /** * Get the module definitions dynamically. * - * @param {string} modulePath The path to modules (JS). + * @param {string} modulesPath The path to modules (JS). * @param {Record} [specificRoute] The specific route of specific modules. + * @param {boolean} [doRequire] If true, require() the module directly. + * Otherwise, print out the module path. Default to true. * @returns {Promise} The module definitions. * * @example getModuleDefinitions("./module", {"album_new.js": "/album/create"}) */ -async function getModulesDefinitions(modulePath, specificRoute) { - const files = await fs.promises.readdir(path.join(__dirname, 'module')) +async function getModulesDefinitions( + modulesPath, + specificRoute, + doRequire = true, +) { + const files = await fs.promises.readdir(modulesPath) const parseRoute = (/** @type {string} */ fileName) => specificRoute && fileName in specificRoute ? specificRoute[fileName] @@ -73,7 +79,8 @@ async function getModulesDefinitions(modulePath, specificRoute) { .map((file) => { const identifier = file.split('.').shift() const route = parseRoute(file) - const module = require(path.join(modulePath, file)) + const modulePath = path.join(modulesPath, file) + const module = doRequire ? require(modulePath) : modulePath return { identifier, route, module } })