Skip to content

Commit 483f876

Browse files
Chore: Build Readme
1 parent 8a9f42f commit 483f876

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "📊 Updates README with your Todoist stats",
55
"main": "todoist-update.js",
66
"dependencies": {
7+
"@actions/core": "^1.2.5",
78
"axios": "^0.20.0",
89
"fs": "^0.0.1-security",
910
"humanize-plus": "^1.8.2"

todoist-update.js

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const core = require("@actions/core");
12
const axios = require("axios");
23
const Humanize = require("humanize-plus");
4+
const fs = require("fs");
35

4-
const TODOIST_API_KEY = "c1d1363fde9478b301eb037c4541abafb00ee07c";
6+
const TODOIST_API_KEY = core.getInput("TODOIST_API_KEY");
57

68
async function main() {
79
const stats = await axios(`https://api.todoist.com/sync/v8.3/completed/get_stats?token=${TODOIST_API_KEY}`);
@@ -10,29 +12,76 @@ async function main() {
1012

1113
async function updateReadme(data) {
1214

13-
const lines = [];
15+
const todoist = [];
1416
const { karma, completed_count, days_items, goals } = data;
1517

16-
const karmaPoint = [`🏅 ${Humanize.intComma(karma)} Karma Points`];
17-
lines.push(karmaPoint.join(" "));
18+
const karmaPoint = [`🌈 ${Humanize.intComma(karma)} Karma Points`];
19+
todoist.push(karmaPoint.join(" "));
1820

1921
const dailyGoal = [
2022
`🌸 Completed ${days_items[0].total_completed.toString()} tasks today`,
2123
];
22-
lines.push(dailyGoal.join(" "));
24+
todoist.push(dailyGoal.join(" "));
2325

2426
const totalTasks = [`✅ Completed ${Humanize.intComma(completed_count)} tasks so far`];
25-
lines.push(totalTasks.join(" "));
27+
todoist.push(totalTasks.join(" "));
2628

2729
const longestStreak = [
2830
`⌛ Longest streak is ${goals.max_daily_streak.count} days`,
2931
];
30-
lines.push(longestStreak.join(" "));
32+
todoist.push(longestStreak.join(" "));
3133

32-
if (lines.length == 0) return;
33-
console.log(lines.join("\n"));
34+
if (todoist.length == 0) return;
35+
// console.log(lines.join("\n"));
36+
37+
try {
38+
console.log(todoist.join("\n"));
39+
const readmeData = fs.readFileSync(README_FILE_PATH, 'utf8');
40+
const newReadme = buildReadme(readmeData, todoist);
41+
42+
if (newReadme !== readmeData) {
43+
core.info('Writing to ' + README_FILE_PATH);
44+
fs.writeFileSync(README_FILE_PATH, newReadme);
45+
}
46+
47+
} catch (error) {
48+
console.error(`Unable to update gist\n${error}`);
49+
}
3450
}
3551

52+
const buildReadme = (prevReadmeContent, newReadmeContent) => {
53+
const tagToLookFor = '<!--TODO-LIST:';
54+
const closingTag = '-->';
55+
const startOfOpeningTagIndex = prevReadmeContent.indexOf(
56+
`${tagToLookFor}START`,
57+
);
58+
const endOfOpeningTagIndex = prevReadmeContent.indexOf(
59+
closingTag,
60+
startOfOpeningTagIndex,
61+
);
62+
const startOfClosingTagIndex = prevReadmeContent.indexOf(
63+
`${tagToLookFor}END`,
64+
endOfOpeningTagIndex,
65+
);
66+
if (
67+
startOfOpeningTagIndex === -1 ||
68+
endOfOpeningTagIndex === -1 ||
69+
startOfClosingTagIndex === -1
70+
) {
71+
core.error(
72+
`Cannot find the comment tag on the readme:\n<!-- ${tagToLookFor}:START -->\n<!-- ${tagToLookFor}:END -->`
73+
);
74+
process.exit(1);
75+
}
76+
return [
77+
prevReadmeContent.slice(0, endOfOpeningTagIndex + closingTag.length),
78+
'',
79+
newReadmeContent,
80+
'',
81+
prevReadmeContent.slice(startOfClosingTagIndex),
82+
].join('');
83+
};
84+
3685
(async () => {
3786
await main();
3887
})();

0 commit comments

Comments
 (0)