Skip to content

Commit

Permalink
πŸ‘¨πŸ»β€πŸ’» Address code review
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Sep 26, 2023
1 parent 513209c commit 239ae76
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-keys-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-common': patch
---

Add `pluginLoads` ruleId
2 changes: 1 addition & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ roles
:class: dropdown
transforms
: These plugins transform the document source while it is rendered.
: For example, add metadata or transform a link to an DOI.
: For example, add metadata or transform a link to a DOI.

renderers
: These plugins add handlers for various nodes when exporting to a specific format.
Expand Down
18 changes: 15 additions & 3 deletions packages/myst-cli/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'node:fs';
import type { ISession } from './session/types.js';
import { selectCurrentProjectConfig } from './store/selectors.js';
import type { MystPlugin } from 'myst-common';
import { RuleId, type MystPlugin } from 'myst-common';
import { addWarningForFile } from './utils/addWarningForFile.js';

export async function loadPlugins(session: ISession): Promise<MystPlugin> {
const config = selectCurrentProjectConfig(session.store.getState());
Expand All @@ -17,14 +18,25 @@ export async function loadPlugins(session: ISession): Promise<MystPlugin> {
const modules = await Promise.all(
config?.plugins?.map(async (filename) => {
if (!fs.statSync(filename).isFile || !filename.endsWith('.mjs')) {
session.log.error(`Unknown plugin ${filename}, it must be an mjs file`);
addWarningForFile(
session,
filename,
`Unknown plugin "${filename}", it must be an mjs file`,
'error',
{
ruleId: RuleId.pluginLoads,
},
);
return null;
}
let module: any;
try {
module = await import(filename);
} catch (error) {
session.log.debug(`\n\n${(error as Error)?.stack}\n\n`);
session.log.error(`Error reading plugin ${filename}: ${error}`);
addWarningForFile(session, filename, `Error reading plugin: ${error}`, 'error', {
ruleId: RuleId.pluginLoads,
});
return null;
}
return { filename, module };
Expand Down
2 changes: 2 additions & 0 deletions packages/myst-common/src/ruleids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ export enum RuleId {
sourceFileCopied = 'source-file-copied',
templateFileCopied = 'template-file-copied',
staticActionFileCopied = 'static-action-file-copied',
// Plugins
pluginLoads = 'plugin-loads',
}

0 comments on commit 239ae76

Please sign in to comment.