-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (60 loc) · 1.69 KB
/
index.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
import { safeLoad } from 'js-yaml'
import { readFileSync } from 'fs'
import memdown from 'memdown'
import { RdfStore } from 'quadstore'
const ParserJsonld = require('@rdfjs/parser-jsonld')
const namespace = require('@rdfjs/namespace')
import { Readable, finished } from 'stream'
import { promisify } from 'util'
const done = promisify(finished)
const meow = require('meow')
import { track, trace } from '@valueflows/track-trace'
import { ResultNode } from '@valueflows/track-trace'
const cli = meow('', {
flags: {
track: {
type: 'string',
alias: 't'
}
}
})
// import data from YAML file to quadstore
const data = safeLoad(readFileSync(cli.input[0], 'utf8'))
const store = new RdfStore(memdown())
const input = new Readable({
read: () => {
input.push(JSON.stringify(data))
input.push(null)
}
})
const parserJsonld = new ParserJsonld()
const output = parserJsonld.import(input)
function pad (string :string, count :number) {
let prefix = ''
for (let i = 0; i < count; i++) {
prefix += ' '
}
return prefix + string
}
const vf = namespace('https://w3id.org/valueflows#')
function log (node :ResultNode) {
let emoji = '⛔'
if (node.type.equals(vf('EconomicResource'))) emoji = '📦'
if (node.type.equals(vf('EconomicEvent'))) emoji = '🔹'
if (node.type.equals(vf('Process'))) emoji = '🌀'
console.log(pad(`${emoji} `, node.distance), node.iri)
}
;(async () => {
await done(store.import(output))
console.log('data imported to store')
if (cli.flags.track) {
for await (const node of track(store, cli.flags.track)) {
log(node)
}
}
if (cli.flags.trace) {
for await (const node of trace(store, cli.flags.trace)) {
log(node)
}
}
})()