|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +import * as fs from 'node:fs'; |
| 7 | +import * as path from 'node:path'; |
| 8 | +import * as os from 'node:os'; |
| 9 | +import type {Logger} from '../logging/types.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Hook names that the plugin system supports. |
| 13 | + */ |
| 14 | +const SUPPORTED_HOOKS = ['b2c:config-sources', 'b2c:http-middleware', 'b2c:auth-middleware'] as const; |
| 15 | + |
| 16 | +export type SupportedHookName = (typeof SUPPORTED_HOOKS)[number]; |
| 17 | + |
| 18 | +/** |
| 19 | + * A discovered plugin with its hook file paths. |
| 20 | + */ |
| 21 | +export interface DiscoveredPlugin { |
| 22 | + /** Plugin package name */ |
| 23 | + name: string; |
| 24 | + /** Absolute path to the plugin's package directory */ |
| 25 | + packageDir: string; |
| 26 | + /** Map of hook name to relative file path(s) within the plugin package */ |
| 27 | + hooks: Partial<Record<SupportedHookName, string[]>>; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Options for plugin discovery. |
| 32 | + */ |
| 33 | +export interface PluginDiscoveryOptions { |
| 34 | + /** Override the oclif data directory (for testing) */ |
| 35 | + dataDir?: string; |
| 36 | + /** Override the dirname used to resolve the data directory (default: 'b2c') */ |
| 37 | + dirname?: string; |
| 38 | + /** Logger for warnings */ |
| 39 | + logger?: Logger; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Resolves the oclif data directory for the CLI. |
| 44 | + * |
| 45 | + * Cross-platform: uses `$XDG_DATA_HOME/<dirname>` or `~/.local/share/<dirname>` |
| 46 | + * on POSIX; `$LOCALAPPDATA\<dirname>` on Windows. |
| 47 | + */ |
| 48 | +export function resolveOclifDataDir(dirname = 'b2c'): string { |
| 49 | + if (process.platform === 'win32') { |
| 50 | + const localAppData = process.env.LOCALAPPDATA ?? path.join(os.homedir(), 'AppData', 'Local'); |
| 51 | + return path.join(localAppData, dirname); |
| 52 | + } |
| 53 | + |
| 54 | + const xdgDataHome = process.env.XDG_DATA_HOME ?? path.join(os.homedir(), '.local', 'share'); |
| 55 | + return path.join(xdgDataHome, dirname); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Reads a JSON file, returning undefined on any error. |
| 60 | + */ |
| 61 | +function readJsonSafe(filePath: string): unknown { |
| 62 | + try { |
| 63 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 64 | + return JSON.parse(content); |
| 65 | + } catch { |
| 66 | + return undefined; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Normalizes a hook value (string or string[]) to a string array. |
| 72 | + */ |
| 73 | +function normalizeHookPaths(value: unknown): string[] { |
| 74 | + if (typeof value === 'string') return [value]; |
| 75 | + if (Array.isArray(value)) return value.filter((v): v is string => typeof v === 'string'); |
| 76 | + return []; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Extracts the plugin name from an oclif plugins entry. |
| 81 | + * |
| 82 | + * Oclif stores user-installed plugins as objects: `{name, type, url}`, |
| 83 | + * while linked/dev plugins may appear as plain strings. |
| 84 | + */ |
| 85 | +function resolvePluginName(entry: unknown): string | undefined { |
| 86 | + if (typeof entry === 'string') return entry; |
| 87 | + if (typeof entry === 'object' && entry !== null && 'name' in entry) { |
| 88 | + const name = (entry as {name: unknown}).name; |
| 89 | + if (typeof name === 'string') return name; |
| 90 | + } |
| 91 | + return undefined; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Discovers installed b2c-cli plugins by reading the oclif data directory. |
| 96 | + * |
| 97 | + * Reads `<dataDir>/package.json` -> `oclif.plugins` array -> each plugin's |
| 98 | + * `package.json` -> `oclif.hooks` -> returns `DiscoveredPlugin[]`. |
| 99 | + * |
| 100 | + * Only hooks matching `b2c:config-sources`, `b2c:http-middleware`, and |
| 101 | + * `b2c:auth-middleware` are included. |
| 102 | + */ |
| 103 | +export function discoverPlugins(options: PluginDiscoveryOptions = {}): DiscoveredPlugin[] { |
| 104 | + const {logger} = options; |
| 105 | + const dataDir = options.dataDir ?? resolveOclifDataDir(options.dirname); |
| 106 | + const plugins: DiscoveredPlugin[] = []; |
| 107 | + |
| 108 | + // Read the root package.json in the data directory |
| 109 | + const rootPkgPath = path.join(dataDir, 'package.json'); |
| 110 | + const rootPkg = readJsonSafe(rootPkgPath) as {oclif?: {plugins?: unknown[]}} | undefined; |
| 111 | + if (!rootPkg?.oclif?.plugins?.length) { |
| 112 | + return plugins; |
| 113 | + } |
| 114 | + |
| 115 | + const nodeModulesDir = path.join(dataDir, 'node_modules'); |
| 116 | + |
| 117 | + for (const pluginEntry of rootPkg.oclif.plugins) { |
| 118 | + const pluginName = resolvePluginName(pluginEntry); |
| 119 | + if (!pluginName) continue; |
| 120 | + |
| 121 | + try { |
| 122 | + const pluginDir = path.join(nodeModulesDir, ...pluginName.split('/')); |
| 123 | + const pluginPkgPath = path.join(pluginDir, 'package.json'); |
| 124 | + const pluginPkg = readJsonSafe(pluginPkgPath) as {oclif?: {hooks?: Record<string, unknown>}} | undefined; |
| 125 | + |
| 126 | + if (!pluginPkg?.oclif?.hooks) continue; |
| 127 | + |
| 128 | + const hookEntries = pluginPkg.oclif.hooks; |
| 129 | + const discoveredHooks: Partial<Record<SupportedHookName, string[]>> = {}; |
| 130 | + let hasHooks = false; |
| 131 | + |
| 132 | + for (const hookName of SUPPORTED_HOOKS) { |
| 133 | + if (hookName in hookEntries) { |
| 134 | + const paths = normalizeHookPaths(hookEntries[hookName]); |
| 135 | + if (paths.length > 0) { |
| 136 | + discoveredHooks[hookName] = paths; |
| 137 | + hasHooks = true; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (hasHooks) { |
| 143 | + plugins.push({ |
| 144 | + name: pluginName, |
| 145 | + packageDir: pluginDir, |
| 146 | + hooks: discoveredHooks, |
| 147 | + }); |
| 148 | + } |
| 149 | + } catch (err) { |
| 150 | + const message = err instanceof Error ? err.message : String(err); |
| 151 | + logger?.warn(`Failed to discover plugin ${pluginName}: ${message}`); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return plugins; |
| 156 | +} |
0 commit comments