Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ufukbakan/generic-import


https://github.com/ufukbakan/generic-import

Last synced: 4 days ago
JSON representation

Awesome Lists containing this project

README

        

generic-import


Iterable, recursive, generic, cumulative imports.


Usage

```ts
import genericImport from "generic-import";
...express backend initial code

interface MyRouterInterface {
basepath: string,
router: Router // imported from express
}

const routesPath = path.join(process.cwd(), "routes");
const routers = genericImport(routesPath);
// [
// { filename: "router1", value: Object },
// { filename: "router2", value: Object },
// ]
for(const r of routers){ // for of loop supported in only ES syntax
app.use(r.value.basepath, r.value.router);
}

app.listen(8080);
```

Why use this approach?


It provides no coupling between main script file and route files. So there is much more scalable code.

Options

- **camelize** *boolean* Camelizes returned file names of modules
- **fileExtensions** *array* Dot prefixed file extensions to include in search
- **recursive** *boolean* Enables search in nested folders

Forked from import-modules