@makano/rew 1.1.81 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/coffeescript/browser.js +144 -139
- package/lib/coffeescript/cake.js +132 -133
- package/lib/coffeescript/coffeescript.js +437 -381
- package/lib/coffeescript/command.js +806 -724
- package/lib/coffeescript/grammar.js +1908 -2474
- package/lib/coffeescript/helpers.js +509 -473
- package/lib/coffeescript/index.js +228 -215
- package/lib/coffeescript/lexer.js +2282 -1909
- package/lib/coffeescript/nodes.js +9782 -9202
- package/lib/coffeescript/optparse.js +255 -227
- package/lib/coffeescript/parser.js +20305 -1265
- package/lib/coffeescript/register.js +107 -87
- package/lib/coffeescript/repl.js +307 -284
- package/lib/coffeescript/rewriter.js +1389 -1079
- package/lib/coffeescript/scope.js +176 -172
- package/lib/coffeescript/sourcemap.js +242 -227
- package/lib/rew/cli/cli.js +312 -239
- package/lib/rew/cli/log.js +27 -27
- package/lib/rew/cli/run.js +8 -8
- package/lib/rew/cli/utils.js +368 -199
- package/lib/rew/const/config_path.js +2 -2
- package/lib/rew/const/default.js +38 -53
- package/lib/rew/const/files.js +11 -14
- package/lib/rew/const/opt.js +6 -6
- package/lib/rew/css/theme.css +1 -1
- package/lib/rew/functions/core.js +55 -55
- package/lib/rew/functions/curl.js +23 -0
- package/lib/rew/functions/emitter.js +52 -55
- package/lib/rew/functions/exec.js +25 -25
- package/lib/rew/functions/export.js +17 -17
- package/lib/rew/functions/fs.js +57 -59
- package/lib/rew/functions/future.js +29 -21
- package/lib/rew/functions/id.js +8 -9
- package/lib/rew/functions/import.js +106 -122
- package/lib/rew/functions/map.js +10 -10
- package/lib/rew/functions/match.js +35 -42
- package/lib/rew/functions/path.js +8 -8
- package/lib/rew/functions/require.js +32 -36
- package/lib/rew/functions/sleep.js +2 -2
- package/lib/rew/functions/stdout.js +18 -18
- package/lib/rew/functions/types.js +82 -106
- package/lib/rew/html/ui.html +12 -12
- package/lib/rew/html/ui.js +196 -201
- package/lib/rew/main.js +14 -14
- package/lib/rew/misc/bin.js +37 -0
- package/lib/rew/misc/findAppInfo.js +13 -13
- package/lib/rew/misc/findAppPath.js +15 -15
- package/lib/rew/misc/req.js +7 -0
- package/lib/rew/misc/seededid.js +8 -8
- package/lib/rew/models/enum.js +12 -12
- package/lib/rew/models/struct.js +30 -32
- package/lib/rew/modules/compiler.js +237 -209
- package/lib/rew/modules/fs.js +10 -10
- package/lib/rew/modules/runtime.js +17 -21
- package/lib/rew/modules/yaml.js +27 -30
- package/lib/rew/pkgs/conf.js +82 -92
- package/lib/rew/pkgs/data.js +10 -10
- package/lib/rew/pkgs/date.js +27 -27
- package/lib/rew/pkgs/env.js +5 -5
- package/lib/rew/pkgs/modules/data/bintree.js +51 -51
- package/lib/rew/pkgs/modules/data/doublylinked.js +84 -84
- package/lib/rew/pkgs/modules/data/linkedList.js +72 -72
- package/lib/rew/pkgs/modules/data/queue.js +18 -18
- package/lib/rew/pkgs/modules/data/stack.js +18 -18
- package/lib/rew/pkgs/modules/threads/worker.js +36 -36
- package/lib/rew/pkgs/modules/ui/classes.js +181 -184
- package/lib/rew/pkgs/pkgs.js +9 -9
- package/lib/rew/pkgs/rune.js +373 -410
- package/lib/rew/pkgs/threads.js +62 -66
- package/lib/rew/pkgs/ui.js +148 -160
- package/lib/rew/qrew/compile.js +12 -0
- package/package.json +4 -3
package/lib/rew/pkgs/rune.js
CHANGED
@@ -1,17 +1,15 @@
|
|
1
|
-
const fs = require(
|
2
|
-
const { v4: uuidv4 } = require(
|
3
|
-
const path = require(
|
4
|
-
const
|
5
|
-
const
|
6
|
-
const { CONFIG_PATH } = require("../const/config_path");
|
1
|
+
const fs = require('fs');
|
2
|
+
const { v4: uuidv4 } = require('uuid');
|
3
|
+
const path = require('path');
|
4
|
+
const { CONFIG_PATH } = require('../const/config_path');
|
5
|
+
const { serializeData, deserializeData, gen_key } = require('../misc/bin');
|
7
6
|
|
8
|
-
const ENCRYPTION_KEY =
|
9
|
-
"e6ad8b0792b9e0472ea44d1f3adfd1d503182efcce25991b05cc5ef83f307ffc";
|
7
|
+
const ENCRYPTION_KEY = 'e6ad8b0792b9e0472ea44d1f3adfd1d503182efcce25991b05cc5ef83f307ffc';
|
10
8
|
|
11
9
|
class Change {
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
constructor(values) {
|
11
|
+
this.values = values;
|
12
|
+
}
|
15
13
|
}
|
16
14
|
|
17
15
|
class PopChange extends Change {}
|
@@ -21,417 +19,382 @@ class PushChange extends Change {}
|
|
21
19
|
const runePush = (...values) => new PushChange(values);
|
22
20
|
const runePop = (...values) => new PopChange(values);
|
23
21
|
|
24
|
-
function makeRef(value, props =
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
function makeRef(value, props = '') {
|
23
|
+
if (!value['@rune.id']) return null;
|
24
|
+
const collection = getCollectionFromID(value['@rune.id']);
|
25
|
+
const ref = collection + '.' + value['@rune.id'];
|
26
|
+
return '@rune.ref ' + ref + props;
|
29
27
|
}
|
30
28
|
|
31
29
|
const eid = (s, diff) =>
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
30
|
+
s
|
31
|
+
.split('')
|
32
|
+
.map((i) => {
|
33
|
+
let charCode = i.charCodeAt(0) + diff;
|
34
|
+
if (charCode > 122) {
|
35
|
+
charCode -= 26;
|
36
|
+
}
|
37
|
+
return String.fromCharCode(charCode);
|
38
|
+
})
|
39
|
+
.join('');
|
42
40
|
|
43
41
|
function generateID(id, collection) {
|
44
|
-
|
42
|
+
return eid(collection, 5) + '+' + id;
|
45
43
|
}
|
46
44
|
|
47
45
|
function getCollectionFromID(id) {
|
48
|
-
|
46
|
+
return eid(id.split('+')[0], -5);
|
49
47
|
}
|
50
48
|
|
51
49
|
const createDB = (dbName, dirname, dbData = {}, encryptionKey) => {
|
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
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
const get = (key) => {
|
383
|
-
if (!fs.existsSync(mapFilePath)) return null;
|
384
|
-
const data = readDataFile(mapFilePath);
|
385
|
-
return data[key] || null;
|
386
|
-
};
|
387
|
-
|
388
|
-
const remove = (key) => {
|
389
|
-
if (!fs.existsSync(mapFilePath)) return false;
|
390
|
-
const data = readDataFile(mapFilePath);
|
391
|
-
if (data[key]) {
|
392
|
-
delete data[key];
|
393
|
-
writeDataFile(mapFilePath, data);
|
394
|
-
return true;
|
395
|
-
}
|
396
|
-
return false;
|
397
|
-
};
|
398
|
-
|
399
|
-
const transform = (cb, mutate = false) => {
|
400
|
-
const data = readDataFile(mapFilePath);
|
401
|
-
const transformedData = cb(data);
|
402
|
-
if (mutate) {
|
403
|
-
writeDataFile(mapFilePath, transformedData);
|
404
|
-
}
|
405
|
-
return transformedData;
|
406
|
-
};
|
407
|
-
|
408
|
-
const list = () => {
|
409
|
-
if (!fs.existsSync(mapFilePath)) return {};
|
410
|
-
const data = readDataFile(mapFilePath);
|
411
|
-
return data;
|
412
|
-
};
|
413
|
-
|
414
|
-
return { set, get, remove, list, transform };
|
415
|
-
};
|
416
|
-
|
417
|
-
readMainData();
|
418
|
-
|
419
|
-
return { setData, getData, collection, findRef, makeRef, map };
|
50
|
+
const dbDirPath = path.join(dirname, dbName);
|
51
|
+
const mainFilePath = path.join(dbDirPath, 'main.bin');
|
52
|
+
|
53
|
+
if (!fs.existsSync(dbDirPath)) {
|
54
|
+
fs.mkdirSync(dbDirPath);
|
55
|
+
}
|
56
|
+
|
57
|
+
const getData = () => {
|
58
|
+
return readMainData().data;
|
59
|
+
};
|
60
|
+
getData.key = (key) => {
|
61
|
+
return getData()[key];
|
62
|
+
};
|
63
|
+
|
64
|
+
const setData = (data) => {
|
65
|
+
const newData = readMainData();
|
66
|
+
for (let i in data) if (data[i] !== undefined) newData.data[i] = data[i];
|
67
|
+
writeMainData(newData);
|
68
|
+
};
|
69
|
+
|
70
|
+
setData.key = (key, value) => {
|
71
|
+
setData({ [key]: value });
|
72
|
+
};
|
73
|
+
|
74
|
+
setData.rm = (key) => {
|
75
|
+
setData({ [key]: undefined });
|
76
|
+
};
|
77
|
+
|
78
|
+
setData.reset = () => {
|
79
|
+
writeMainData({
|
80
|
+
...readMainData(),
|
81
|
+
data: { ...dbData, name: dbName },
|
82
|
+
});
|
83
|
+
};
|
84
|
+
|
85
|
+
const readMainData = () => {
|
86
|
+
if (!fs.existsSync(mainFilePath)) {
|
87
|
+
writeMainData({
|
88
|
+
collections: [],
|
89
|
+
data: { ...dbData, name: dbName },
|
90
|
+
maps: [],
|
91
|
+
});
|
92
|
+
}
|
93
|
+
const buffer = fs.readFileSync(mainFilePath);
|
94
|
+
return deserializeData(buffer, encryptionKey);
|
95
|
+
};
|
96
|
+
|
97
|
+
const writeMainData = (data) => {
|
98
|
+
const buffer = serializeData(data, encryptionKey);
|
99
|
+
fs.writeFileSync(mainFilePath, buffer);
|
100
|
+
};
|
101
|
+
|
102
|
+
const readDataFile = (filePath) => {
|
103
|
+
const buffer = fs.readFileSync(filePath);
|
104
|
+
return deserializeData(buffer, encryptionKey);
|
105
|
+
};
|
106
|
+
|
107
|
+
const writeDataFile = (filePath, data) => {
|
108
|
+
const buffer = serializeData(data, encryptionKey);
|
109
|
+
fs.writeFileSync(filePath, buffer);
|
110
|
+
};
|
111
|
+
|
112
|
+
const collection = (collectionName) => {
|
113
|
+
const collectionFilePath = path.join(dbDirPath, `${collectionName}.col`);
|
114
|
+
|
115
|
+
const insert = (record) => {
|
116
|
+
const mainData = readMainData();
|
117
|
+
if (!mainData.collections.includes(collectionName)) {
|
118
|
+
mainData.collections.push(collectionName);
|
119
|
+
writeMainData(mainData);
|
120
|
+
}
|
121
|
+
|
122
|
+
let data = [];
|
123
|
+
if (fs.existsSync(collectionFilePath)) {
|
124
|
+
data = readDataFile(collectionFilePath);
|
125
|
+
}
|
126
|
+
const id = uuidv4();
|
127
|
+
record['@rune.id'] = generateID(id, collectionName);
|
128
|
+
data.push(record);
|
129
|
+
writeDataFile(collectionFilePath, data);
|
130
|
+
return record;
|
131
|
+
};
|
132
|
+
|
133
|
+
const read = (id, evaluate = true) => {
|
134
|
+
if (typeof id == 'object' && '@rune.id' in id) id = id['@rune.id'];
|
135
|
+
if (!fs.existsSync(collectionFilePath)) return null;
|
136
|
+
const data = readDataFile(collectionFilePath);
|
137
|
+
const record = data.find((record) => record['@rune.id'] === id);
|
138
|
+
if (record) {
|
139
|
+
return evaluateRecord(record);
|
140
|
+
}
|
141
|
+
return null;
|
142
|
+
};
|
143
|
+
|
144
|
+
const evaluateRecord = (record, prevRecord) => {
|
145
|
+
const evaluateValue = (val) => {
|
146
|
+
if (typeof val == 'string' && val.startsWith('@rune.ref')) {
|
147
|
+
const ref = val.split('@rune.ref')[1].trim();
|
148
|
+
const refData = findRef(ref, false);
|
149
|
+
if (!refData) {
|
150
|
+
return null;
|
151
|
+
} else {
|
152
|
+
let value = refData;
|
153
|
+
if (refData['@rune.id']) {
|
154
|
+
value = prevRecord && prevRecord['@rune.id'] == refData['@rune.id'] ? prevRecord : evaluateRecord(refData, record);
|
155
|
+
}
|
156
|
+
return value;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
if (Array.isArray(val)) {
|
160
|
+
val = val.map((i) => evaluateValue(i));
|
161
|
+
}
|
162
|
+
return val;
|
163
|
+
};
|
164
|
+
for (let i in record) {
|
165
|
+
const val = record[i];
|
166
|
+
record[i] = evaluateValue(val);
|
167
|
+
}
|
168
|
+
return record;
|
169
|
+
};
|
170
|
+
|
171
|
+
const update = (caseRecord, newRecord) => {
|
172
|
+
let id;
|
173
|
+
if (typeof caseRecord === 'string') {
|
174
|
+
id = caseRecord;
|
175
|
+
} else if (typeof caseRecord === 'object') {
|
176
|
+
const data = readDataFile(collectionFilePath);
|
177
|
+
const record = data.find((record) => {
|
178
|
+
for (const key in caseRecord) {
|
179
|
+
if (record[key] !== caseRecord[key]) return false;
|
180
|
+
}
|
181
|
+
return true;
|
182
|
+
});
|
183
|
+
if (record) {
|
184
|
+
id = record['@rune.id'];
|
185
|
+
} else {
|
186
|
+
return null; // No matching record found
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
if (!id) return null;
|
191
|
+
|
192
|
+
const data = readDataFile(collectionFilePath);
|
193
|
+
const index = data.findIndex((record) => record['@rune.id'] === id);
|
194
|
+
if (index !== -1) {
|
195
|
+
const oldRecord = data[index];
|
196
|
+
for (const key in newRecord) {
|
197
|
+
const value = newRecord[key];
|
198
|
+
if (value instanceof PushChange) {
|
199
|
+
if (!oldRecord[key] || !Array.isArray(oldRecord[key])) {
|
200
|
+
oldRecord[key] = [];
|
201
|
+
}
|
202
|
+
oldRecord[key].push(...value.values);
|
203
|
+
} else if (value instanceof PopChange) {
|
204
|
+
if (oldRecord[key] && Array.isArray(oldRecord[key])) {
|
205
|
+
value.values.forEach((val) => {
|
206
|
+
const index = oldRecord[key].indexOf(val);
|
207
|
+
if (index !== -1) {
|
208
|
+
oldRecord[key].splice(index, 1);
|
209
|
+
}
|
210
|
+
});
|
211
|
+
}
|
212
|
+
} else {
|
213
|
+
oldRecord[key] = value;
|
214
|
+
}
|
215
|
+
}
|
216
|
+
data[index] = oldRecord;
|
217
|
+
writeDataFile(collectionFilePath, data);
|
218
|
+
return data[index];
|
219
|
+
}
|
220
|
+
return null;
|
221
|
+
};
|
222
|
+
|
223
|
+
const find = (criteria) => {
|
224
|
+
if (typeof criteria == 'string') return read(criteria);
|
225
|
+
if (!criteria || typeof criteria !== 'object') return null;
|
226
|
+
|
227
|
+
const data = readDataFile(collectionFilePath);
|
228
|
+
const record =
|
229
|
+
data.find((record) => {
|
230
|
+
for (const key in criteria) {
|
231
|
+
if (record[key] !== criteria[key]) return false;
|
232
|
+
}
|
233
|
+
return true;
|
234
|
+
}) || null;
|
235
|
+
if (record) {
|
236
|
+
return evaluateRecord(record);
|
237
|
+
}
|
238
|
+
return null;
|
239
|
+
};
|
240
|
+
|
241
|
+
const remove = (id) => {
|
242
|
+
if ('@rune.id' in id) id = id['@rune.id'];
|
243
|
+
if (!fs.existsSync(collectionFilePath)) return false;
|
244
|
+
let data = readDataFile(collectionFilePath);
|
245
|
+
const index = data.findIndex((record) => record['@rune.id'] === id);
|
246
|
+
if (index !== -1) {
|
247
|
+
data.splice(index, 1);
|
248
|
+
writeDataFile(collectionFilePath, data);
|
249
|
+
return true;
|
250
|
+
}
|
251
|
+
return false;
|
252
|
+
};
|
253
|
+
|
254
|
+
const list = () => {
|
255
|
+
if (!fs.existsSync(collectionFilePath)) return [];
|
256
|
+
const data = readDataFile(collectionFilePath);
|
257
|
+
return data.map((rec) => evaluateRecord(rec));
|
258
|
+
};
|
259
|
+
|
260
|
+
const map = (cb, mutate = false) => {
|
261
|
+
const data = readDataFile(collectionFilePath);
|
262
|
+
const mappedData = data.map(cb);
|
263
|
+
if (mutate) {
|
264
|
+
writeDataFile(collectionFilePath, mappedData);
|
265
|
+
}
|
266
|
+
return mappedData;
|
267
|
+
};
|
268
|
+
|
269
|
+
const transform = (cb, mutate = true) => {
|
270
|
+
const data = readDataFile(collectionFilePath);
|
271
|
+
const transformedData = cb(data);
|
272
|
+
if (mutate) {
|
273
|
+
writeDataFile(collectionFilePath, transformedData);
|
274
|
+
}
|
275
|
+
return transformedData;
|
276
|
+
};
|
277
|
+
|
278
|
+
const filter = (cb, mutate = false) => {
|
279
|
+
const data = readDataFile(collectionFilePath);
|
280
|
+
const filteredData = data.filter(cb);
|
281
|
+
if (mutate) {
|
282
|
+
writeDataFile(collectionFilePath, filteredData);
|
283
|
+
}
|
284
|
+
return filteredData;
|
285
|
+
};
|
286
|
+
|
287
|
+
const sort = (cb, mutate = false) => {
|
288
|
+
const data = readDataFile(collectionFilePath);
|
289
|
+
const sortedData = data.sort(cb);
|
290
|
+
if (mutate) {
|
291
|
+
writeDataFile(collectionFilePath, sortedData);
|
292
|
+
}
|
293
|
+
return sortedData;
|
294
|
+
};
|
295
|
+
|
296
|
+
return {
|
297
|
+
insert,
|
298
|
+
read,
|
299
|
+
update,
|
300
|
+
remove,
|
301
|
+
find,
|
302
|
+
map,
|
303
|
+
transform,
|
304
|
+
filter,
|
305
|
+
sort,
|
306
|
+
list,
|
307
|
+
};
|
308
|
+
};
|
309
|
+
|
310
|
+
const findRef = (ref, evaluate = true) => {
|
311
|
+
const [name, id, ...rest] = ref.split('.');
|
312
|
+
const col = collection(name);
|
313
|
+
const record = col.read(id, evaluate);
|
314
|
+
if (rest.length === 0) return record;
|
315
|
+
let value = record;
|
316
|
+
for (const prop of rest) {
|
317
|
+
if (typeof value != 'object') break;
|
318
|
+
if (!(prop in value)) return null;
|
319
|
+
value = value[prop];
|
320
|
+
}
|
321
|
+
return value;
|
322
|
+
};
|
323
|
+
|
324
|
+
const map = (mapName) => {
|
325
|
+
const mapFilePath = path.join(dbDirPath, `${mapName}.map`);
|
326
|
+
|
327
|
+
const set = (key, value) => {
|
328
|
+
const mainData = readMainData();
|
329
|
+
if (!mainData.maps.includes(mapName)) {
|
330
|
+
mainData.maps.push(mapName);
|
331
|
+
writeMainData(mainData);
|
332
|
+
}
|
333
|
+
|
334
|
+
let data = {};
|
335
|
+
if (fs.existsSync(mapFilePath)) {
|
336
|
+
data = readDataFile(mapFilePath);
|
337
|
+
}
|
338
|
+
data[key] = value;
|
339
|
+
writeDataFile(mapFilePath, data);
|
340
|
+
};
|
341
|
+
|
342
|
+
const get = (key) => {
|
343
|
+
if (!fs.existsSync(mapFilePath)) return null;
|
344
|
+
const data = readDataFile(mapFilePath);
|
345
|
+
return data[key] || null;
|
346
|
+
};
|
347
|
+
|
348
|
+
const remove = (key) => {
|
349
|
+
if (!fs.existsSync(mapFilePath)) return false;
|
350
|
+
const data = readDataFile(mapFilePath);
|
351
|
+
if (data[key]) {
|
352
|
+
delete data[key];
|
353
|
+
writeDataFile(mapFilePath, data);
|
354
|
+
return true;
|
355
|
+
}
|
356
|
+
return false;
|
357
|
+
};
|
358
|
+
|
359
|
+
const transform = (cb, mutate = false) => {
|
360
|
+
const data = readDataFile(mapFilePath);
|
361
|
+
const transformedData = cb(data);
|
362
|
+
if (mutate) {
|
363
|
+
writeDataFile(mapFilePath, transformedData);
|
364
|
+
}
|
365
|
+
return transformedData;
|
366
|
+
};
|
367
|
+
|
368
|
+
const list = () => {
|
369
|
+
if (!fs.existsSync(mapFilePath)) return {};
|
370
|
+
const data = readDataFile(mapFilePath);
|
371
|
+
return data;
|
372
|
+
};
|
373
|
+
|
374
|
+
return { set, get, remove, list, transform };
|
375
|
+
};
|
376
|
+
|
377
|
+
readMainData();
|
378
|
+
|
379
|
+
return { setData, getData, collection, findRef, makeRef, map };
|
420
380
|
};
|
421
381
|
|
422
382
|
module.exports = (context) => ({
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
383
|
+
_onImport() {
|
384
|
+
delete this.createDB;
|
385
|
+
return this;
|
386
|
+
},
|
387
|
+
db(dbname, data = {}, encryptionKey) {
|
388
|
+
if (!context.app) throw new Error('rune can only be used in apps');
|
389
|
+
const pkg = path.join(CONFIG_PATH, context.app.config.package, 'db');
|
390
|
+
if (!fs.existsSync(pkg)) fs.mkdirSync(pkg, { recursive: true });
|
391
|
+
return createDB(dbname, pkg, data, encryptionKey || ENCRYPTION_KEY);
|
392
|
+
},
|
393
|
+
genKey(secret){
|
394
|
+
return gen_key(secret);
|
395
|
+
},
|
396
|
+
makeRef,
|
397
|
+
runePop,
|
398
|
+
runePush,
|
399
|
+
createDB,
|
437
400
|
});
|