UNPKG

@hoover-institution/hubspot-lib

Version:

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

46 lines (35 loc) 1.44 kB
import { PLUGINS as BASE_PLUGINS } from "./plugins.js"; import { definePlugins } from "./definePlugin.js"; /** @type {Record<string, string> | null} */ let cachedPluginMap = null; export let fullPluginMap = null; /** * Loads and merges built-in plugins with custom plugin names or discovered plugins. * * @param {string[]} [names=[]] - List of plugin names to register manually. * @param {Object} [options={ useDirectory: true }] - Options to control plugin loading behavior. * @param {boolean} [options.useDirectory=true] - Whether to automatically load file-based plugins from disk. * @param {boolean} [options.cache=false] - Whether to cache the result of the plugin load (memoizes the first successful load). * @returns {Record<string, string>} Merged map of plugin names to their identifiers. * * @example * const PLUGINS = loadPlugins([], { useDirectory: true, cache: true }); */ export function loadPlugins(names = [], options = { useDirectory: true }) { const { useDirectory = true, cache = true } = options; if (cache && cachedPluginMap) { return cachedPluginMap; } const merged = { ...(globalThis.__PLUGINS_TEMP ?? {}), ...BASE_PLUGINS, ...definePlugins(names, { useDirectory }), }; const allBitmask = Object.values(merged).reduce((acc, bit) => acc | bit, 0); merged.ALL = "ALL"; if (cache) { cachedPluginMap = merged; } fullPluginMap = merged; return merged; }