postman-json-chopper
Version:
Splits up large postman collection json file into folders and smaller files and merges them back together
97 lines (96 loc) • 3.5 kB
JavaScript
// main.ts
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
// Import the file system module
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const split_1 = require("./split");
const merge_1 = require("./merge");
function readConfigFile(configFilePath) {
if (fs.existsSync(configFilePath)) {
const configFileContent = fs.readFileSync(configFilePath, "utf-8");
return JSON.parse(configFileContent);
}
return null;
}
// Main function to process the command line arguments
function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.error("Error: Invalid number of arguments. Usage: node main.js [split|merge] <input> <output>");
process.exit(1);
}
const action = args[0];
let input;
let output;
let maxDepthArg;
let maxDepth;
if (args.length === 1 || args.length === 2) {
let configFilePath = "postman-json-chopper.json";
if (args.length === 2) {
configFilePath = args[1];
let directory = path.parse(configFilePath).dir;
process.chdir(directory);
}
const config = readConfigFile(configFilePath);
if (config === null) {
console.error("Error: postman-json-chopper.json file not found");
process.exit(1);
}
let items = config.items;
items.forEach(item => {
input = action === "split" ? item.mergedFile : item.splitFile;
output = action === "split" ? item.splitFile : item.mergedFile;
maxDepth = item.maxDepth;
performAction(action, input, output, maxDepth);
});
}
else if (args.length >= 3) {
input = args[1];
output = args[2];
maxDepthArg = args[3];
performAction(action, input, output, maxDepth);
}
else {
console.error("Error: Invalid number of arguments. Usage: node main.js [split|merge] <input> <output>");
process.exit(1);
}
}
function performAction(action, input, output, maxDepth) {
switch (action) {
case "split":
(0, split_1.split)(input, output, maxDepth);
break;
case "merge":
(0, merge_1.merge)(input, output);
break;
default:
console.error("Error: Invalid action. Use 'split' or 'merge'");
process.exit(1);
}
}
main();
;