postman-json-chopper
Version:
Splits up large postman collection json file into folders and smaller files and merges them back together
70 lines (64 loc) • 2.21 kB
text/typescript
import fs from "fs";
import path from "path";
import slug from "slug";
export function split(inFile: string, outFile: string, maxDepth?: number){
// If maxDepth is not provided, set it to the maximum safe integer value
maxDepth = maxDepth !== undefined ? maxDepth : Number.MAX_SAFE_INTEGER;
// read file to memory
var col;
try {
col = JSON.parse(fs.readFileSync(inFile).toString());
} catch (e) {
console.info(e);
process.exit(0);
}
var visitedNames = {};
let outFilePath = path.parse(outFile);
var itemQueue = [
{
jsonObject: col,
directory: outFilePath.dir,
fileName: outFilePath.name,
depth:0
}
]
while (itemQueue.length > 0){
var currentItem = itemQueue.shift();
if (typeof currentItem !== "undefined"){
if (Array.isArray(currentItem.jsonObject.item) && currentItem.depth < maxDepth){
for(let i = 0; i < currentItem.jsonObject.item.length; i++){
let subItemJsonObject = currentItem.jsonObject.item[i];
let subItemDirectory = `${currentItem.directory}/${currentItem.fileName}`
let subItemFileName = `${slug(subItemJsonObject["name"]).toLowerCase()}`;
let filePath = `${subItemDirectory}/${subItemFileName}`
if (filePath in visitedNames) {
subItemFileName = `${subItemFileName}-${visitedNames[filePath]}`;
} else {
visitedNames[filePath] = 1;
}
visitedNames[filePath]++;
let subItem = {
jsonObject: subItemJsonObject,
directory: subItemDirectory,
fileName: subItemFileName,
depth: currentItem.depth+1
}
itemQueue.push(subItem);
let replacementReference = {"$ref": `#/${currentItem.fileName}/${subItem.fileName}.json`}
currentItem.jsonObject.item[i] = replacementReference;
}
}
if (!fs.existsSync(currentItem.directory)){
fs.mkdirSync(currentItem.directory);
}
let filePath = `${currentItem.directory}/${currentItem.fileName}.json`
if (filePath in visitedNames) {
filePath = `${currentItem.directory}/${currentItem.fileName}-${visitedNames[filePath]}.json`;
} else {
visitedNames[filePath] = 1;
}
visitedNames[filePath]++;
fs.writeFileSync(filePath, JSON.stringify(currentItem.jsonObject, null, "\t"));
}
}
}