Skip to content

Commit cf1125a

Browse files
✨ Add daily full summary
1 parent 5a9f509 commit cf1125a

File tree

1 file changed

+60
-17
lines changed

1 file changed

+60
-17
lines changed

src/cli.ts

+60-17
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
#!/usr/bin/env node
2+
import { lstat, pathExists, readdir, readJson, writeFile } from "fs-extra";
3+
import { join } from "path";
24
import {
3-
Spotify,
4-
Rescuetime,
5+
Clockify,
6+
Goodreads,
7+
GoogleFit,
58
LastFm,
9+
OuraRing,
610
PocketCasts,
11+
Rescuetime,
12+
Spotify,
13+
Twitter,
714
Wakatime,
15+
} from "./";
16+
import { zero } from "./common";
17+
18+
const INTEGRATIONS = [
819
Clockify,
20+
Goodreads,
921
GoogleFit,
22+
LastFm,
1023
OuraRing,
11-
Goodreads,
24+
PocketCasts,
25+
Rescuetime,
26+
Spotify,
1227
Twitter,
13-
} from "./";
28+
Wakatime,
29+
];
1430

1531
const cli = async () => {
1632
const command = process.argv[2];
@@ -20,26 +36,53 @@ const cli = async () => {
2036
const start = process.argv[4];
2137
if (!start) throw new Error("Provide a start date");
2238

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);
2943
}
30-
);
44+
});
3145
} else if (command === "summary") {
3246
const integration = process.argv[3];
3347
if (!integration) throw new Error("Provide an integration");
3448

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+
}
4078
}
4179
}
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+
}
4386
} else {
4487
throw new Error(`CLI command '${command}' not recognized`);
4588
}

0 commit comments

Comments
 (0)