-
Notifications
You must be signed in to change notification settings - Fork 4
/
merge.js
executable file
·103 lines (92 loc) · 2.82 KB
/
merge.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
/**
* Script that merges one or more JSKOS files in ndjson format.
*
* See `./merge.js --help` for usage info.
*/
let args = process.argv.slice(2)
function printUsage() {
console.warn("usage: ./merge.js [--help] [--append] file1 [file2 ...]")
console.warn()
}
if (args.includes("-h") || args.includes("--help")) {
printUsage()
console.warn("Merges JSKOS files (ndjson) by URI.")
console.warn("Prints resulting ndjson data to stdout, messages to stderr (see examples below).")
console.warn()
console.warn("Options:")
console.warn(" --help Prints this help document")
console.warn(" --append Appends concepts that are not in the first provided file (skips by default)")
console.warn()
console.warn("Examples:")
console.warn(" ./merge.js file-en.ndjson file-de.ndjson > file-merged.ndjson")
process.exit(0)
}
let append = false
if (args.includes("--append")) {
append = true
args = args.filter(arg => arg !== "--append")
}
let option
while (option = args.find(arg => arg.startsWith("--"))) {
console.warn(`Unknow option: ${option}`)
args = args.filter(arg => arg !== option)
}
if (args.length < 1) {
console.warn("Need to provide JSKOS files in ndjson format.")
printUsage()
process.exit(1)
}
import fs from "node:fs"
import readline from "node:readline"
import jskos from "jskos-tools"
function getReadlineInterface(file) {
return readline.createInterface({
input: fs.createReadStream(file),
crlfDelay: Infinity
})
}
const concepts = {}
function mergeIntoConcepts(concept) {
const uri = concept.uri
if (!concepts[uri]) {
return false
}
concepts[uri] = jskos.merge(concepts[uri], concept)
return true
}
main()
async function main() {
const mainFile = args[0]
args = args.slice(1)
let mergedCount = 0
for await (const line of getReadlineInterface(mainFile)) {
try {
const concept = JSON.parse(line)
if (!mergeIntoConcepts(concept)) {
concepts[concept.uri] = concept
} else {
mergedCount += 1
}
} catch (error) {
// Ignore for now
}
}
console.warn(`- Read ${Object.keys(concepts).length} concepts from ${mainFile} (${mergedCount} were already merged).`)
for (const file of args) {
console.warn(`- Merging with ${file}...`)
let mergedCount = 0
let skippedOrAddedCount = 0
for await (const line of getReadlineInterface(file)) {
const concept = JSON.parse(line)
const merged = mergeIntoConcepts(concept)
mergedCount += merged ? 1 : 0
skippedOrAddedCount += merged ? 0 : 1
if (!merged && append) {
concepts[concept.uri] = concept
}
}
console.warn(` ... ${mergedCount} concepts merged, ${skippedOrAddedCount} concepts ${append ? "added" : "skipped"}.`)
}
Object.values(concepts).forEach(concept => console.log(JSON.stringify(concept)))
}