Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions astro/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import markdownOutput from "./src/plugins/markdown-output.js";
import contributorMapping from "./src/plugins/contributor-mapping.js";

// https://astro.build/config
const isDev = process.env.NODE_ENV !== "production";
Comment thread
maartenba marked this conversation as resolved.
Outdated

export default defineConfig({
site: "https://docs.duendesoftware.com",
trailingSlash: "ignore",
Expand All @@ -39,6 +41,7 @@ export default defineConfig({
contributorMapping({
include: ["src/content/docs/**"],
repo: "DuendeSoftware/docs.duendesoftware.com",
maxAgeDays: isDev ? 10 : 0,
}),
starlight({
customCss: ["./src/styles/custom.css"],
Expand Down
33 changes: 30 additions & 3 deletions astro/src/plugins/contributor-mapping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { execSync } from "node:child_process";
import { writeFileSync, mkdirSync } from "node:fs";
import { writeFileSync, mkdirSync, statSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import picomatch from "picomatch";
Expand All @@ -19,6 +19,15 @@ export interface ContributorMappingOptions {
* @example "DuendeSoftware/docs.duendesoftware.com"
*/
repo: string;

/**
* Maximum age (in days) of an existing contributors.json before it is
* regenerated. If the file exists and is younger than this, the plugin
* short-circuits and skips the expensive git + API work.
* Set to 0 to always regenerate.
* @default 0
*/
maxAgeDays?: number;
}

/**
Expand All @@ -36,13 +45,13 @@ export interface ContributorMappingOptions {
export default function contributorMapping(
options: ContributorMappingOptions
): AstroIntegration {
const { include, repo } = options;
const { include, repo, maxAgeDays = 0 } = options;

return {
name: "contributor-mapping",
hooks: {
"astro:config:setup": async ({ logger }) => {
await generateContributors(include, repo, logger);
await generateContributors(include, repo, maxAgeDays, logger);
},
},
};
Expand Down Expand Up @@ -384,8 +393,26 @@ function buildPerFileContributors(
async function generateContributors(
include: string[],
repo: string,
maxAgeDays: number,
logger: Logger
) {
// Short-circuit if the mapping file is fresh enough
if (maxAgeDays > 0) {
try {
const stat = statSync(outPath);
const ageDays =
(Date.now() - stat.mtimeMs) / (1000 * 60 * 60 * 24);
if (ageDays < maxAgeDays) {
logger.info(
`contributors.json is ${ageDays.toFixed(1)} days old (< ${maxAgeDays}) — skipping regeneration`
);
return;
}
} catch {
Comment thread
maartenba marked this conversation as resolved.
// File doesn't exist yet — continue to generate
}
}

const gitPrefix = getGitPrefix();
const isMatch = picomatch(include);

Expand Down
Loading