forked from chronologos/roam-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (104 loc) · 3.8 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
require("dotenv").config();
const config = {
backupFolder: "backups"
};
const AWS = require("aws-sdk");
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_ACCESS_KEY_SECRET
});
// Use application default credentials for GCS.
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const fs = require("fs");
const glob = require("glob");
const puppeteer = require("puppeteer");
async function waitAndClick(page, selector) {
await page.waitForSelector(selector);
await page.click(selector);
}
const generateExport = async () => {
const browser = await puppeteer.launch();
// {
// args: ['--remote-debugging-port=9222']
// })
const page = await browser.newPage();
try {
await page._client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath: process.cwd()
});
await page.goto("https://roamresearch.com/#/signin");
console.log("Logging into Roam");
await page.focus('[name="email"]');
await page.keyboard.type(process.env.ROAM_EMAIL);
await page.focus('[name="password"]');
await page.keyboard.type(process.env.ROAM_PASSWORD);
await page.$eval(".bp3-button", el => el.click());
await page.waitFor(5000);
console.log("Successfully logged in");
await waitAndClick(page, ".flex-h-box > div > .bp3-popover-wrapper > .bp3-popover-target > .bp3-small"); // Options menu
console.log("Opening Export menu");
await waitAndClick(page, ".bp3-popover-content > .bp3-menu > li:nth-child(3) > .bp3-menu-item > .bp3-text-overflow-ellipsis"); // "Export" list item
await waitAndClick(page, ".bp3-popover-wrapper > .bp3-popover-target > div > .bp3-button > .bp3-button-text") // "Markdown"
console.log("Selecting MD export");
// await waitAndClick(page, "div > .bp3-menu > li > .bp3-menu-item > .bp3-text-overflow-ellipsis") // "JSON"
console.log("Waiting for switch to MD format")
await page.waitFor(5000);
console.log("Creating export");
await waitAndClick(page, ".bp3-dialog-container > .bp3-dialog > div > .flex-h-box > .bp3-intent-primary") // "Export" button
console.log("Waiting 15 seconds for it to download");
await page.waitFor(15000);
} catch (err) {
console.error("Something went wrong!");
console.error(err);
await page.screenshot({path: "error.png"});
}
await browser.close();
};
const uploadToS3 = async filename => {
try {
const fileContent = fs.readFileSync(filename);
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `${config.backupFolder}/${filename}`,
Body: fileContent
};
const data = await s3.upload(params).promise();
console.log(`Successfully backed up Roam data to S3: ${data.Location}`);
} catch (err) {
console.error("Something went wrong while uploading to S3");
console.error(err);
}
};
const uploadToGCS = async filename => {
try {
const fileContent = fs.readFileSync(filename);
console.log(`Uploading to ${`process.env.GCS_BUCKET_NAME`}`)
const bucket = storage.bucket(process.env.GCS_BUCKET_NAME);
const data = await bucket.upload(filename)
console.log(`Successfully backed up Roam data to GCS: ${data[0].metadata.selfLink}`);
} catch (err) {
console.error("Something went wrong while uploading to GCS");
console.error(err);
}
};
const main = async function () {
if (!process.env.STORAGE_OPT){
console.log("STORAGE_OPT must be set to `aws` or `gcp`")
return
}
await generateExport();
const files = glob.sync("*.zip");
const filename = files[0];
if (!filename) {
throw new Error("Couldn't find a file to upload, aborting");
}
console.log(`Uploading ${filename}`);
if (process.env.STORAGE_OPT == "aws") {
await uploadToS3(filename);
} else {
await uploadToGCS(filename);
}
};
main();