You've already forked website-stats
mirror of
https://github.com/jakejarvis/website-stats.git
synced 2026-04-17 20:18:42 -04:00
symlink a "latest.json" file to the freshest stats
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import fetch from "node-fetch";
|
||||
import { format } from "date-fns";
|
||||
import simpleGit from "simple-git";
|
||||
import { writeJsonFile } from "write-json-file";
|
||||
|
||||
// https://github.com/jakejarvis/jarv.is/blob/main/pages/api/hits.ts#L101
|
||||
// https://github.com/jakejarvis/jarv.is/blob/main/pages/api/hits.ts
|
||||
const API_ENDPOINT = "https://jarv.is/api/hits/";
|
||||
|
||||
// formulate path to target .json file
|
||||
@@ -16,6 +16,7 @@ const jsonPath = path.join(
|
||||
format(today, "MM"), // month
|
||||
`${format(today, "yyyy-MM-dd")}.json` // year-month-day.json
|
||||
);
|
||||
const latestPath = path.join(process.cwd(), "data", "latest.json");
|
||||
|
||||
// pull the latest stats from API
|
||||
console.info("📡 Fetching latest data from API...");
|
||||
@@ -23,7 +24,13 @@ const data = await (await fetch(API_ENDPOINT)).json();
|
||||
|
||||
// write pretty JSON to timestamped file
|
||||
console.info(`📂 Writing data to ${jsonPath} ...`);
|
||||
await writeJsonFile(jsonPath, data.pages);
|
||||
fs.mkdirSync(path.dirname(jsonPath), { recursive: true });
|
||||
fs.writeFileSync(jsonPath, JSON.stringify(data.pages || data, null, "\t") + "\n");
|
||||
|
||||
// update latest.json symlink to newest timestamped file
|
||||
console.info(`🔗 Pointing latest.json to ${jsonPath} ...`);
|
||||
fs.rmSync(latestPath, { force: true });
|
||||
fs.symlinkSync(path.relative(path.dirname(latestPath), jsonPath), latestPath, "file");
|
||||
|
||||
// automatically push changes only if running from CI
|
||||
if (process.env.GITHUB_ACTION) {
|
||||
@@ -38,7 +45,7 @@ if (process.env.GITHUB_ACTION) {
|
||||
git.addConfig("user.email", "41898282+github-actions[bot]@users.noreply.github.com");
|
||||
|
||||
// do the normal git stuff
|
||||
await git.add([jsonPath]);
|
||||
await git.add([jsonPath, latestPath]);
|
||||
await git.commit(`📈 Add new snapshot for ${format(today, "yyyy-MM-dd")}`);
|
||||
await git.push("origin", "main");
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user