This repository was archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (47 loc) · 1.42 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
const fs = require('fs');
const fetch = require('node-fetch');
const sjcl = require('sjcl');
const yargs = require('yargs');
const MONOD_URL = 'https://monod.lelab.tailordev.fr';
const decrypt = (content, secret) => {
let decryptedContent;
try {
decryptedContent = sjcl.decrypt(secret, content);
} catch (e) {
return null;
}
return decryptedContent;
}
const main = () => {
const argv = yargs.options({
'stdout': {
description: 'output document(s) to the standard output',
type: 'boolean'
}
}).argv;
const stdout = !!argv.stdout;
const urls = argv._.filter((url) => url.startsWith(MONOD_URL));
if (urls.length === 0) {
console.error('monod-cli: no urls to backup, exiting...');
process.exit(1);
}
urls.forEach(async (url) => {
const [uuid, secret] = url.replace(`${MONOD_URL}/`, '').split('#');
try {
const response = await fetch(`${MONOD_URL}/documents/${uuid}`);
const document = await response.json();
const content = decrypt(document.content, secret);
if (stdout) {
console.log(content);
} else {
const filename = `monod-${uuid}.md`;
await fs.promises.writeFile(filename, content);
console.log(`[+] ${filename}`);
}
} catch (e) {
console.error(`monod-cli: could not backup document at "${url}".`);
console.error(`monod-cli: ${e.message}`);
}
});
};
module.exports = { main };