From da3ba50f9c0ac6add38aaa247faf10f17156a78c Mon Sep 17 00:00:00 2001 From: Timothy Jones Date: Sat, 15 Jun 2024 14:30:41 +1000 Subject: [PATCH] chore: Expose CORE_PLUGIN_PREFIX as a constant --- packages/case-core-plugin-http/src/index.ts | 10 ++++++++-- .../case-core/src/diffmatch/plugins/loadPlugin.ts | 13 +++++++------ packages/case-plugin-base/src/corePlugins.ts | 12 ++++++++++++ packages/case-plugin-base/src/index.ts | 1 + 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 packages/case-plugin-base/src/corePlugins.ts diff --git a/packages/case-core-plugin-http/src/index.ts b/packages/case-core-plugin-http/src/index.ts index 5ed48a72..480e78df 100644 --- a/packages/case-core-plugin-http/src/index.ts +++ b/packages/case-core-plugin-http/src/index.ts @@ -17,7 +17,10 @@ import { MOCK_HTTP_SERVER, URL_ENCODED_STRING_TYPE, } from '@contract-case/case-core-plugin-http-dsl'; -import { ContractCasePlugin } from '@contract-case/case-plugin-base'; +import { + CORE_PLUGIN_PREFIX, + ContractCasePlugin, +} from '@contract-case/case-plugin-base'; import { HttpBasicAuthMatcher, HttpRequestMatcher, @@ -39,7 +42,10 @@ const CoreHttpPlugin: ContractCasePlugin< AllHttpMockDescriptors, AllHttpMockSetupInfo > = { - name: `_CaseCore: Http / Rest Plugin`, + // Note: If using this code as an example for your own plugin, + // DO NOT start your plugin name with the core plugin prefix + // or ContractCase will not log debug information / load failures appropriately + name: `${CORE_PLUGIN_PREFIX} Http / Rest Plugin`, version: pluginVersion, matcherExecutors: { [HTTP_BASIC_AUTH_TYPE]: HttpBasicAuthMatcher, diff --git a/packages/case-core/src/diffmatch/plugins/loadPlugin.ts b/packages/case-core/src/diffmatch/plugins/loadPlugin.ts index 8ac63be9..f39a4483 100644 --- a/packages/case-core/src/diffmatch/plugins/loadPlugin.ts +++ b/packages/case-core/src/diffmatch/plugins/loadPlugin.ts @@ -6,6 +6,7 @@ import { IsCaseNodeForType, LogContext, MockExecutorFn, + CORE_PLUGIN_PREFIX, } from '@contract-case/case-plugin-base'; import { caseVersion } from '../../entities/caseVersion'; @@ -28,7 +29,7 @@ export const loadPlugin = < if (loadedPluginVersions[plugin.name] != null) { if (plugin.version !== loadedPluginVersions[plugin.name]) { if ( - plugin.name.startsWith(`_CaseCore:`) && + plugin.name.startsWith(CORE_PLUGIN_PREFIX) && plugin.version !== caseVersion ) { throw new CaseCoreError( @@ -40,15 +41,15 @@ export const loadPlugin = < ); } context.logger.deepMaintainerDebug( - `Plugin '${plugin.name}' at version '${plugin.version}' has been previously loaded, skipping load of mock executors`, + `Plugin '${plugin.name}' at version '${plugin.version}' has been previously loaded, skipping`, ); return; } - if (plugin.name.startsWith(`_CaseCore:`)) { + if (plugin.name.startsWith(CORE_PLUGIN_PREFIX)) { context.logger.deepMaintainerDebug(`Loading core plugin '${plugin.name}'`); } else { context.logger.debug( - `Loading mock definitions for plugin '${plugin.name}' version ${plugin.version}`, + `Loading plugin '${plugin.name}' version ${plugin.version}`, ); } @@ -77,8 +78,8 @@ export const loadPlugin = < unknown, '_case:MockHttpServer' >; - typeToPluginName[mockType] = plugin.name; - loadedPluginVersions[plugin.name] = plugin.version; }); + + loadedPluginVersions[plugin.name] = plugin.version; }; diff --git a/packages/case-plugin-base/src/corePlugins.ts b/packages/case-plugin-base/src/corePlugins.ts new file mode 100644 index 00000000..e6371068 --- /dev/null +++ b/packages/case-plugin-base/src/corePlugins.ts @@ -0,0 +1,12 @@ +/** + * DO NOT USE THIS IN YOUR OWN PLUGINS + * + * The prefix for ContractCase core plugin names. Plugin names with + * this prefix are treated as always loaded, log less debug information, + * and any errors in loading are treated as core crashes rather than user + * configuration errors. + * + * Other than the way logs and load failures are treated, there's no special + * treatment given to core plugins, + */ +export const CORE_PLUGIN_PREFIX = '_CaseCore:' as const; diff --git a/packages/case-plugin-base/src/index.ts b/packages/case-plugin-base/src/index.ts index 8e635b0a..0a20eb0b 100644 --- a/packages/case-plugin-base/src/index.ts +++ b/packages/case-plugin-base/src/index.ts @@ -6,6 +6,7 @@ export * from './context'; // export * from './core'; +export * from './corePlugins'; export * from './errors'; export * from './logger'; export * from './matchers';