UNPKG

@hoover-institution/hubspot-lib

Version:

A toolkit for deep integration with HubSpot's Marketing Events API with a plugin-based architecture.

66 lines (56 loc) 1.9 kB
import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; import { findUpSync } from "find-up"; /** @type {string} The absolute path to this file */ const __filename = fileURLToPath(import.meta.url); /** @type {string} The absolute directory of this file */ const __dirname = path.dirname(__filename); /** * Reads the user's nearest package.json and returns the configured plugin directory, if defined. * * @returns {string | null} Absolute path to plugin directory or null if not found */ function getPluginDir() { const pkgPath = findUpSync("package.json"); if (!pkgPath) return null; const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8")); const dir = pkg["hubspot-lib"]?.pluginDir; return dir ? path.resolve(path.dirname(pkgPath), dir) : null; } /** * Defines a plugin map like { PLUGIN_NAME: "PLUGIN_NAME" }. * Can optionally load all plugin files from the filesystem. * * @param {string[]} [names=[]] - Array of plugin names to define (manual mode) * @param {{ useDirectory?: boolean }} [options={}] - Options to control plugin loading * @returns {Record<string, string>} Map of plugin names to plugin names (identity map) * * @example * definePlugins(["CUSTOM_1", "CUSTOM_2"]); * * @example * definePlugins([], { useDirectory: true }); */ export function definePlugins(names = [], options = {}) { /** @type {Record<string, string>} */ const map = {}; if (options.useDirectory) { const pluginDir = getPluginDir(); if (!pluginDir || !fs.existsSync(pluginDir)) { console.warn("⚠️ No valid pluginDir found for definePlugins()"); return map; } const files = fs.readdirSync(pluginDir); for (const file of files) { const name = path.parse(file).name; map[name] = name; } return map; } // Manual mode for (const name of names) { map[name] = name; } return map; }