-
Notifications
You must be signed in to change notification settings - Fork 1
/
serve
291 lines (269 loc) · 9.48 KB
/
serve
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env node
var fs = require("fs"),
ncp = require('ncp').ncp,
path = require('path'),
jade = require('jade'),
n3 = require('n3'),
owl2html = require('./lib/owl2html.js'),
async = require('async'),
ArgumentParser = require('argparse').ArgumentParser,
rdfExt = require('rdf-ext'),
exec = require('child_process').exec,
N3Parser = require('rdf-parser-n3'),
XMLSerializer = require('rdf-serializer-xml');
var packageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json')));
var parser = new ArgumentParser({
version: packageJson.version,
addHelp: true,
description: packageJson.description
});
parser.addArgument(
['-o', '--ontology'],
{
help: 'label of the turtle file you want to serve, as specified in the config file. ' +
'If provided, the version can also be added (e.g., "apps4X:0.4", if so, the turtle file will also be published under apps4X/0.4/. ' +
'The name of the turtle CANNOT be `assets` (as all site assets reside in the `assets`-folder)'
}
);
parser.addArgument(
['-a', '--all'],
{
help: '(re-)serve all turtle files in the config file',
action: 'storeTrue'
}
);
parser.addArgument(
['-i', '--index'],
{
help: '(re-)render the index.html file. Don\'t do this if you have your own index.html file!\n' +
'PS: this actually tries to be smart, and links to all subfolders that have a .htaccess file in them, so is not necessarily depending on config.files',
action: 'storeTrue'
}
);
/**
* Configuration
*/
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
if (!config.files) {
console.error("Please check your config file. It should contain turtle files");
process.exit();
}
var files = config.files;
if (!config.outpath) {
console.error("Please check your config file. It should contain an output path");
process.exit();
}
var outpath = config.outpath;
if (!(path.isAbsolute(outpath))) {
outpath = path.resolve(__dirname, outpath);
}
if (!(fs.existsSync(outpath))) {
console.error("Outpath does not exists, probably you should create a folder at " + outpath + " and copy the assets from /public there");
outpath = path.resolve(__dirname, outpath);
}
var args = parser.parseArgs();
// intitial copy-pasting of the HTML assets
if (!fs.existsSync(path.resolve(outpath, 'assets'))) {
console.log('First time copying of the html assets...');
fs.mkdir(path.resolve(outpath, 'assets'));
ncp(path.resolve(__dirname, './public/'), path.resolve(outpath, 'assets'), function (err) {
if (err) {
throw err;
}
console.log('Copying done!');
assetsReady();
});
}
else {
assetsReady();
}
/*****************************************************************************/
function assetsReady() {
var oPath;
if (args.all) {
async.forEachOf(files, function (file, key, done) {
var oPath = file;
if (!path.isAbsolute(oPath)) {
oPath = path.resolve(__dirname, oPath);
}
serve(key, oPath, null, done);
}, afterServe);
}
else if (args.ontology) {
var name = args.ontology.split(':');
var version = name.length > 1 ? name[1] : null;
name = name[0];
oPath = files[name];
if (!path.isAbsolute(oPath)) {
oPath = path.resolve(__dirname, oPath);
}
serve(name, oPath, version, afterServe);
}
else {
afterServe();
}
function afterServe() {
if (args.index) {
renderIndex();
}
}
}
/**
* Serve!
* @param name
* @param file
* @param version
* @param cb
*/
function serve(name, file, version, cb) {
version = version || null;
if (!fs.existsSync(path.resolve(outpath, name))) {
fs.mkdirSync(path.resolve(outpath, name));
}
if (version && !fs.existsSync(path.resolve(outpath, name, version))) {
fs.mkdirSync(path.resolve(outpath, name, version));
}
async.parallel([
function (done) {
// TTL
var oPath = path.resolve(outpath, name, name + '.ttl');
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
done(err);
}
fs.writeFile(oPath, data, function () {
if (version) {
fs.writeFile(path.resolve(outpath, name, version, name + '.ttl'), data, done);
}
else {
done();
}
});
});
},
function (done) {
// OWL
var oPath = path.resolve(outpath, name, name + '.owl');
rdfExt.parsers = rdfExt.parsers || new rdfExt.Parsers();
rdfExt.serializers = rdfExt.serializers || new rdfExt.Serializers();
rdfExt.parsers['text/turtle'] = N3Parser;
rdfExt.serializers['application/rdf+xml'] = XMLSerializer;
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
return done(err);
}
rdfExt.parsers.parse('text/turtle', data).then(function (graph) {
rdfExt.serializers.serialize('application/rdf+xml', graph).then(function (string) {
fs.writeFile(oPath, string, function () {
if (version) {
fs.writeFile(path.resolve(outpath, name, version, name + '.owl'), string, done);
}
else {
done();
}
});
}).catch(function (err) {
done(err);
});
}).catch(function (err) {
done(err);
});
});
},
function (done) {
// HTML
getTriples(file, function (triples) {
var oPath = path.resolve(outpath, name, name + '.html');
var html = owl2html(triples);
fs.writeFile(oPath, html, function () {
if (version) {
fs.writeFile(path.resolve(outpath, name, version, name + '.html'), html.replace(/\.\.\/assets\//g, '../../assets/'), done);
}
else {
done();
}
});
});
},
function (done) {
// JSON
getTriples(file, function (triples) {
var oPath = path.resolve(outpath, name, name + '.json');
fs.writeFile(oPath, JSON.stringify(triples, null, ' '), function () {
if (version) {
fs.writeFile(path.resolve(outpath, name, version, name + '.json'), JSON.stringify(triples, null, ' '), done);
}
else {
done();
}
});
});
}
], function (err, results) {
fs.readFile(path.resolve(__dirname, './lib/.htaccess.template'), 'utf8', function (err, htAccess) {
htAccess = htAccess.replace(/%%NAME%%/g, name);
fs.writeFile(path.resolve(outpath, name, '.htaccess'), htAccess.replace(/%%BASE%%/g, config.namespaceBase + name), function () {
if (version) {
fs.writeFile(path.resolve(outpath, name, version, '.htaccess'), htAccess.replace(/%%BASE%%/g, config.namespaceBase + name + '/' + version), cb);
}
else {
cb();
}
});
});
});
}
/**
* Check whether the index is still up to date
*/
function renderIndex() {
var paths = [];
fs.readdir(outpath, function (err, files) {
async.each(files, function (file, done) {
fs.stat(path.resolve(outpath, file), function (err, stat) {
if (!stat.isDirectory()) {
return done();
}
fs.stat(path.resolve(outpath, file, '.htaccess'), function (err, stats) {
if (err) {
if (err.code == 'ENOENT') {
return done();
}
throw err;
}
if (stats.isFile()) {
paths.push({
path: './' + file + '/',
name: file
});
}
done();
});
});
}, function () {
var jadeTemplate = fs.readFileSync(__dirname + '/lib/index.jd', "utf8");
var j = jade.compile(jadeTemplate, {pretty: true});
var html = j({
pageTitle: "Ontologies hosted by Data Science Lab",
description: "Ontologies hosted by Data Science Lab",
paths: paths,
currentYear: (new Date()).getFullYear()
});
fs.writeFile(path.resolve(outpath, 'index.html'), html);
});
});
}
/**
* Get all triples
*/
function getTriples(file, callback) {
var turtleStream = fs.createReadStream(file, 'utf8');
var parser = n3.StreamParser();
var triples = [];
turtleStream.pipe(parser);
parser.on("data", function (triple) {
triples.push(triple);
}).on("end", function () {
callback(triples);
});
}