forked from tl-its-umich-edu/ahanca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ahanca.js
executable file
·82 lines (65 loc) · 2.84 KB
/
ahanca.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
#!/usr/bin/env node
const fs = require('fs');
const _ = require('lodash');
const jq = require('node-jq');
const argumentsRequired = {
specFrom: 'Specification version to be converted from (e.g., "caliper-1p0")',
specTo: 'Specification version to be converted to (e.g., "caliper-1p1")',
inputFilename: 'Name of the input file',
};
const argumentsRequiredNum = Object.keys(argumentsRequired).length;
const argumentsRequiredError = 'Error: Exactly ' + argumentsRequiredNum + ' arguments are required';
const argumentParser = require('yargs')
.usage('Usage: $0 [options] ' + '<' + Object.keys(argumentsRequired).join('> <') + '>');
for (var argument in argumentsRequired) {
if (argumentsRequired.hasOwnProperty(argument)) {
console.log(argument + " -> " + argumentsRequired[argument]);
argumentParser.command(argument, argumentsRequired[argument]);
}
}
var arguments = argumentParser
.demandCommand(argumentsRequiredNum, argumentsRequiredNum,
argumentsRequiredError, argumentsRequiredError)
.argv._;
var specFrom, specTo, inputFilename;
[specFrom, specTo, inputFilename] = arguments.slice(0, argumentsRequiredNum);
console.log('Starting ahanca...');
console.log('Converting from: "%s". Converting to: "%s"', specFrom, specTo);
// Problem: `include "__";` at the beginning of filters causes errors. node-jq serializes
// input objects to JSON and concatenates the filter to it as "<input> | <filter>". jq
// allows "include" directives only at the beginning of filters. This concatenation puts
// the "include" in the middle of the filter instead.
// Workaround: Remove the "include" directive from the filter. Read the contents of the "__.jq"
// module file into a constant, then prepend it to filters.
const filterPreamble = fs.readFileSync('xor.jq', 'utf8');
const jqOptions = {input: 'json'};
var filter =
'"' + specFrom + '" as $specFrom | ' +
'"' + specTo + '" as $specTo | ' +
'map(. | select(.mapping[].spec | xor(contains([$specFrom]); contains([$specTo]))) | ' +
'{term, mapping: [.mapping[] | select(.spec | (contains([$specFrom]) or contains([$specTo])))]})';
var mapping = JSON.parse(fs.readFileSync('mapping.json', 'utf8'));
// var terms = _(mapping).map(function (termMap) {
// return termMap.term
// }).value()
var terms = _(mapping)
.filter(function (t) {
return t.domain.includes('ahanca:root');
})
.map(function (termMap) {
return termMap.term
})
.value();
console.dir(terms, {'depth': 9999});
var specs = _(mapping)
.filter(function (t) {
return t.domain.includes('ahanca:root');
})
.map(function (termMap) {
return _.map(termMap, 'spec');
})
.value();
console.dir(specs, {'depth': 9999});
process.exit(999);
jq.run(filterPreamble + filter, mapping, jqOptions).then(console.log);
console.log('Ending ahanca...');