Search code examples
javascriptnode.jstypescriptbabeljsnode-modules

import all files exported by client


I'm making my own restful-APIs framework. Here is a simple overview of my framework and how it suppose to work.

/// backend


   export default abstract class RandomsRoute {
    
    public  async   get     (req: Request, res: Response):  Promise<void> {  res.send (`can't         resolve ${this.__getPath()}`)  }
    
    // returns caller file path
    public  __getPath ():   string  { return getCallerFileName () }

   }

/// client side
class MyRoute extends RandomsRoute {
    public async get(req: Request, res: Response): Promise <void> {
        res.send ('from client side')
    }    
}

export default new MyRoute ();

Users have to extend any class from build in the base class RandomsRoute and override some functions in this case get. __getPath provide file path so I can create express router by using the file path like Next Js.

Under the hood, I'm looking for classes exported by the user. I cloud forces users to pass all exports to a function. for example:-

   import MyRoute from './MyRoute'
   .... so on
   import createRoute from 'mylib'

    createRoute (MyRoute,....) 

but my target is to keep it simple.

is there any way i can import all classes exported by users my own.

Forexample:-
[Folder]route
- index.ts
- products.ts


Solution

  • Try this package:-

    @randoms-pkg/code-generator

    USAGE:-

    • install package as a dev dependency
      ~ npm i @randoms-pkg/code-generator -D
      # or
      ~ yarn add @randoms-pkg/code-generator -D
    
    • Add randoms.config.json file in the root of the project
    {
      "target":  "./src",
      "include":  "^(?:[a-zA-Z0-9]+|\\[(?:\\.{3})?[a-zA-Z0-9]+\\])\\.js$",
      "outputDir": "./dist",
      "outputFile": "output.js",
      "preservedFiles": {
        "./_index.js":  ["getProps"],
        "./server/_server.js": ["getServerSideProps"]
      }
    }
    
    • Add CLI in scripts
     "scripts": {
       "dev": "randoms-generator"
     },
    
    • test
      ~ npm run dev
      # or
      ~ yarn dev