-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge.ts
74 lines (58 loc) · 1.82 KB
/
merge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
import fs from "fs";
import FileItem from "./interfaces/FileItem";
import Info from "./interfaces/Info";
import { influcePostmanJsonFile } from "./helper";
const inDir: string = process.argv[2];
const outFile: string = process.argv[3];
class ExtendedFileItem implements FileItem {
public name: string;
public predecessor: string;
public item: any;
public constructor(fileItem: FileItem) {
this.predecessor = fileItem.predecessor;
this.item = fileItem.item;
}
}
if (process.argv.length !== 4) {
console.error("Please specify all command line arguments:");
console.error("postbox-merge ./outdir path/to/postman_collection.json");
process.exit(1);
}
const files: string[] = fs.readdirSync(inDir);
var items: ExtendedFileItem[] = [];
// read all files into main memory
files
.filter((file: string) => {
// remove interal files, those start with two underscores
return influcePostmanJsonFile(file);
})
.forEach((file: string) => {
// read the file content
const fileItem: FileItem = JSON.parse(fs.readFileSync(`${inDir}/${file}`).toString());
let item = new ExtendedFileItem(fileItem);
// store the filename
item.name = file.replace(".json", "");
console.log("Merged:", item.name);
items.push(item);
});
var sortedItems: ExtendedFileItem[] = [];
let currentSearchItem = null;
for (let item of items) {
for (let searchItem of items) {
if (searchItem.predecessor === currentSearchItem) {
sortedItems.push(searchItem);
currentSearchItem = searchItem.name;
}
}
}
// read info portion of the collection
const info: Info = JSON.parse(fs.readFileSync(`${inDir}/__info.json`).toString());
// reassemble postman collection file
const postmanCol = {
info,
item: sortedItems.map((item: ExtendedFileItem) => {
return item.item;
})
};
fs.writeFileSync(outFile, JSON.stringify(postmanCol, null, 4));