Project Files
src / helpers / pluginMeta.ts
import fs from "node:fs";
import path from "node:path";
export type PluginMeta = {
version: string;
owner: string;
name: string;
revisionsUrl: string;
pluginIdentifier: string;
};
function getModuleDir(): string {
try {
return __dirname;
} catch {
return process.cwd();
}
}
function readJsonFile<T = any>(filePath: string): T | null {
try {
return JSON.parse(fs.readFileSync(filePath, "utf8")) as T;
} catch {
return null;
}
}
function findUpwards(startDir: string, fileName: string, maxDepth = 8): string | null {
let dir = startDir;
for (let i = 0; i < maxDepth; i++) {
const candidate = path.join(dir, fileName);
if (fs.existsSync(candidate)) return candidate;
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
return null;
}
export function getPluginMeta(): PluginMeta {
const moduleDir = getModuleDir();
const packageJson = readJsonFile<any>(findUpwards(moduleDir, "package.json") ?? "");
const manifestJson = readJsonFile<any>(findUpwards(moduleDir, "manifest.json") ?? "");
const version = String(packageJson?.version ?? "unknown");
const owner = String(manifestJson?.owner ?? "").trim();
const name = String(manifestJson?.name ?? "").trim();
const revisionsUrl =
owner && name
? `https://raw.githubusercontent.com/${owner}/${name}-docs/main/docs/CHANGELOG.md`
: "https://raw.githubusercontent.com";
const pluginIdentifier = owner && name ? `${owner}/${name}` : name || "unknown";
return { version, owner, name, revisionsUrl, pluginIdentifier };
}
export function formatToolMetaBlock(meta: PluginMeta = getPluginMeta()): string {
return `Plugin-Identifier: ${meta.pluginIdentifier}\nPlugin version: ${meta.version}`;
}