forked from JohnPhamous/strava-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
Β·59 lines (51 loc) Β· 1.36 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
#!/usr/bin/env node
require("dotenv").config();
const getStats = require("./stats");
const { Octokit } = require("@octokit/rest");
const error = require("./error");
const { GIST_ID: gistId, GIST_TOKEN: gistToken } = process.env;
const octokit = new Octokit({
auth: `token ${gistToken}`,
});
async function main() {
const body = await getStats();
await updateGist(body);
}
async function updateGist(/** @type {string} */ body) {
let gist;
try {
gist = await octokit.gists.get({ gist_id: gistId });
} catch (e) {
error("Unable to get gist", e);
}
try {
// Get original filename to update that same file
const filename = Object.keys(gist.data.files).sort()[0];
const existingBody = gist.data.files[filename].content;
// If the string does not contain anything other than whitespace
if (!/\S/.test(body)) {
body = `No activities yet for ${new Date().getFullYear()}, showing ${
new Date().getFullYear() - 1
}\n`;
if (!existingBody.startsWith(body)) body += existingBody;
}
if (gist.data.files[filename].content == body) return;
await octokit.gists.update({
gist_id: gistId,
files: {
[filename]: {
content: body,
},
},
});
} catch (e) {
error("Unable to update gist", e);
}
}
(async () => {
try {
await main();
} catch (e) {
error(e);
}
})();