forked from kristianfreeman/roam-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (98 loc) · 3.37 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
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
});
const fs = require("fs");
const glob = require("glob");
const puppeteer = require("puppeteer");
const generateExport = async () => {
const browser = await puppeteer.launch();
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(25000);
console.log("Successfully logged in");
await page.waitForSelector(
".flex-h-box > div > .bp3-popover-wrapper > .bp3-popover-target > .bp3-small"
);
await page.click(
".flex-h-box > div > .bp3-popover-wrapper > .bp3-popover-target > .bp3-small"
);
console.log("Opening Export menu");
await page.waitForSelector(
".bp3-popover-content > .bp3-menu > li:nth-child(3) > .bp3-menu-item > .bp3-text-overflow-ellipsis"
);
await page.click(
".bp3-popover-content > .bp3-menu > li:nth-child(3) > .bp3-menu-item > .bp3-text-overflow-ellipsis"
);
await page.waitForSelector(
".bp3-popover-wrapper > .bp3-popover-target > div > .bp3-button > .bp3-button-text"
);
await page.click(
".bp3-popover-wrapper > .bp3-popover-target > div > .bp3-button > .bp3-button-text"
);
console.log("Selecting JSON export");
await page.waitForSelector(
"div > .bp3-menu > li > .bp3-menu-item > .bp3-text-overflow-ellipsis"
);
await page.click(
"div > .bp3-menu > li > .bp3-menu-item > .bp3-text-overflow-ellipsis"
);
console.log("Creating export");
await page.waitForSelector(
".bp3-dialog-container > .bp3-dialog > div > .flex-h-box > .bp3-intent-primary"
);
await page.click(
".bp3-dialog-container > .bp3-dialog > div > .flex-h-box > .bp3-intent-primary"
);
console.log("Created export");
console.log("Waiting five seconds for it to download");
await page.waitFor(25000);
} 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 main = async function() {
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} to S3`);
await uploadToS3(filename);
};
main();