-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
177 lines (137 loc) · 4.22 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
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
const { v4: uuidv4 } = require('uuid');
const fs = require('fs');
const ldes = require('./ldes_v1');
const YAML = require('yaml');
const prom = require('timers/promises');
const commander = require('commander');
commander
.version('1.0.0','-v, --version')
.argument('<scenario>')
.argument('[<directory>]')
.option('-b, --baseUrl <url>', 'Solid base url', 'http://localhost:3000')
.option('-c, --config <directory>', 'Configuration directory', __dirname + '/config')
.option('--ldes','Create LDES',false)
.parse(process.argv);
const options = commander.opts();
const baseUrl = options.baseUrl;
const agents = loadJsonFiles(options.config + '/agents');
const objects = loadJsonFiles(options.config + '/objects');
const sleep = t => new Promise(res => setTimeout(res, t))
const scenarioFile = commander.args[0]
const outputDir = commander.args[1];
const scenario = YAML.parseAllDocuments(fs.readFileSync(scenarioFile, 'utf8'));
doit();
async function doit() {
for (let s = 0 ; s < scenario.length ; s++) {
const scene = scenario[s];
const sceneJS = scene.toJS();
const notification = generateNotification(sceneJS);
if (fs.existsSync(outputDir)) {
let dirs = ['data','service'];
if (sceneJS['$']) {
dirs = sceneJS['$'] instanceof Array ? sceneJS['$'] : [sceneJS['$']];
}
for (let i = 0 ; i < dirs.length ; i++) {
const subdir = dirs[i];
generateDir(`${outputDir}/${subdir}`);
const id = notification['id'].replace(/:/g,'-');
await prom.setTimeout(500);
console.error(`waiting 500ms`);
console.error(`generating ${outputDir}/${subdir}/${id}.jsonld`);
fs.writeFileSync(`${outputDir}/${subdir}/${id}.jsonld`, JSON.stringify(notification,null,2));
}
}
else {
console.log(JSON.stringify(notification,null,2));
}
objects.push(notification);
}
}
if (fs.existsSync(outputDir) && options.ldes) {
console.error('creating ldes...');
ldes.generateLDES(outputDir,baseUrl);
}
function loadJsonFiles(path) {
const res = [];
const files = fs.readdirSync(path);
files.forEach(file => {
if (file.endsWith('.json')) {
res.push(JSON.parse(fs.readFileSync(path + '/' + file)));
}
});
return res;
}
function genid() {
return 'urn:uuid' + uuidv4();
}
function generateNotification(param) {
const notification = {
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://purl.org/coar/notify"
]
};
const actor = resolveAgent(param['actor']);
const origin = resolveAgent(param['origin']);
const target = resolveAgent(param['target']);
const object = resolveObject(param['object']);
const context = resolveObject(param['context']);
const inReplyTo = resolveNotification(param['inReplyTo']);
if (param['@context']) {
notification['@context'] = param['@context'];
}
if (param['id']) {
notification['id'] = param['id'];
}
if (param['type']) {
notification['type'] = param['type'];
}
if (actor) {
notification['actor'] = actor;
}
if (origin) {
notification['origin'] = origin;
}
if (context) {
notification['context'] = context;
}
if (inReplyTo) {
notification['inReplyTo'] = inReplyTo;
}
if (object) {
notification['object'] = object;
}
if (target) {
notification['target'] = target;
}
return notification;
}
function generateDir(dir) {
const subdir = dir.replace(/.*\//g,'');
if (! fs.existsSync(dir)) {
fs.mkdirSync(dir , { recursive: true });
}
}
function resolveAgent(id) {
if (!id) {
return undefined;
}
return agents.find( (elm) => {
return elm['id'] == id;
});
}
function resolveObject(id) {
if (!id) {
return undefined;
}
const res = objects.find( (elm) => {
return elm['id'] == id;
});
return res ? res : id ;
}
function resolveNotification(id) {
if (!id) {
return undefined;
}
return id;
}