1
1
#!/usr/bin/env node
2
+ import { lstat , pathExists , readdir , readJson , writeFile } from "fs-extra" ;
3
+ import { join } from "path" ;
2
4
import {
3
- Spotify ,
4
- Rescuetime ,
5
+ Clockify ,
6
+ Goodreads ,
7
+ GoogleFit ,
5
8
LastFm ,
9
+ OuraRing ,
6
10
PocketCasts ,
11
+ Rescuetime ,
12
+ Spotify ,
13
+ Twitter ,
7
14
Wakatime ,
15
+ } from "./" ;
16
+ import { zero } from "./common" ;
17
+
18
+ const INTEGRATIONS = [
8
19
Clockify ,
20
+ Goodreads ,
9
21
GoogleFit ,
22
+ LastFm ,
10
23
OuraRing ,
11
- Goodreads ,
24
+ PocketCasts ,
25
+ Rescuetime ,
26
+ Spotify ,
12
27
Twitter ,
13
- } from "./" ;
28
+ Wakatime ,
29
+ ] ;
14
30
15
31
const cli = async ( ) => {
16
32
const command = process . argv [ 2 ] ;
@@ -20,26 +36,53 @@ const cli = async () => {
20
36
const start = process . argv [ 4 ] ;
21
37
if ( ! start ) throw new Error ( "Provide a start date" ) ;
22
38
23
- [ Spotify , Rescuetime , LastFm , PocketCasts , Wakatime , Clockify , GoogleFit , OuraRing , Goodreads , Twitter ] . forEach (
24
- ( ClassName ) => {
25
- const integrationObject = new ClassName ( ) ;
26
- if ( integration === integrationObject . name ) {
27
- integrationObject . legacy ( start ) ;
28
- }
39
+ INTEGRATIONS . forEach ( ( ClassName ) => {
40
+ const integrationObject = new ClassName ( ) ;
41
+ if ( integration === integrationObject . name ) {
42
+ integrationObject . legacy ( start ) ;
29
43
}
30
- ) ;
44
+ } ) ;
31
45
} else if ( command === "summary" ) {
32
46
const integration = process . argv [ 3 ] ;
33
47
if ( ! integration ) throw new Error ( "Provide an integration" ) ;
34
48
35
- [ Spotify , Rescuetime , LastFm , PocketCasts , Wakatime , Clockify , GoogleFit , OuraRing , Goodreads , Twitter ] . forEach (
36
- ( ClassName ) => {
37
- const integrationObject = new ClassName ( ) ;
38
- if ( integration === integrationObject . name ) {
39
- integrationObject . summary ( ) ;
49
+ INTEGRATIONS . forEach ( async ( ClassName ) => {
50
+ const integrationObject = new ClassName ( ) ;
51
+ if ( integration === integrationObject . name ) {
52
+ integrationObject . summary ( ) ;
53
+ }
54
+ } ) ;
55
+
56
+ const createdIntegrationData = await readdir ( join ( "." , "data" ) ) ;
57
+ for ( const dir of createdIntegrationData ) {
58
+ const summary : Record < string , any > = { } ;
59
+ if (
60
+ ( await pathExists ( join ( "." , "data" , dir , "summary" , "days" ) ) ) &&
61
+ ( await lstat ( join ( "." , "data" , dir , "summary" , "days" ) ) ) . isDirectory ( )
62
+ ) {
63
+ const years = await readdir ( join ( "." , "data" , dir , "summary" , "days" ) ) ;
64
+ for ( const year of years ) {
65
+ if (
66
+ ( await pathExists ( join ( "." , "data" , dir , "summary" , "days" , year ) ) ) &&
67
+ ( await lstat ( join ( "." , "data" , dir , "summary" , "days" , year ) ) ) . isDirectory ( )
68
+ ) {
69
+ const months = await readdir ( join ( "." , "data" , dir , "summary" , "days" , year ) ) ;
70
+ for ( const month of months ) {
71
+ const file = join ( "." , "data" , dir , "summary" , "days" , year , month ) ;
72
+ const data = ( await readJson ( file ) ) as Record < string , any > ;
73
+ Object . values ( data ) . forEach ( ( [ day , value ] ) => {
74
+ summary [ `${ zero ( year ) } -${ zero ( month ) } -${ zero ( day ) } ` ] = value ;
75
+ } ) ;
76
+ }
77
+ }
40
78
}
41
79
}
42
- ) ;
80
+ if ( Object . keys ( summary ) . length )
81
+ await writeFile (
82
+ join ( "." , "data" , dir , "summary" , "days.json" ) ,
83
+ JSON . stringify ( summary , null , 2 ) + "\n"
84
+ ) ;
85
+ }
43
86
} else {
44
87
throw new Error ( `CLI command '${ command } ' not recognized` ) ;
45
88
}
0 commit comments