https://github.com/devtin/js-dir-into-json
Loads content of found javascript and json files in given directory into a single structured object
https://github.com/devtin/js-dir-into-json
Last synced: 4 months ago
JSON representation
Loads content of found javascript and json files in given directory into a single structured object
- Host: GitHub
- URL: https://github.com/devtin/js-dir-into-json
- Owner: devtin
- License: mit
- Created: 2020-06-07T17:11:52.000Z (almost 6 years ago)
- Default Branch: dev
- Last Pushed: 2023-03-04T22:58:34.000Z (about 3 years ago)
- Last Synced: 2025-10-22T02:48:00.329Z (5 months ago)
- Language: JavaScript
- Size: 345 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# js-dir-into-json
> Recursively loads content of found JavaScript and JSON files in given directory into a single structured object
[](https://github.com/devtin/js-dir-into-json/actions)
This module recursively loads exported content of all `*.js` and `*.json` files (configurable via `extensions`) found in
given `directory` using given `fileLoader` (defaults to `require`) creating a single object that preservers the file
structure of the loaded content as the property path, transforming the file and folder names using given
`pathTransformer` function (defaults to
lodash→camelCase).
Useful to split what could be a big configuration object into a file / folder structure.
## Example
Take the following file structure:
```
├── users/
│ ├── index.js
│ ├── maria.json
│ ├── martin.js
│ └── maria.json
└── some-config.json
```
**users/index.js**
```js
module.exports = {
'some-guy': 'Un-altered'
}
```
**users/maria.json**
```json
{
"name": "Maria",
"email": "maria@hotmail.com"
}
```
**users/martin.js**
```js
module.exports = {
name: "Martin",
email: "tin@devtin.io"
}
```
**some-config.js** (see esm support below)
```js
export default {
plugins: [
'plugin-1',
'plugin-2'
]
}
```
And the following script:
```js
const { jsDirIntoJson } = require('js-dir-into-json')
jsDirIntoJson('',
{
// extensions: ['*.js', '*.json'], // minimatch or RegExp
// pathTransformer: default to lodash camelCase
// fileLoader: default to require (or fileLoader = require('esm')(module) for esm support)
}
).then(obj => {
t.deepEquals(obj, {
"users": {
"maria": {
"name": "Maria",
"email": "maria@hotmail.com"
},
"martin": {
"name": "Martin",
"email": "tin@devtin.io"
},
"olivia": "thats me!",
"some-guy": "Un-altered"
},
"someConfig": {
"plugins": [
"plugin-1",
"plugin-2"
]
}
});
});
```
* * *
### License
[MIT](https://opensource.org/licenses/MIT)
© 2020-present Martin Rafael Gonzalez