1
+ const core = require ( "@actions/core" ) ;
1
2
const axios = require ( "axios" ) ;
2
3
const Humanize = require ( "humanize-plus" ) ;
4
+ const fs = require ( "fs" ) ;
3
5
4
- const TODOIST_API_KEY = "c1d1363fde9478b301eb037c4541abafb00ee07c" ;
6
+ const TODOIST_API_KEY = core . getInput ( "TODOIST_API_KEY" ) ;
5
7
6
8
async function main ( ) {
7
9
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() {
10
12
11
13
async function updateReadme ( data ) {
12
14
13
- const lines = [ ] ;
15
+ const todoist = [ ] ;
14
16
const { karma, completed_count, days_items, goals } = data ;
15
17
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 ( " " ) ) ;
18
20
19
21
const dailyGoal = [
20
22
`🌸 Completed ${ days_items [ 0 ] . total_completed . toString ( ) } tasks today` ,
21
23
] ;
22
- lines . push ( dailyGoal . join ( " " ) ) ;
24
+ todoist . push ( dailyGoal . join ( " " ) ) ;
23
25
24
26
const totalTasks = [ `✅ Completed ${ Humanize . intComma ( completed_count ) } tasks so far` ] ;
25
- lines . push ( totalTasks . join ( " " ) ) ;
27
+ todoist . push ( totalTasks . join ( " " ) ) ;
26
28
27
29
const longestStreak = [
28
30
`⌛ Longest streak is ${ goals . max_daily_streak . count } days` ,
29
31
] ;
30
- lines . push ( longestStreak . join ( " " ) ) ;
32
+ todoist . push ( longestStreak . join ( " " ) ) ;
31
33
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
+ }
34
50
}
35
51
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
+
36
85
( async ( ) => {
37
86
await main ( ) ;
38
87
} ) ( ) ;
0 commit comments