forked from abhisheknaiidu/todoist-readme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (124 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const core = require("@actions/core");
const axios = require("axios");
const Humanize = require("humanize-plus");
const fs = require("fs");
const exec = require("./exec");
const TODOIST_API_KEY = core.getInput("TODOIST_API_KEY") || process.env.TODOIST_API_KEY;
const PREMIUM = core.getInput("PREMIUM");
async function main() {
// v8 => v9
const stats = await axios(
"https://api.todoist.com/sync/v9/completed/get_stats",
{ headers: { Authorization: `Bearer ${TODOIST_API_KEY}` } }
);
await updateReadme(stats.data);
}
let todoist = [];
let jobFailFlag = false;
const README_FILE_PATH = "./README.md";
async function updateReadme(data) {
const { karma, completed_count, days_items, goals, week_items } = data;
const karmaPoint = [`π **${Humanize.intComma(karma)}** Karma Points`];
todoist.push(karmaPoint);
const dailyGoal = [
`πΈ Completed **${days_items[0].total_completed.toString()}** tasks today`,
];
todoist.push(dailyGoal);
if (PREMIUM == "true") {
const weekItems = [
`π Completed **${week_items[0].total_completed.toString()}** tasks this week`,
];
todoist.push(weekItems);
}
const totalTasks = [
`β
Completed **${Humanize.intComma(completed_count)}** tasks so far`,
];
todoist.push(totalTasks);
const longestStreak = [
`β³ Longest streak is **${goals.max_daily_streak.count}** days`,
];
todoist.push(longestStreak);
// add the updated date in a pretty way
const date = new Date();
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
const lastUpdated = [
`π
Last updated: **${date.toLocaleDateString("en-US", options)}**`,
];
todoist.push(lastUpdated);
// const currentStreak = [
// `π₯ Current streak is **${goals.current_streak.count}** days`,
// ];
// todoist.push(currentStreak);
/**
*
*
*
* Done with composition of todoist array
*/
if (todoist.length == 0) return;
if (todoist.length > 0) {
// console.log(todoist.length);
// const showTasks = todoist.reduce((todo, cur, index) => {
// return todo + `\n${cur} ` + (((index + 1) === todoist.length) ? '\n' : '');
// })
const readmeData = fs.readFileSync(README_FILE_PATH, "utf8");
const newReadme = buildReadme(readmeData, todoist.join(" \n <br>"));
if (newReadme !== readmeData) {
core.info("βοΈ Writing to " + README_FILE_PATH);
fs.writeFileSync(README_FILE_PATH, newReadme);
// if (!process.env.TEST_MODE) {
// commitReadme();
// }
core.info("README.md updated π Successfully π");
process.exit(0);
// GitHub Action git push
} else {
core.info("No change detected, skipping");
process.exit(0);
}
} else {
core.info("Nothing fetched");
process.exit(jobFailFlag ? 1 : 0);
}
}
console.log(todoist.length);
const buildReadme = (prevReadmeContent, newReadmeContent) => {
const tagToLookFor = "<!-- TODO-IST:";
const closingTag = "-->";
const startOfOpeningTagIndex = prevReadmeContent.indexOf(
`${tagToLookFor}START`
);
const endOfOpeningTagIndex = prevReadmeContent.indexOf(
closingTag,
startOfOpeningTagIndex
);
const startOfClosingTagIndex = prevReadmeContent.indexOf(
`${tagToLookFor}END`,
endOfOpeningTagIndex
);
if (
startOfOpeningTagIndex === -1 ||
endOfOpeningTagIndex === -1 ||
startOfClosingTagIndex === -1
) {
core.error(
`Cannot find the comment tag on the readme:\n<!-- ${tagToLookFor}:START -->\n<!-- ${tagToLookFor}:END -->`
);
process.exit(1);
}
return [
prevReadmeContent.slice(0, endOfOpeningTagIndex + closingTag.length),
"\n",
newReadmeContent,
"\n",
prevReadmeContent.slice(startOfClosingTagIndex),
].join("");
};
(async () => {
await main();
})();