Skip to content

Commit

Permalink
feat(server): allow printing out module paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Jan 26, 2022
1 parent 7061a9e commit c412f53
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>} [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<ModuleDefinition[]>} 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]
Expand All @@ -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 }
})
Expand Down

0 comments on commit c412f53

Please sign in to comment.