Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

generateCitation.ts 3.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  1. import fs from 'fs';
  2. import yaml from 'js-yaml';
  3. import path from 'path';
  4. /**
  5. * Represents the structure of package.json file
  6. */
  7. interface PackageJson {
  8. license: string;
  9. version: string;
  10. description: string;
  11. }
  12. /**
  13. * Represents the structure of CITATION.cff file
  14. */
  15. interface Citation {
  16. 'cff-version': string;
  17. message: string;
  18. authors: Array<{
  19. 'family-names': string;
  20. 'given-names': string;
  21. }>;
  22. title: string;
  23. version: string;
  24. 'date-released': string;
  25. url: string;
  26. 'repository-code': string;
  27. license: string;
  28. type: string;
  29. description: string;
  30. keywords: string[];
  31. }
  32. /**
  33. * Creates a default Citation object with information from package.json
  34. * @param packageJson - The parsed package.json file
  35. * @returns A default Citation object
  36. */
  37. const createDefaultCitation = (packageJson: PackageJson): Citation => ({
  38. 'cff-version': '1.2.0',
  39. message: 'If you use this software, please cite it as below.',
  40. authors: [
  41. {
  42. 'family-names': 'Webster',
  43. 'given-names': 'Ian',
  44. },
  45. ],
  46. title: 'promptfoo',
  47. version: packageJson.version,
  48. 'date-released': new Date().toISOString().slice(0, 10),
  49. url: 'https://promptfoo.dev',
  50. 'repository-code': 'https://github.com/promptfoo/promptfoo',
  51. license: packageJson.license,
  52. type: 'software',
  53. description: packageJson.description,
  54. keywords: ['llm', 'evaluation', 'evals', 'testing', 'prompt-engineering', 'red-team'],
  55. });
  56. /**
  57. * Fetches the release date for a specific version from GitHub
  58. * @param version The version to fetch the release date for
  59. * @returns Promise<string> The release date in ISO format, or null if not found
  60. */
  61. async function getReleaseDate(version: string): Promise<string | null> {
  62. try {
  63. const response = await fetch(
  64. `https://api.github.com/repos/promptfoo/promptfoo/releases/tags/${version}`,
  65. );
  66. if (!response.ok) {
  67. if (response.status === 404) {
  68. console.warn(`No release found for version ${version}`);
  69. return null;
  70. }
  71. throw new Error(`GitHub API request failed: ${response.statusText}`);
  72. }
  73. const data = await response.json();
  74. return data.published_at ? new Date(data.published_at).toISOString().slice(0, 10) : null;
  75. } catch (error) {
  76. console.error(`Error fetching release date for version ${version}:`, error);
  77. return null;
  78. }
  79. }
  80. /**
  81. * Updates the CITATION.cff file with the latest information from package.json and GitHub
  82. * @throws {Error} If there's an issue reading or writing files
  83. */
  84. export const updateCitation = async (): Promise<void> => {
  85. const packageJsonPath: string = path.join(__dirname, '../package.json');
  86. const citationPath: string = path.join(__dirname, '../CITATION.cff');
  87. const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
  88. let citation: Citation;
  89. try {
  90. citation = yaml.load(fs.readFileSync(citationPath, 'utf8')) as Citation;
  91. } catch {
  92. citation = createDefaultCitation(packageJson);
  93. }
  94. citation['version'] = packageJson.version;
  95. const releaseDate = await getReleaseDate(packageJson.version);
  96. citation['date-released'] = releaseDate || new Date().toISOString().slice(0, 10);
  97. fs.writeFileSync(citationPath, yaml.dump(citation, { lineWidth: -1 }));
  98. console.log('CITATION.cff file has been updated.');
  99. };
  100. if (require.main === module) {
  101. updateCitation().catch(console.error);
  102. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...