diff --git a/packages/config/rollup-plugin-build-citation.js b/packages/config/rollup-plugin-build-citation.js new file mode 100644 index 0000000000..6fd80e6ca3 --- /dev/null +++ b/packages/config/rollup-plugin-build-citation.js @@ -0,0 +1,44 @@ +const fs = require("node:fs"); +const path = require("path"); +require("@citation-js/plugin-software-formats"); +const { Cite } = require("@citation-js/core"); + +function cffToJsonPlugin(options = {}) { + return { + name: "rollup-plugin-cff-to-json", + version: "1.0.0", + + generateBundle() { + const srcDir = options.srcDir || __dirname; + const indexFilePath = path.join(srcDir, "index.js"); // Assume index.js is in src + + const updateCitations = (indexFilePath, citationJson) => { + let fileContent = fs.readFileSync(indexFilePath, "utf-8"); + fileContent = fileContent.replace(/`{citationJson}`/g, citationJson); + fs.writeFileSync(indexFilePath, fileContent, "utf-8"); + }; + + const templateDir = path.dirname(srcDir); + const cffFilePath = path.join(templateDir, "CITATION.cff"); // Assume CITATION.cff is top level + + try { + let cffCitation = fs.readFileSync(cffFilePath, "utf-8").toString(); + Cite.async(cffCitation).then((data) => { + const citationJson = JSON.stringify( + data.format("data", { + format: "object", + lang: "en-US", + }), + null, + 2 + ); + updateCitations(indexFilePath, citationJson); + }); + } catch (error) { + this.error(`Error building citation from CITATION.cff: ${error.message}`); + } + }, + }; +} + +module.exports = cffToJsonPlugin; diff --git a/packages/config/rollup.js b/packages/config/rollup.js index caac6d97a8..90586fee33 100644 --- a/packages/config/rollup.js +++ b/packages/config/rollup.js @@ -9,6 +9,8 @@ import esbuild from "rollup-plugin-esbuild"; import externals from "rollup-plugin-node-externals"; import ts from "typescript"; +import cffToJsonPlugin from "./rollup-plugin-build-citation"; + const getTsCompilerOptions = () => { const cwd = process.cwd(); return ts.parseJsonConfigFileContent(