Skip to content

ezze/copy-json-file-merged

Repository files navigation

copy-json-file-merged

npm version Build Status Coverage Status

Copies source JSON file and merges its contents with destination JSON file if present.

Installation

npm install copy-json-file-merged --save

Usage

In code:

var copyJsonFileMerged = require('copy-json-file-merged');
copyJsonFileMerged('src.json', 'dest.json', {
    overwrite: true,
    mergeKey: 'id'
});

From CLI:

./node_modules/.bin/copy-json-file-merged src.json dest.json --overwrite --mergeKey id

From package.json's scripts:

{
  "scripts": {
    "copy": "copy-json-file-merged src.json dest.json --overwrite --mergeKey id"
  }
}

API

copyJsonFileMerged function accepts three arguments:

  • src — path to source JSON file (must exist);
  • dest — path to destination JSON file (can exist);
  • options — object of properties (optional):
    • mergeKey — a key (property) to merge nested objects and arrays by (see merge arrays uniting their object items by specified identifier field), by default is id;

    • overwrite — shows whether source object should overwrite destination one (if it exists), by default is false; alternatively, an array of top-level properties' names of source JSON object can be passed to limit sections of destination JSON file to overwrite, e.g.:

      src.json

      {
        "obj1": { "merged": true },
        "obj2": { "merged": true }
      }

      dest.json

      {
        "obj1": { "merged": false },
        "obj2": { "merged": false }
      }
      copyJsonFileMerged('src.json', 'dest.json', {
          overwrite: ['obj1']
      }); // => { "obj1": { "merged": true }, "obj2": { "merged": false } }

Building

In order to build library run:

npm run build

Testing

Run unit tests:

npm test

In order to run tests with coverage run the following command:

npm run test:coverage

In order to run tests with Coveralls locally you have to provide COVERALLS_REPO_TOKEN:

COVERALLS_REPO_TOKEN=<token> npm run test:coveralls

Contribution

Before making a pull request, please, be sure that your changes are rebased to develop branch.

License

MIT

About

Copies source JSON file and merges its contents with destination JSON file if present.

Resources

License

Stars

Watchers

Forks

Packages

No packages published