3

I'm working with a project that exports some functions from an exports.js file.

// some-project/exports.js
export { loginFunction } from "./login/function"
export { registerFunction } from "./register/function"

I want to develop a tool that can import such a file and output the filesystem paths of all its exports. For example,

const projectExports = require("some-project/exports")

const exportPaths = findExportPaths(projectExports)

console.log(exportPaths) // ["/Projects/some-project/login/function.js", "/Projects/some-project/register/function.js"]

How can I implement the functionality of findExportPaths?

I have tried the following methods/tools to accomplish this, but could not make them work:

Any help would be greatly appreciated.

harold_admin
  • 1,117
  • 2
  • 10
  • 20
  • An export doesn't have a path. – Keith May 01 '21 at 06:49
  • I understand. Exploring options outside JS code, Is it possible for static analysis tools to find the path? Or would it be possible to attach one to it? Perhaps using some Typescript transformers or decorators magic? – harold_admin May 01 '21 at 07:35
  • the only thing that comes to my mind is trimming the name of the module from the string in require, then search it in the fileSystem with `fs.readDir` (if you're using nodejs) or similar ( https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback ) , then if found console.log the path – Don Diego May 01 '21 at 08:40
  • Interesting idea. It's not reliable enough for my needs, but I appreciate the input. – harold_admin May 01 '21 at 08:50
  • 1
    I've had some success with the [dependency-tree](https://www.npmjs.com/package/dependency-tree) module. – harold_admin Dec 13 '21 at 16:33

0 Answers0