move citation rollup plugin to config folder

This commit is contained in:
cchang-vassar 2024-08-27 13:43:12 -04:00
parent 1c5f826fbe
commit bbd09e2ace
2 changed files with 46 additions and 0 deletions

View File

@ -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;

View File

@ -9,6 +9,8 @@ import esbuild from "rollup-plugin-esbuild";
import externals from "rollup-plugin-node-externals"; import externals from "rollup-plugin-node-externals";
import ts from "typescript"; import ts from "typescript";
import cffToJsonPlugin from "./rollup-plugin-build-citation";
const getTsCompilerOptions = () => { const getTsCompilerOptions = () => {
const cwd = process.cwd(); const cwd = process.cwd();
return ts.parseJsonConfigFileContent( return ts.parseJsonConfigFileContent(