postman-json-chopper
Version:
Splits up large postman collection json file into folders and smaller files and merges them back together
91 lines (77 loc) • 2.37 kB
text/typescript
// main.ts
// Import the file system module
import * as fs from "fs";
import * as path from "path";
import { split } from "./split";
import { merge } from "./merge";
interface ChopperConfig {
items: ChopperConfigItem[];
}
interface ChopperConfigItem {
mergedFile: string;
splitFile: string;
maxDepth: number;
}
function readConfigFile(configFilePath: string): ChopperConfig | null {
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: string;
let output: string;
let maxDepthArg: string;
let maxDepth: number;
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: string, input: string, output: string, maxDepth: number){
switch (action) {
case "split":
split(input, output, maxDepth);
break;
case "merge":
merge(input, output);
break;
default:
console.error("Error: Invalid action. Use 'split' or 'merge'");
process.exit(1);
}
}
main();