Files
promptfoo--promptfoo/scripts/generateCitation.ts
T
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

122 lines
3.6 KiB
TypeScript

import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as yaml from 'js-yaml';
import { loadYaml } from '../src/util/yamlLoad';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Represents the structure of package.json file
*/
interface PackageJson {
license: string;
version: string;
description: string;
}
/**
* Represents the structure of CITATION.cff file
*/
interface Citation {
'cff-version': string;
message: string;
authors: Array<{
'family-names': string;
'given-names': string;
}>;
title: string;
version: string;
'date-released': string;
url: string;
'repository-code': string;
license: string;
type: string;
description: string;
keywords: string[];
}
/**
* Creates a default Citation object with information from package.json
* @param packageJson - The parsed package.json file
* @returns A default Citation object
*/
const createDefaultCitation = (packageJson: PackageJson): Citation => ({
'cff-version': '1.2.0',
message: 'If you use this software, please cite it as below.',
authors: [
{
'family-names': 'Webster',
'given-names': 'Ian',
},
],
title: 'promptfoo',
version: packageJson.version,
'date-released': new Date().toISOString().slice(0, 10),
url: 'https://promptfoo.dev',
'repository-code': 'https://github.com/promptfoo/promptfoo',
license: packageJson.license,
type: 'software',
description: packageJson.description,
keywords: ['llm', 'evaluation', 'evals', 'testing', 'prompt-engineering', 'red-team'],
});
/**
* Fetches the release date for a specific version from GitHub
* @param version The version to fetch the release date for
* @returns Promise<string> The release date in ISO format, or null if not found
*/
async function getReleaseDate(version: string): Promise<string | null> {
try {
// biome-ignore lint/style/noRestrictedGlobals: Standalone script, fetchWithProxy not needed
const response = await fetch(
`https://api.github.com/repos/promptfoo/promptfoo/releases/tags/${version}`,
);
if (!response.ok) {
if (response.status === 404) {
console.warn(`No release found for version ${version}`);
return null;
}
throw new Error(`GitHub API request failed: ${response.statusText}`);
}
const data = await response.json();
return data.published_at ? new Date(data.published_at).toISOString().slice(0, 10) : null;
} catch (error) {
console.error(`Error fetching release date for version ${version}:`, error);
return null;
}
}
/**
* Updates the CITATION.cff file with the latest information from package.json and GitHub
* @throws {Error} If there's an issue reading or writing files
*/
export const updateCitation = async (): Promise<void> => {
const packageJsonPath: string = path.join(__dirname, '../package.json');
const citationPath: string = path.join(__dirname, '../CITATION.cff');
const packageJson: PackageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
let citation: Citation;
try {
citation = loadYaml(await fs.readFile(citationPath, 'utf8')) as Citation;
} catch {
citation = createDefaultCitation(packageJson);
}
citation['version'] = packageJson.version;
const releaseDate = await getReleaseDate(packageJson.version);
citation['date-released'] = releaseDate || new Date().toISOString().slice(0, 10);
await fs.writeFile(citationPath, yaml.dump(citation, { lineWidth: -1 }));
console.log('CITATION.cff file has been updated.');
};
updateCitation().catch((error) => {
console.error(error);
process.exitCode = 1;
});