Last active
November 3, 2018 14:55
-
-
Save labiak/123e6ebb6c7e4d496449c4134101e4b0 to your computer and use it in GitHub Desktop.
This is a script for applying changes to base Postman import file. Use node merge-postman.js <source> <target> Where <source> - is a base JSON import file for applying changes And <target> - file with new request to add to <source> file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a script for applying changes to base Postman import file. | |
* Use | |
* node merge-postman.js <source> <target> | |
* Where <source> - is a base JSON import file for applying changes | |
* And <target> - file with new request to add to <source> file | |
* @author Taras Labiak <[email protected]> | |
* {@link https://indeema.com Indeema} | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
function pretty(object) { | |
return JSON.stringify(object, null, ' '); | |
} | |
function read(reader = process.stdin, type = 'json', encoding = 'utf8') { | |
return read[type](reader, encoding); | |
} | |
read.buffer = function buffer(reader = process.stdin) { | |
return new Promise(function (resolve, reject) { | |
const chunks = []; | |
reader.on('data', function (chunk) { | |
chunks.push(chunk); | |
}); | |
reader.on('error', function (error) { | |
reject(error); | |
}); | |
reader.on('end', function () { | |
resolve(chunks.length > 1 ? new Buffer(chunks) : chunks[0]); | |
}); | |
}); | |
}; | |
read.string = async function string(reader = process.stdin, encoding = 'utf8') { | |
if ('string' === typeof reader) { | |
reader = fs.createReadStream(reader, {encoding}); | |
} | |
const buffer = await read.buffer(reader); | |
return buffer.toString(encoding); | |
}; | |
read.json = async function json(reader = process.stdin, encoding = 'utf8') { | |
const string = await read.string(reader, encoding); | |
return JSON.parse(string); | |
}; | |
function getProcessArguments(args = process.argv) { | |
const index = args.map(a => path.basename(a)).indexOf('node'); | |
return args.slice(index + 2); | |
} | |
function findByName(array, name) { | |
return array.find(item => name === item.name); | |
} | |
async function main() { | |
const filenames = getProcessArguments(); | |
const [target, source] = await Promise.all(filenames.map(filename => read.json(filename))); | |
for(const item of source.item) { | |
const targetCollection = findByName(target.item, item.name); | |
if (targetCollection) { | |
for(const sourceItem of item.item) { | |
const targetItem = findByName(targetCollection.item, sourceItem.name); | |
if (!targetItem) { | |
targetCollection.item.push(sourceItem); | |
} | |
} | |
} | |
else { | |
target.item.push(item); | |
} | |
} | |
fs.writeFileSync(filenames[0], pretty(target)); | |
} | |
void main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment