Skip to content

Commit

Permalink
move citation rollup plugin to config folder
Browse files Browse the repository at this point in the history
  • Loading branch information
cherriechang committed Aug 27, 2024
1 parent 1c5f826 commit bbd09e2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/config/rollup-plugin-build-citation.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions packages/config/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bbd09e2

Please sign in to comment.