Skip to content
Permalink
Newer
Older
100755 97 lines (88 sloc) 3.21 KB
1
#!/usr/bin/env node
2
/*! @license MIT ©2014–2016 Ruben Verborgh, Ghent University – imec */
3
/* Merges the queries from the 'queries' folder into 'settings.json'. */
4
5
var fs = require('fs'),
6
path = require('path');
7
8
var queryDir = path.join(__dirname, 'queries'),
9
settingsFile = path.join(__dirname, 'settings.json'),
10
outputFile = path.join(__dirname, 'queries.json');
12
// Write the settings with queries to the output file
13
var settings = JSON.parse(fs.readFileSync(settingsFile, { encoding: 'utf8' }));
14
settings.queries = readQueryDirectory(queryDir)
15
// Sort by ascending priority and name
16
.sort(function (a, b) {
17
return (a.priority - b.priority) ||
18
(a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1);
19
})
20
// Remove priority numbers in output
21
.map(function (d) { return delete d.priority, d; });
22
fs.writeFileSync(outputFile, JSON.stringify(settings));
23
24
// Recursively reads queries in the given directory
25
function readQueryDirectory(directory) {
26
return [].concat.apply([], fs.readdirSync(directory).map(function (filename) {
27
filename = path.join(directory, filename);
28
if (fs.statSync(filename).isDirectory())
29
return readQueryDirectory(filename);
30
else if (/\.(sparql)|(graphql)$/.test(filename))
31
return readQuery(filename);
37
// Creates a query from the file
38
function readQuery(queryFile) {
39
// Construct the initial query
40
var query = {
41
name: queryFile.replace(/^.+\/|\.\w+$/g, ''),
42
priority: 1000, datasources: []
43
};
45
// Parse query comments for metadata
46
var inputLines = fs.readFileSync(queryFile, {encoding: 'utf8'}).split('\n'), outputLines = [];
47
inputLines.forEach(function (line, index) {
48
// The line might be a key/value pair
49
var keyValue = line.match(/^#\s*(\w+)\s*:\s*(.*)\s*/);
50
if (keyValue) {
51
var key = keyValue[1].toLowerCase(), value = keyValue[2];
52
switch (key) {
53
case 'datasource':
54
case 'datasources':
55
value.split(/\s+/)
56
.forEach(function (d) {
57
if (d.length) query.datasources.push(d);
58
});
59
return;
Sep 11, 2014
60
}
62
// The first line might contain a name
63
if (index === 0 && line[0] === '#') {
64
query.name = line.replace(/#\s*/, '');
65
// A number preceding the name indicates the priority
66
if (/^\d/.test(query.name)) {
67
query.priority = parseInt(query.name, 10);
68
query.name = query.name.replace(/^\d+\W\s*/, '');
69
}
70
}
71
// Any other line is part of the query
72
else {
73
outputLines.push(line);
74
}
Sep 11, 2014
75
});
76
77
// Depending on the file name, assume a different query format
78
if (queryFile.endsWith('graphql')) {
79
var emptyLineIndex = outputLines.indexOf('');
80
if (emptyLineIndex < 0)
81
throw new Error(queryFile + ' does not have an empty line between GraphQL query and JSON-LD context');
82
query.queryFormat = 'graphql';
83
query.query = outputLines.slice(0, emptyLineIndex).join('\n').trim();
84
query.context = outputLines.slice(emptyLineIndex + 1).join('\n').trim();
85
}
86
else {
87
query.queryFormat = 'sparql';
88
query.query = outputLines.join('\n').trim();
89
}
90
91
// Skip disabled query formats
92
if (!settings.queryFormats[query.queryFormat]) {
93
return [];
94
}
95
96
return query;