-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
56 lines (47 loc) · 1.52 KB
/
index.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
var merge = require('ngraph.merge');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
module.exports = save;
function save(graph, options) {
options = merge(options, {
outDir: '.',
labels: 'labels.json',
meta: 'meta.json',
links: 'links.bin'
});
fixPaths();
var labels = require('./lib/getLabels.js')(graph);
saveLabels(labels);
var linksBuffer = require('./lib/getLinksBuffer.js')(graph, labels);
fs.writeFileSync(options.links, linksBuffer);
console.log(graph.getLinksCount() + ' links saved to ' + options.links);
saveMeta();
function fixPaths() {
if (!fs.existsSync(options.outDir)) {
mkdirp.sync(options.outDir);
}
options.labels = path.join(options.outDir, options.labels);
options.meta = path.join(options.outDir, options.meta);
options.links = path.join(options.outDir, options.links);
}
function saveMeta() {
var meta = getMetaInfo();
fs.writeFileSync(options.meta, JSON.stringify(meta), 'utf8');
console.log('Meta information saved to ' + options.meta);
}
function getMetaInfo() {
return {
date: +new Date(),
nodeCount: graph.getNodesCount(),
linkCount: graph.getLinksCount(),
nodeFile: options.labels,
linkFile: options.links,
version: require(path.join(__dirname, 'package.json')).version
};
}
function saveLabels(labels) {
fs.writeFileSync(options.labels, JSON.stringify(labels), 'utf8');
console.log(labels.length + ' ids saved to ' + options.labels);
}
}