You've already forked website-stats
mirror of
https://github.com/jakejarvis/website-stats.git
synced 2026-04-17 20:18:42 -04:00
bump deps & convert to ESM
This commit is contained in:
@@ -1,54 +1,46 @@
|
||||
const path = require("path");
|
||||
const fs = require("fs-extra");
|
||||
const pkgDir = require("pkg-dir");
|
||||
const got = require("got");
|
||||
const simpleGit = require("simple-git");
|
||||
const { DateTime } = require("luxon");
|
||||
const prettier = require("prettier");
|
||||
import path from "path";
|
||||
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/api/hits.ts#L101
|
||||
// https://github.com/jakejarvis/jarv.is/blob/main/pages/api/hits.ts#L101
|
||||
const API_ENDPOINT = "https://jarv.is/api/hits/";
|
||||
|
||||
(async () => {
|
||||
// find our bearings
|
||||
const rootDir = await pkgDir(__dirname);
|
||||
const fullDate = DateTime.now().toISODate();
|
||||
const jsonPath = path.join(
|
||||
rootDir,
|
||||
"data",
|
||||
DateTime.now().toFormat('yyyy'), // year
|
||||
DateTime.now().toFormat('LL'), // month
|
||||
`${fullDate}.json` // year-month-day.json
|
||||
);
|
||||
// formulate path to target .json file
|
||||
const today = new Date();
|
||||
const jsonPath = path.join(
|
||||
process.cwd(),
|
||||
"data",
|
||||
format(today, 'yyyy'), // year
|
||||
format(today, 'MM'), // month
|
||||
`${format(today, 'yyyy-MM-dd')}.json` // year-month-day.json
|
||||
);
|
||||
|
||||
// pull the latest stats from API
|
||||
console.info("📡 Fetching latest data from API...");
|
||||
const { body } = await got(API_ENDPOINT, { responseType: "json" });
|
||||
// pull the latest stats from API
|
||||
console.info("📡 Fetching latest data from API...");
|
||||
const data = await (await fetch(API_ENDPOINT)).json();
|
||||
|
||||
// write pretty JSON to timestamped file
|
||||
console.info(`✏️ Writing data to ${jsonPath} ...`);
|
||||
await fs.outputFile(
|
||||
jsonPath,
|
||||
prettier.format(JSON.stringify(body.pages), { parser: "json" })
|
||||
);
|
||||
// write pretty JSON to timestamped file
|
||||
console.info(`📂 Writing data to ${jsonPath} ...`);
|
||||
await writeJsonFile(jsonPath, data.pages);
|
||||
|
||||
// automatically push changes only if running from CI
|
||||
if (process.env.GITHUB_ACTION) {
|
||||
console.info("🤖 Detected CI: committing new data to repo and pushing back...");
|
||||
// automatically push changes only if running from CI
|
||||
if (process.env.GITHUB_ACTION) {
|
||||
console.info("🤖 Detected CI: committing new data to repo and pushing back...");
|
||||
|
||||
// the repo is located one level up from this script
|
||||
const git = simpleGit({ baseDir: rootDir });
|
||||
// the repo is located one level up from this script
|
||||
const git = simpleGit({ baseDir: process.cwd() });
|
||||
|
||||
// let a generic GitHub bot user author this commit instead of me:
|
||||
// https://github.community/t/github-actions-bot-email-address/17204/5
|
||||
git.addConfig("user.name", "github-actions[bot]");
|
||||
git.addConfig("user.email", "41898282+github-actions[bot]@users.noreply.github.com");
|
||||
// let a generic GitHub bot user author this commit instead of me:
|
||||
// https://github.community/t/github-actions-bot-email-address/17204/5
|
||||
git.addConfig("user.name", "github-actions[bot]");
|
||||
git.addConfig("user.email", "41898282+github-actions[bot]@users.noreply.github.com");
|
||||
|
||||
// do the normal git stuff
|
||||
await git.add([ jsonPath ]);
|
||||
await git.commit(`📈 Add new snapshot for ${fullDate}`);
|
||||
await git.push("origin", "main");
|
||||
} else {
|
||||
console.info("🤖 Didn't detect CI: skipping Git steps...");
|
||||
}
|
||||
})();
|
||||
// do the normal git stuff
|
||||
await git.add([jsonPath]);
|
||||
await git.commit(`📈 Add new snapshot for ${format(today, 'yyyy-MM-dd')}`);
|
||||
await git.push("origin", "main");
|
||||
} else {
|
||||
console.warn("🤖 Didn't detect CI: skipping Git steps...");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user