import { existsSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; const EXACT_PLUGIN = '@stim-cli/plugin-exact'; const DEFAULT_EXACT_PLUGIN_VERSION = '1.0.0'; /** * Where the Exact golden takes the Exact plugin from: the copy the Stim package under test bundles in * `dist/plugins/exact`, an npm install of the pinned version when that Stim has `stim plugin add`, or the built-in * Exact support of a Stim that has neither. */ export function exactPluginSource({ stimPackage, pluginHelp, pinned }) { const bundled = join(stimPackage, 'dist', 'plugins', 'exact', 'package.json'); if (existsSync(bundled)) return { package: EXACT_PLUGIN, source: 'bundled', version: JSON.parse(readFileSync(bundled, 'utf8')).version }; if (/^\s+add\b/m.test(pluginHelp)) return { package: EXACT_PLUGIN, source: 'npm', version: pinned || DEFAULT_EXACT_PLUGIN_VERSION }; return { package: EXACT_PLUGIN, source: 'builtin', version: null }; } /** The `plugins` setting a golden home needs for `plugin`. */ export function exactPluginSetting(plugin) { if (plugin.source === 'bundled') return ['exact']; if (plugin.source === 'npm') return [`${plugin.package}@${plugin.version}`]; return []; } function readJson(path) { try { return JSON.parse(readFileSync(path, 'utf8')); } catch { return null; } } /** `plugin` as the golden home at `home` holds it: with its install integrity for an npm install, null when missing. */ export function goldenExactPlugin(home, plugin) { if (plugin.source !== 'npm') return plugin; const record = readJson( join(home, 'plugins', `${plugin.package.replace('/', '+')}@${plugin.version}`, 'install.json'), ); return typeof record?.integrity === 'string' ? { ...plugin, integrity: record.integrity } : null; } /** * Why the golden at `directory` no longer runs the Exact plugin `READY.json` recorded, or null when it does. `plugin` * is the source this dispatch selects. */ export function exactPluginMismatch(directory, recorded, plugin) { const home = join(directory, 'golden-stim-home'); const current = goldenExactPlugin(home, plugin); const plugins = readJson(join(home, 'config.json'))?.plugins ?? []; const problem = !recorded ? 'READY.json records no Exact plugin' : !current ? `the golden home has no install of ${plugin.package}@${plugin.version}` : JSON.stringify(current) !== JSON.stringify(recorded) ? `READY.json records ${JSON.stringify(recorded)}, this dispatch selects ${JSON.stringify(current)}` : JSON.stringify(plugins) !== JSON.stringify(exactPluginSetting(current)) ? `the golden home's plugins setting is ${JSON.stringify(plugins)}, not ${JSON.stringify(exactPluginSetting(current))}` : null; return ( problem && `Native golden pin mismatch: exactPlugin (${problem}). Prepare the golden again: delete ${join(directory, 'READY.json')} and ${home}, then run prepare.` ); }