Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rollup plugin for building citation #3385

Open
wants to merge 17 commits into
base: update-config-dependencies
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,163 changes: 2,930 additions & 3,233 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"@changesets/changelog-github": "^0.4.7",
"@changesets/cli": "^2.25.2",
"@jspsych/config": "^1.3.2",
"@jspsych/config": "^3.0.0",
"husky": "^8.0.2",
"import-sort-style-module": "^6.0.0",
"lint-staged": "^13.0.3",
Expand All @@ -51,5 +51,12 @@
"projects": [
"<rootDir>/packages/*/jest.config.cjs"
]
},
"dependencies": {
"@citation-js/core": "^0.7.14",
"@citation-js/plugin-software-formats": "^0.6.1",
"@citation-js/plugin-bibtex": "^0.7.14",
"@citation-js/plugin-csl": "^0.7.14",
"symlink": "^2.1.0"
}
}
10 changes: 10 additions & 0 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"homepage": "https://www.jspsych.org/latest/developers/configuration",
"dependencies": {
"@rollup/plugin-commonjs": "26.0.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "15.2.3",
"@sucrase/jest-plugin": "3.0.0",
"@types/gulp": "4.0.17",
Expand All @@ -59,5 +60,14 @@
"sucrase": "3.34.0",
"tslib": "2.6.2",
"typescript": "^5.2.2"
},
"overrides": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I last checked a few months ago, NPM did only respect overrides defined in the root package.json. Did that change in a recent NPM version? If so, we still can't rely on it until we require the new version...

"alias-hq": {
"jscodeshift": "0.16.1",
"inquirer": "10.1.6"
}
},
"devDependencies": {
"yaml": "^2.5.1"
}
}
98 changes: 98 additions & 0 deletions packages/config/rollup-plugin-build-citation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import "@citation-js/plugin-bibtex";
import "@citation-js/plugin-software-formats";
import "@citation-js/plugin-csl";

import fs from "node:fs";
import { extname } from "path";

import { Cite } from "@citation-js/core";
import { createFilter } from "@rollup/pluginutils";
import MagicString from "magic-string";
import yaml from "yaml";

export default function cffToJsonPlugin() {
const options = { include: ["**/index*"], exclude: [], sourcemap: false };
let filter = createFilter(options.include, options.exclude);

return {
name: "rollup-plugin-cff-to-json",
version: "1.0.0",
transform: function (code, id) {
if (!filter(id) || (extname(id) !== ".js" && extname(id) !== ".ts")) return;
const magicString = new MagicString(code);
let preferredCitation = false;

// Try to find CITATION.cff file and look for preferred-citation
const citationCff = (() => {
try {
const rawCff = fs.readFileSync("./CITATION.cff", "utf-8").toString();
const cffData = yaml.parse(rawCff);
if (cffData["preferred-citation"]) {
console.log("Found 'preferred-citation' in CITATION.cff");
preferredCitation = true;
} else {
console.log("No 'preferred-citation' found in CITATION.cff");
}
return yaml.stringify(rawCff);
} catch (error) {
console.log(`Error finding CITATION.cff: ${error.message}`);
return null;
}
})();

// Try to convert CITATION.cff to APA string
const citationApa = (() => {
try {
const apaCite = new Cite(citationCff);
apaCite["data"] = preferredCitation ? apaCite["data"].slice(1) : apaCite["data"];
const citationApa = apaCite.format("bibliography", {
format: "text",
template: "apa",
lang: "en-us",
});
return citationApa;
} catch (error) {
console.log(`Error converting CITATION.cff to APA string: ${error.message}`);
return null;
}
})();

// Try to convert CITATION.cff to bibtex string
const citationBibtex = (() => {
try {
const bibtexCite = new Cite(citationCff);
bibtexCite["data"] = preferredCitation ? bibtexCite["data"].slice(1) : bibtexCite["data"];
const citationBibtex = bibtexCite.format("bibtex", {
format: "text",
template: "bibtex",
lang: "en-us",
});
return citationBibtex;
} catch (error) {
console.log(`Error converting CITATION.cff to bibtex string: ${error.message}`);
return null;
}
})();

// Replace target string with citation APA and bibtex strings
if (!citationApa && !citationBibtex) {
return { code: code };
}
//console.log(`citation: {\napa: "${citationApa.replace(/\n/g, ' ')}", \nbibtex: "${citationBibtex.replace(/\n/g, ' ')}"\n}`);
const citationString = `citation:\n { apa: "${citationApa.replace(
/\n/g,
" "
)}",\n bibtex: "${citationBibtex.replace(/\n/g, " ")}"\n }`;
console.log(citationString);
const targetString = "citation: []";
const startIndex = code.indexOf(targetString);
if (startIndex !== -1) {
magicString.overwrite(startIndex, startIndex + targetString.length, citationString);
return { code: magicString.toString() };
} else {
this.error(`Error replacing citation string in ${id}`);
return { code: code };
}
},
};
}
15 changes: 13 additions & 2 deletions packages/config/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { readFileSync } from "node:fs";
import path from "path";

import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import resolve from "@rollup/plugin-node-resolve";
import { defineConfig } from "rollup";
import dts from "rollup-plugin-dts";
import esbuild from "rollup-plugin-esbuild";
import externals from "rollup-plugin-node-externals";
import ts from "typescript";

import cffToJsonPlugin from "./rollup-plugin-build-citation.js";

const getTsCompilerOptions = () => {
const cwd = process.cwd();
return ts.parseJsonConfigFileContent(
Expand All @@ -27,6 +30,7 @@ const makeConfig = ({
outputOptions = {},
globalOptions = {},
iifeOutputOptions = {},
additionalPlugins = [],
isNodeOnlyBuild = false,
}) => {
const input = "src/index.ts";
Expand All @@ -40,7 +44,7 @@ const makeConfig = ({

/** @type{import("rollup-plugin-esbuild").Options} */
const esBuildPluginOptions = {
loaders: { ".json": "json" },
//loaders: { ".json": "json" },
};

/** @type{import("@rollup/plugin-commonjs").RollupCommonJSOptions} */
Expand Down Expand Up @@ -70,7 +74,9 @@ const makeConfig = ({
...globalOptions,
input,
plugins: [
...additionalPlugins,
externals(),
json(),
esbuild({ ...esBuildPluginOptions, target: "node18" }),
commonjs(commonjsPluginOptions),
],
Expand All @@ -95,10 +101,12 @@ const makeConfig = ({
...globalOptions,
input,
plugins: [
...additionalPlugins,
externals({ deps: false }),
resolve({ preferBuiltins: false }),
esbuild({ ...esBuildPluginOptions, target: "esnext" }),
json(),
commonjs(commonjsPluginOptions),
esbuild({ ...esBuildPluginOptions, target: "esnext" }),
],
output: {
file: `${destination}.browser.js`,
Expand All @@ -114,8 +122,10 @@ const makeConfig = ({
...globalOptions,
input,
plugins: [
...additionalPlugins,
externals({ deps: false }),
resolve({ preferBuiltins: false }),
json(),
esbuild({ ...esBuildPluginOptions, target: "es2015", minify: true }),
commonjs(commonjsPluginOptions),
],
Expand Down Expand Up @@ -145,6 +155,7 @@ export const makeRollupConfig = (iifeName) =>
exports: "default",
globals: { jspsych: "jsPsychModule" },
},
additionalPlugins: [cffToJsonPlugin()],
globalOptions: { external: ["jspsych"] },
iifeOutputOptions: { name: iifeName },
});
Expand Down
14 changes: 14 additions & 0 deletions packages/jspsych/src/JsPsych.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import autoBind from "auto-bind";
// To work with citations
import { Class } from "type-fest";

import { version } from "../package.json";
import { ExtensionManager, ExtensionManagerDependencies } from "./ExtensionManager";
import { JsPsychData, JsPsychDataDependencies } from "./modules/data";
import { JsPsychExtension } from "./modules/extensions";
import { PluginAPI, createJointPluginAPIObject } from "./modules/plugin-api";
import { JsPsychPlugin } from "./modules/plugins";
import * as randomization from "./modules/randomization";
import * as turk from "./modules/turk";
import * as utils from "./modules/utils";
Expand Down Expand Up @@ -257,6 +261,16 @@ export class JsPsych {
return this.timeline?.description.timeline;
}

getCitations(
plugins: Array<Class<JsPsychPlugin<any>> | JsPsychExtension>,
format: "apa" | "bibtex"
) {
plugins.map((plugin) => {
let pluginCitation = plugin["info"].citation;
console.log(format == "apa" ? pluginCitation.apa : pluginCitation.bibtex);
});
}

get extensions() {
return this.extensionManager?.extensions ?? {};
}
Expand Down
47 changes: 47 additions & 0 deletions packages/plugin-html-keyboard-response/CITATION.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cff-version: "1.2.0"
authors:
- family-names: Leeuw
given-names: Joshua R.
name-particle: de
orcid: "https://orcid.org/0000-0003-4815-2364"
- family-names: Gilbert
given-names: Rebecca A.
orcid: "https://orcid.org/0000-0003-4574-7792"
- family-names: Luchterhandt
given-names: Björn
orcid: "https://orcid.org/0000-0002-9225-2787"
contact:
- family-names: Leeuw
given-names: Joshua R.
name-particle: de
orcid: "https://orcid.org/0000-0003-4815-2364"
doi: 10.5281/zenodo.7702307
message: If you use this software, please cite our article in the
Journal of Open Source Software.
preferred-citation:
authors:
- family-names: Leeuw
given-names: Joshua R.
name-particle: de
orcid: "https://orcid.org/0000-0003-4815-2364"
- family-names: Gilbert
given-names: Rebecca A.
orcid: "https://orcid.org/0000-0003-4574-7792"
- family-names: Luchterhandt
given-names: Björn
orcid: "https://orcid.org/0000-0002-9225-2787"
date-published: 2023-05-11
doi: 10.21105/joss.05351
issn: 2475-9066
issue: 85
journal: Journal of Open Source Software
publisher:
name: Open Journals
start: 5351
title: "jsPsych: Enabling an Open-Source Collaborative Ecosystem of
Behavioral Experiments"
type: article
url: "https://joss.theoj.org/papers/10.21105/joss.05351"
volume: 8
title: "jsPsych: Enabling an Open-Source Collaborative Ecosystem of
Behavioral Experiments"
1 change: 1 addition & 0 deletions packages/plugin-html-keyboard-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const info = <const>{
type: ParameterType.STRING,
},
},
citation: [],
};

type Info = typeof info;
Expand Down
Loading