UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

86 lines • 3.22 kB
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs"; import { isAbsolute, join, relative, resolve } from "node:path"; import { diagnostic } from "./diagnostics.js"; export const MANIFEST_PATH = ".codex-plugin/plugin.json"; export const DEFAULT_HOOK_PATH = "hooks/hooks.json"; export function buildPluginEnv(pluginRoot, dataRoot) { const pluginData = dataRoot === undefined ? join(pluginRoot, ".plugin-data") : resolve(dataRoot); return { CLAUDE_PLUGIN_DATA: pluginData, CLAUDE_PLUGIN_ROOT: pluginRoot, PLUGIN_DATA: pluginData, PLUGIN_ROOT: pluginRoot, }; } export function pluginSource(input) { return { discoveredAt: input.discoveredAt, displayOrder: input.displayOrder, manifestPath: input.manifestPath, pluginEnv: input.env, pluginRoot: input.pluginRoot, scope: "plugin", sourcePath: input.sourcePath, }; } export function sourceForPath(options, env, sourcePath) { const pluginRoot = resolve(options.pluginRoot); return pluginSource({ discoveredAt: options.discoveredAt ?? "pre-session", displayOrder: options.displayOrder, env, manifestPath: join(pluginRoot, MANIFEST_PATH), pluginRoot, sourcePath, }); } export function resolveContainedPath(pluginRootInput, manifestPathInput) { const pluginRoot = resolve(pluginRootInput); const normalizedInput = manifestPathInput.replaceAll("\\", "/").replace(/^\.\//, ""); const resolvedPath = isAbsolute(normalizedInput) ? resolve(normalizedInput) : resolve(pluginRoot, normalizedInput); if (!isContained(pluginRoot, resolvedPath)) { return { message: `Plugin hook path is outside plugin root: ${manifestPathInput}`, ok: false, }; } if (fileExists(resolvedPath)) { const realPath = realpathSync.native(resolvedPath); if (!isContained(realpathSync.native(pluginRoot), realPath)) { return { message: `Plugin hook path is outside plugin root: ${manifestPathInput}`, ok: false, }; } } return { ok: true, path: resolvedPath }; } export function readJsonFile(path, manifestPath, source) { try { const value = JSON.parse(readFileSync(path, "utf-8")); return { ok: true, value }; } catch (error) { if (error instanceof Error) { return { diagnostic: diagnostic({ code: "invalid_root", message: `Could not read plugin hook JSON: ${error.message}`, path: "$" }, { ...source, manifestPath }), ok: false, }; } throw error; } } export function fileExists(path) { return existsSync(path) && statSync(path).isFile(); } export function directoryExists(path) { return existsSync(path) && statSync(path).isDirectory(); } export function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function isContained(root, target) { const rel = relative(root, target); return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel)); } //# sourceMappingURL=plugin-manifest.js.map