merge-files-content

0.1.5 • Public • Published

merge-files-content Build Status codecov

Node.JS utility function to recursively load multiple directories and files as one. Useful for project-wide configuration files

Table of contents

  1. Installation
  2. Usage
  3. API

Installation

npm i merge-files-content

or

yarn add merge-files-content

Usage

This module exports a single function which receives two arguments: the file(s) and/or directories full paths, and a (optional) settings object.

Example

Let's say we have a src/config directory with 3 files exporting objects:

  • /database.js
  • /storage.json
  • /nested/file.js

Note: In this case we have .js and .json files, but you can use any file extension as long as its supported by require().

If the directory contains unsupported module types, the program shall crash.

/**
 * /src/config/database.js
 */
module.exports = {
  PORT: 3000
}

/src/config/storage.json

{
  "driver": "s3"
}
/**
 * /src/config/database.js
 */
module.exports = 'Hello World!'
/**
 * /index.js
 */
const mfc = require('merge-files-content');
const path = require('path');
 
const config = mfc(path.resolve('src/config'));
 
config.database.PORT // 3000
config.storage.driver // s3
config.nested.file // Hello World!

API

mfc(AbsolutePaths, MfcSettings?): Object

AbsolutePaths

Either a single or an array of absolute paths. They can point either to a file, a dir, or a mix of boths.

MfcSettings

An optional object of settings

MfcSettings.useFilenames (default: true)

When true, the file exported contents will live under a key (namespace) equal to the camelCased version of the file name (without its extension).

Note: this option is ignored if you're loading a single file instead of a directory. In that case, mfc behaves just like require().

MfcSettings.maxDepth (default: 3)

The max level of deepness you want the script to look for files.

/merge-files-content/

    Package Sidebar

    Install

    npm i merge-files-content

    Weekly Downloads

    6

    Version

    0.1.5

    License

    MIT

    Unpacked Size

    12.8 kB

    Total Files

    16

    Last publish

    Collaborators

    • frondor