From aade4d25bde7b7927228f6202a6896cdca18d9d1 Mon Sep 17 00:00:00 2001 From: sven1103-agent <261423644+sven1103-agent@users.noreply.github.com> Date: Mon, 30 Mar 2026 18:03:21 +0200 Subject: [PATCH] fix: make npm plugin entrypoint callable --- index.cjs | 18 ++++++++++++++ package.json | 9 +++++-- test/plugin-entrypoint.test.js | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 index.cjs create mode 100644 test/plugin-entrypoint.test.js diff --git a/index.cjs b/index.cjs new file mode 100644 index 0000000..84a326d --- /dev/null +++ b/index.cjs @@ -0,0 +1,18 @@ +"use strict"; + +async function loadPlugin() { + const mod = await import("./src/index.js"); + return mod.default || mod.WorktreeWorkflowPlugin; +} + +async function WorktreeWorkflowPlugin(...args) { + const plugin = await loadPlugin(); + if (typeof plugin !== "function") { + throw new TypeError("Plugin export is not a function"); + } + return plugin(...args); +} + +module.exports = WorktreeWorkflowPlugin; +module.exports.default = WorktreeWorkflowPlugin; +module.exports.WorktreeWorkflowPlugin = WorktreeWorkflowPlugin; diff --git a/package.json b/package.json index b69df1c..565a980 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,19 @@ "version": "0.1.0", "description": "OpenCode plugin for creating and cleaning up git worktrees.", "type": "module", - "main": "./src/index.js", + "main": "./index.cjs", "bin": { "opencode-worktree-workflow": "./src/cli.js" }, "exports": { - ".": "./src/index.js" + ".": { + "require": "./index.cjs", + "import": "./src/index.js", + "default": "./index.cjs" + } }, "files": [ + "index.cjs", "src", "schemas", "skills" diff --git a/test/plugin-entrypoint.test.js b/test/plugin-entrypoint.test.js new file mode 100644 index 0000000..c49d743 --- /dev/null +++ b/test/plugin-entrypoint.test.js @@ -0,0 +1,43 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createRequire } from "node:module"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const require = createRequire(import.meta.url); +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); + +test("package root resolves to a callable plugin factory for require loaders", async () => { + const pluginModule = require(repoRoot); + + assert.equal(typeof pluginModule, "function"); + assert.equal(pluginModule, pluginModule.default); + assert.equal(pluginModule, pluginModule.WorktreeWorkflowPlugin); + + const plugin = await pluginModule({ + $: Object.assign( + () => ({ + cwd() { + return this; + }, + quiet() { + return this; + }, + async nothrow() { + return { + text() { + return ""; + }, + stderr: Buffer.from(""), + exitCode: 0, + }; + }, + }), + { escape(value) { return JSON.stringify(String(value)); } }, + ), + directory: repoRoot, + }); + + assert.equal(typeof plugin, "object"); + assert.equal(typeof plugin.hooks["tool.execute.before"], "function"); +});