json-combiner
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

Build Status

json-combiner

json-combiner is a tool that ingests a folder of source files to produce a single JSON file with the same structure as the source folder. json-combiner will take the following types of files:

  • .json - Standard JS comments are stripped out of these files
  • .json5 - https://json5.org/
  • .js - Uses Node's require() to load the file. See below.
  • .ts - Uses ts-node and Node's require() to load the file. See below.

Installation

npm install json-combiner

Usage:

-s, --src  Source to folder of files to combine
-o, --out  Output JSON path (single file)
-m, --minify  If the output file should be minified. Default is false.

json-combiner -s ./configSrc/ -o ./lib/config.json

Examples

Assuming we have the following source JSON files:

src/foo.json:

{
    "title": "The Foo",
    "name":  "A wonderful component"
}

src/bar.json:

{
    "title": "The Bar",
    "name":  "An even more wonderful component"
}

Will generate the following JSON file as output:

{
    "foo": {
        "title": "The Foo",
        "name":  "A wonderful component"
    },
    "bar": {
        "title": "The Bar",
        "name":  "An even more wonderful component"
    }
}

Merging of Files and Folders

If a .json file and a folder share the same name, they will be merged into one object when the JSON is concatenated. Assuming we have the following source JSON files:

src/foo.json:

{
    "default": {
        "title": "The Foo",
        "name":  "A wonderful component"
    }
}

src/foo/bar.json:

{
    "title": "The Bar",
    "name":  "An even more wonderful component"
}

Will generate the following JSON file as output:

{
    "foo": {
        "default": {
            "title": "The Foo",
            "name":  "A wonderful component"
        },
        "bar": {
            "title": "The Bar",
            "name":  "An even more wonderful component"
        }
    }
}

Folder-as-Array Example

The contents of a folder can be grouped together as an array. The folder name must end in '[]'. For the files

  • src/foo[]/foo1.json:
  • src/foo[]/foo2.json:
  • src/foo[]/foo3.json:
{
    "foo": [
        {
            //contents of foo1.json...
        },
        {
            //contents of foo2.json...
        },
        {
            //contents of foo3.json...
        },
    ]
}

Note, that the .json files in an array folder do not retain their file names as keys, since they are now array index items.

Handling JavaScript files

A JavaScript file will be required (require()), and the module export (module.exports = ...) will be used as the object to be stringified. If the module export is a function, then that function will be called to obtain the result - if you need to do async stuff then return a promise from your function.

module.exports = {
    TWO_PI: Math.PI * 2,
    foo: "bar"
};
module.exports = () => {
    return new Promise((resolve) => {
        let rtn = [];
        for(let i = 100; i > 50; --i)
            rtn.push(i);
        //The return value here is the final result, which saves us from having to make our array
        //of integer values from 100 to 51 by hand.
        resolve(rtn);
    });
};

Handling TypeScript files

A TypeScript file will be required (require()) using ts-node, and the module export (export = ...) will be used as the object to be stringified. If the module export is a function, then that function will be called to obtain the result - if you need to do async stuff then return a promise from your function. NOTE: ts-node is a peer dependency, you must provide the version you wish to use.

export = {
    TWO_PI: Math.PI * 2,
    foo: "bar"
};

Code as Text

If for some reason you want to have JavaScript code inserted into your JSON as a string, use a file with a .text.js or .text.ts (must provide your version of TypeScript) extension. The JavaScript code will be added to the output as a string instead of running the code for a result.

Credits

This was originally implemented in https://github.com/SpringRoll/grunt-concat-json

Readme

Keywords

none

Package Sidebar

Install

npm i json-combiner

Weekly Downloads

5

Version

2.1.0

License

MIT

Unpacked Size

34.1 kB

Total Files

7

Last publish

Collaborators

  • andrewstart