js-dir-into-json

2.7.1 • Public • Published

js-dir-into-json

Recursively loads content of found JavaScript and JSON files in given directory into a single structured object

Version tests

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:

<directory>
├── users/
│   ├── index.js
│   ├── maria.json
│   ├── martin.js
│   └── maria.json
└── some-config.json

users/index.js

module.exports = {
  'some-guy': 'Un-altered'
}

users/maria.json

{
  "name": "Maria",
  "email": "maria@hotmail.com"
}

users/martin.js

module.exports = {
  name: "Martin",
  email: "tin@devtin.io"
}

some-config.js (see esm support below)

export default {
  plugins: [
    'plugin-1',
    'plugin-2'
  ]
}

And the following script:

const { jsDirIntoJson } = require('js-dir-into-json')

jsDirIntoJson('<directory>',
  {
    // 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

© 2020-present Martin Rafael Gonzalez tin@devtin.io

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.7.1
    286
    • latest

Version History

Package Sidebar

Install

npm i js-dir-into-json

Weekly Downloads

222

Version

2.7.1

License

MIT

Unpacked Size

27 kB

Total Files

22

Last publish

Collaborators

  • tin_r