github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/bin/splice_json (about) 1 #!/usr/bin/env node 2 3 /** 4 * Copyright 2017 Intel Corporation 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * ---------------------------------------------------------------------------- 18 */ 19 20 const { resolve } = require('path') 21 22 const baseDir = process.cwd() 23 const jsonPaths = process.argv.slice(2) 24 25 // Deeply combines objects, overwriting primitive data, but recursively 26 // checking nested objects for unique keys. For example: 27 // {a: 1, b: {c: 2}} + {a: 3, b: {x: 5}} -> {a: 3, b: {c: 2, x: 5}} 28 const spliceObjects = (a, b) => { 29 return Object.keys(b).reduce((spliced, key) => { 30 if (typeof a[key] === 'object' && typeof b[key] === 'object') { 31 spliced[key] = spliceObjects(a[key], b[key]) 32 } else { 33 spliced[key] = b[key] 34 } 35 return spliced 36 }, Object.assign({}, a)) 37 } 38 39 // Splice the JSON files specified then sent to stdout as JSON string 40 process.stdout.write(JSON.stringify(jsonPaths 41 .map(relPath => resolve(baseDir, relPath)) 42 .map(absPath => require(absPath)) 43 .reduce((spliced, jsonObj) => spliceObjects(spliced, jsonObj), {})))