@karmaniverous/get-dotenv 5.2.6 → 6.0.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +106 -70
  2. package/dist/cliHost.d.ts +232 -226
  3. package/dist/cliHost.mjs +777 -545
  4. package/dist/config.d.ts +7 -2
  5. package/dist/env-overlay.d.ts +21 -9
  6. package/dist/env-overlay.mjs +14 -19
  7. package/dist/getdotenv.cli.mjs +1366 -1163
  8. package/dist/index.d.ts +415 -242
  9. package/dist/index.mjs +1364 -1414
  10. package/dist/plugins-aws.d.ts +149 -94
  11. package/dist/plugins-aws.mjs +307 -195
  12. package/dist/plugins-batch.d.ts +153 -99
  13. package/dist/plugins-batch.mjs +277 -95
  14. package/dist/plugins-cmd.d.ts +140 -94
  15. package/dist/plugins-cmd.mjs +636 -502
  16. package/dist/plugins-demo.d.ts +140 -94
  17. package/dist/plugins-demo.mjs +237 -46
  18. package/dist/plugins-init.d.ts +140 -94
  19. package/dist/plugins-init.mjs +129 -12
  20. package/dist/plugins.d.ts +166 -103
  21. package/dist/plugins.mjs +977 -840
  22. package/package.json +15 -53
  23. package/templates/cli/ts/plugins/hello.ts +27 -6
  24. package/templates/config/js/getdotenv.config.js +1 -1
  25. package/templates/config/ts/getdotenv.config.ts +9 -2
  26. package/dist/cliHost.cjs +0 -1875
  27. package/dist/cliHost.d.cts +0 -409
  28. package/dist/cliHost.d.mts +0 -409
  29. package/dist/config.cjs +0 -252
  30. package/dist/config.d.cts +0 -55
  31. package/dist/config.d.mts +0 -55
  32. package/dist/env-overlay.cjs +0 -163
  33. package/dist/env-overlay.d.cts +0 -50
  34. package/dist/env-overlay.d.mts +0 -50
  35. package/dist/index.cjs +0 -4140
  36. package/dist/index.d.cts +0 -457
  37. package/dist/index.d.mts +0 -457
  38. package/dist/plugins-aws.cjs +0 -667
  39. package/dist/plugins-aws.d.cts +0 -158
  40. package/dist/plugins-aws.d.mts +0 -158
  41. package/dist/plugins-batch.cjs +0 -616
  42. package/dist/plugins-batch.d.cts +0 -180
  43. package/dist/plugins-batch.d.mts +0 -180
  44. package/dist/plugins-cmd.cjs +0 -1113
  45. package/dist/plugins-cmd.d.cts +0 -178
  46. package/dist/plugins-cmd.d.mts +0 -178
  47. package/dist/plugins-demo.cjs +0 -307
  48. package/dist/plugins-demo.d.cts +0 -158
  49. package/dist/plugins-demo.d.mts +0 -158
  50. package/dist/plugins-init.cjs +0 -289
  51. package/dist/plugins-init.d.cts +0 -162
  52. package/dist/plugins-init.d.mts +0 -162
  53. package/dist/plugins.cjs +0 -2283
  54. package/dist/plugins.d.cts +0 -210
  55. package/dist/plugins.d.mts +0 -210
@@ -1,9 +1,39 @@
1
- import { Command } from 'commander';
2
- import { ZodType } from 'zod';
1
+ import { z, ZodObject } from 'zod';
2
+ import { Command, Option } from 'commander';
3
+
4
+ /**
5
+ * Scripts table shape (configurable shell type).
6
+ */
7
+ type ScriptsTable<TShell extends string | boolean = string | boolean> = Record<string, string | {
8
+ cmd: string;
9
+ shell?: TShell | undefined;
10
+ }>;
11
+
12
+ declare const getDotenvOptionsSchemaResolved: z.ZodObject<{
13
+ defaultEnv: z.ZodOptional<z.ZodString>;
14
+ dotenvToken: z.ZodOptional<z.ZodString>;
15
+ dynamicPath: z.ZodOptional<z.ZodString>;
16
+ dynamic: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
17
+ env: z.ZodOptional<z.ZodString>;
18
+ excludeDynamic: z.ZodOptional<z.ZodBoolean>;
19
+ excludeEnv: z.ZodOptional<z.ZodBoolean>;
20
+ excludeGlobal: z.ZodOptional<z.ZodBoolean>;
21
+ excludePrivate: z.ZodOptional<z.ZodBoolean>;
22
+ excludePublic: z.ZodOptional<z.ZodBoolean>;
23
+ loadProcess: z.ZodOptional<z.ZodBoolean>;
24
+ log: z.ZodOptional<z.ZodBoolean>;
25
+ logger: z.ZodOptional<z.ZodUnknown>;
26
+ outputPath: z.ZodOptional<z.ZodString>;
27
+ paths: z.ZodOptional<z.ZodArray<z.ZodString>>;
28
+ privateToken: z.ZodOptional<z.ZodString>;
29
+ vars: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodOptional<z.ZodString>>>;
30
+ }, z.core.$strip>;
3
31
 
4
32
  /**
5
33
  * A minimal representation of an environment key/value mapping.
6
- * Values may be `undefined` to represent "unset". */ type ProcessEnv = Record<string, string | undefined>;
34
+ * Values may be `undefined` to represent "unset".
35
+ */
36
+ type ProcessEnv = Record<string, string | undefined>;
7
37
  /**
8
38
  * Dynamic variable function signature. Receives the current expanded variables
9
39
  * and the selected environment (if any), and returns either a string to set
@@ -13,87 +43,86 @@ type GetDotenvDynamicFunction = (vars: ProcessEnv, env: string | undefined) => s
13
43
  type GetDotenvDynamic = Record<string, GetDotenvDynamicFunction | ReturnType<GetDotenvDynamicFunction>>;
14
44
  type Logger = Record<string, (...args: unknown[]) => void> | typeof console;
15
45
  /**
16
- * Options passed programmatically to `getDotenv`.
46
+ * Canonical programmatic options type (schema-derived).
47
+ * This type is the single source of truth for programmatic options.
17
48
  */
18
- interface GetDotenvOptions {
19
- /**
20
- * default target environment (used if `env` is not provided)
21
- */
22
- defaultEnv?: string;
49
+ type GetDotenvOptions = z.output<typeof getDotenvOptionsSchemaResolved> & {
23
50
  /**
24
- * token indicating a dotenv file
51
+ * Compile-time overlay: narrowed logger for DX (schema stores unknown).
25
52
  */
26
- dotenvToken: string;
27
- /**
28
- * path to JS/TS module default-exporting an object keyed to dynamic variable functions
29
- */
30
- dynamicPath?: string;
53
+ logger?: Logger;
31
54
  /**
32
- * Programmatic dynamic variables map. When provided, this takes precedence
33
- * over {@link GetDotenvOptions.dynamicPath}.
55
+ * Compile-time overlay: narrowed dynamic map for DX (schema stores unknown).
34
56
  */
35
57
  dynamic?: GetDotenvDynamic;
58
+ };
59
+
60
+ declare const getDotenvCliOptionsSchemaResolved: z.ZodObject<{
61
+ defaultEnv: z.ZodOptional<z.ZodString>;
62
+ dotenvToken: z.ZodOptional<z.ZodString>;
63
+ dynamicPath: z.ZodOptional<z.ZodString>;
64
+ dynamic: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
65
+ env: z.ZodOptional<z.ZodString>;
66
+ excludeDynamic: z.ZodOptional<z.ZodBoolean>;
67
+ excludeEnv: z.ZodOptional<z.ZodBoolean>;
68
+ excludeGlobal: z.ZodOptional<z.ZodBoolean>;
69
+ excludePrivate: z.ZodOptional<z.ZodBoolean>;
70
+ excludePublic: z.ZodOptional<z.ZodBoolean>;
71
+ loadProcess: z.ZodOptional<z.ZodBoolean>;
72
+ log: z.ZodOptional<z.ZodBoolean>;
73
+ logger: z.ZodOptional<z.ZodUnknown>;
74
+ outputPath: z.ZodOptional<z.ZodString>;
75
+ privateToken: z.ZodOptional<z.ZodString>;
76
+ debug: z.ZodOptional<z.ZodBoolean>;
77
+ strict: z.ZodOptional<z.ZodBoolean>;
78
+ capture: z.ZodOptional<z.ZodBoolean>;
79
+ trace: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodArray<z.ZodString>]>>;
80
+ redact: z.ZodOptional<z.ZodBoolean>;
81
+ warnEntropy: z.ZodOptional<z.ZodBoolean>;
82
+ entropyThreshold: z.ZodOptional<z.ZodNumber>;
83
+ entropyMinLength: z.ZodOptional<z.ZodNumber>;
84
+ entropyWhitelist: z.ZodOptional<z.ZodArray<z.ZodString>>;
85
+ redactPatterns: z.ZodOptional<z.ZodArray<z.ZodString>>;
86
+ paths: z.ZodOptional<z.ZodString>;
87
+ pathsDelimiter: z.ZodOptional<z.ZodString>;
88
+ pathsDelimiterPattern: z.ZodOptional<z.ZodString>;
89
+ scripts: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
90
+ shell: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodString]>>;
91
+ vars: z.ZodOptional<z.ZodString>;
92
+ varsAssignor: z.ZodOptional<z.ZodString>;
93
+ varsAssignorPattern: z.ZodOptional<z.ZodString>;
94
+ varsDelimiter: z.ZodOptional<z.ZodString>;
95
+ varsDelimiterPattern: z.ZodOptional<z.ZodString>;
96
+ }, z.core.$strip>;
97
+
98
+ type Scripts = ScriptsTable;
99
+ /**
100
+ * Canonical CLI options type derived from the Zod schema output.
101
+ * Includes CLI-only flags (debug/strict/capture/trace/redaction/entropy),
102
+ * stringly paths/vars, and inherited programmatic fields (minus normalized
103
+ * shapes that are handled by resolution).
104
+ */
105
+ type GetDotenvCliOptions = z.output<typeof getDotenvCliOptionsSchemaResolved> & {
36
106
  /**
37
- * target environment
38
- */
39
- env?: string;
40
- /**
41
- * exclude dynamic variables from loading
42
- */
43
- excludeDynamic?: boolean;
44
- /**
45
- * exclude environment-specific variables from loading
46
- */
47
- excludeEnv?: boolean;
48
- /**
49
- * exclude global variables from loading
50
- */
51
- excludeGlobal?: boolean;
52
- /**
53
- * exclude private variables from loading
54
- */
55
- excludePrivate?: boolean;
56
- /**
57
- * exclude public variables from loading
58
- */
59
- excludePublic?: boolean;
60
- /**
61
- * load dotenv variables to `process.env`
62
- */
63
- loadProcess?: boolean;
64
- /**
65
- * log loaded dotenv variables to `logger`
66
- */
67
- log?: boolean;
68
- /**
69
- * logger object (defaults to console)
107
+ * Compile-only overlay for DX: logger narrowed from unknown.
70
108
  */
71
109
  logger?: Logger;
72
110
  /**
73
- * if populated, writes consolidated dotenv file to this path (follows dotenvExpand rules)
74
- */
75
- outputPath?: string;
76
- /**
77
- * array of input directory paths
78
- */
79
- paths?: string[];
80
- /**
81
- * filename token indicating private variables
82
- */
83
- privateToken?: string;
84
- /**
85
- * explicit variables to include
111
+ * Compile-only overlay for DX: scripts narrowed from Record\<string, unknown\>.
86
112
  */
87
- vars?: ProcessEnv;
88
- /**
89
- * Reserved: config loader flag (no-op).
90
- * The plugin-first host and generator paths already use the config
91
- * loader/overlay pipeline unconditionally (no-op when no config files
92
- * are present). This flag is accepted for forward compatibility but
93
- * currently has no effect.
94
- */
95
- useConfigLoader?: boolean;
96
- }
113
+ scripts?: Scripts;
114
+ };
115
+
116
+ type ResolvedHelpConfig = Partial<GetDotenvCliOptions> & {
117
+ plugins: Record<string, unknown>;
118
+ };
119
+ /** Per-invocation context shared with plugins and actions. */
120
+ type GetDotenvCliCtx<TOptions extends GetDotenvOptions = GetDotenvOptions> = {
121
+ optionsResolved: TOptions;
122
+ dotenv: ProcessEnv;
123
+ plugins?: Record<string, unknown>;
124
+ pluginConfigs?: Record<string, unknown>;
125
+ };
97
126
 
98
127
  /** src/cliHost/definePlugin.ts
99
128
  * Plugin contracts for the GetDotenv CLI host.
@@ -111,52 +140,69 @@ interface GetDotenvOptions {
111
140
  * Purpose: remove nominal class identity (private fields) from the plugin seam
112
141
  * to avoid TS2379 under exactOptionalPropertyTypes in downstream consumers.
113
142
  */
114
- type GetDotenvCliPublic<TOptions extends GetDotenvOptions = GetDotenvOptions> = Command & {
115
- ns: (name: string) => Command;
116
- getCtx: () => GetDotenvCliCtx<TOptions> | undefined;
117
- resolveAndLoad: (customOptions?: Partial<TOptions>) => Promise<GetDotenvCliCtx<TOptions>>;
118
- };
143
+ interface GetDotenvCliPublic<TOptions extends GetDotenvOptions = GetDotenvOptions> extends Command {
144
+ ns(name: string): Command;
145
+ getCtx(): GetDotenvCliCtx<TOptions> | undefined;
146
+ resolveAndLoad(customOptions?: Partial<TOptions>, opts?: {
147
+ runAfterResolve?: boolean;
148
+ }): Promise<GetDotenvCliCtx<TOptions>>;
149
+ setOptionGroup(opt: Option, group: string): void;
150
+ /**
151
+ * Create a dynamic option whose description is computed at help time
152
+ * from the resolved configuration.
153
+ */
154
+ createDynamicOption(flags: string, desc: (cfg: ResolvedHelpConfig & {
155
+ plugins: Record<string, unknown>;
156
+ }) => string, parser?: (value: string, previous?: unknown) => unknown, defaultValue?: unknown): Option;
157
+ createDynamicOption<TValue = unknown>(flags: string, desc: (cfg: ResolvedHelpConfig & {
158
+ plugins: Record<string, unknown>;
159
+ }) => string, parser: (value: string, previous?: TValue) => TValue, defaultValue?: TValue): Option;
160
+ }
119
161
  /** Public plugin contract used by the GetDotenv CLI host. */
120
- interface GetDotenvCliPlugin {
162
+ interface GetDotenvCliPlugin<TOptions extends GetDotenvOptions = GetDotenvOptions> {
121
163
  id?: string;
122
164
  /**
123
165
  * Setup phase: register commands and wiring on the provided CLI instance.
124
166
  * Runs parent → children (pre-order).
125
167
  */
126
- setup: (cli: GetDotenvCliPublic) => void | Promise<void>;
168
+ setup: (cli: GetDotenvCliPublic<TOptions>) => void | Promise<void>;
127
169
  /**
128
170
  * After the dotenv context is resolved, initialize any clients/secrets
129
171
  * or attach per-plugin state under ctx.plugins (by convention).
130
172
  * Runs parent → children (pre-order).
131
173
  */
132
- afterResolve?: (cli: GetDotenvCliPublic, ctx: GetDotenvCliCtx) => void | Promise<void>;
174
+ afterResolve?: (cli: GetDotenvCliPublic<TOptions>, ctx: GetDotenvCliCtx<TOptions>) => void | Promise<void>;
133
175
  /**
134
- * Optional Zod schema for this plugin's config slice (from config.plugins[id]).
135
- * When provided, the host validates the merged config under the guarded loader path.
176
+ * Zod schema for this plugin's config slice (from config.plugins[id]).
177
+ * Enforced object-like (ZodObject) to simplify code paths and inference.
136
178
  */
137
- configSchema?: ZodType;
179
+ configSchema?: ZodObject;
138
180
  /**
139
181
  * Compositional children. Installed after the parent per pre-order.
140
182
  */
141
- children: GetDotenvCliPlugin[];
183
+ children: GetDotenvCliPlugin<TOptions>[];
142
184
  /**
143
185
  * Compose a child plugin. Returns the parent to enable chaining.
144
186
  */
145
- use: (child: GetDotenvCliPlugin) => GetDotenvCliPlugin;
187
+ use: (child: GetDotenvCliPlugin<TOptions>) => GetDotenvCliPlugin<TOptions>;
146
188
  }
147
-
148
- /** * Per-invocation context shared with plugins and actions. */
149
- type GetDotenvCliCtx<TOptions extends GetDotenvOptions = GetDotenvOptions> = {
150
- optionsResolved: TOptions;
151
- dotenv: ProcessEnv;
152
- plugins?: Record<string, unknown>;
153
- pluginConfigs?: Record<string, unknown>;
189
+ /**
190
+ * Compile-time helper type: the plugin object returned by definePlugin always
191
+ * includes the instance-bound helpers as required members. Keeping the public
192
+ * interface optional preserves compatibility for ad-hoc/test plugins, while
193
+ * return types from definePlugin provide stronger DX for shipped/typed plugins.
194
+ */
195
+ type PluginWithInstanceHelpers<TOptions extends GetDotenvOptions = GetDotenvOptions, TConfig = unknown> = GetDotenvCliPlugin<TOptions> & {
196
+ readConfig<TCfg = TConfig>(cli: GetDotenvCliPublic<TOptions>): Readonly<TCfg>;
197
+ createPluginDynamicOption<TCfg = TConfig>(cli: GetDotenvCliPublic<TOptions>, flags: string, desc: (cfg: ResolvedHelpConfig & {
198
+ plugins: Record<string, unknown>;
199
+ }, pluginCfg: Readonly<TCfg>) => string, parser?: (value: string, previous?: unknown) => unknown, defaultValue?: unknown): Option;
154
200
  };
155
201
 
156
202
  type InitPluginOptions = {
157
203
  logger?: Logger;
158
204
  };
159
- declare const initPlugin: (opts?: InitPluginOptions) => GetDotenvCliPlugin;
205
+ declare const initPlugin: (opts?: InitPluginOptions) => PluginWithInstanceHelpers<GetDotenvOptions, {}>;
160
206
 
161
207
  export { initPlugin };
162
208
  export type { InitPluginOptions };
@@ -2,6 +2,90 @@ import { stdin, stdout } from 'node:process';
2
2
  import fs from 'fs-extra';
3
3
  import path from 'path';
4
4
  import { createInterface } from 'readline/promises';
5
+ import { z } from 'zod';
6
+ import 'package-directory';
7
+ import 'url';
8
+ import 'yaml';
9
+ import 'nanoid';
10
+ import 'dotenv';
11
+ import 'crypto';
12
+
13
+ /**
14
+ * Zod schemas for configuration files discovered by the new loader.
15
+ *
16
+ * Notes:
17
+ * - RAW: all fields optional; shapes are stringly-friendly (paths may be string[] or string).
18
+ * - RESOLVED: normalized shapes (paths always string[]).
19
+ * - For JSON/YAML configs, the loader rejects "dynamic" and "schema" (JS/TS-only).
20
+ */
21
+ // String-only env value map
22
+ const stringMap = z.record(z.string(), z.string());
23
+ const envStringMap = z.record(z.string(), stringMap);
24
+ // Allow string[] or single string for "paths" in RAW; normalize later.
25
+ const rawPathsSchema = z.union([z.array(z.string()), z.string()]).optional();
26
+ const getDotenvConfigSchemaRaw = z.object({
27
+ dotenvToken: z.string().optional(),
28
+ privateToken: z.string().optional(),
29
+ paths: rawPathsSchema,
30
+ loadProcess: z.boolean().optional(),
31
+ log: z.boolean().optional(),
32
+ shell: z.union([z.string(), z.boolean()]).optional(),
33
+ scripts: z.record(z.string(), z.unknown()).optional(), // Scripts validation left wide; generator validates elsewhere
34
+ requiredKeys: z.array(z.string()).optional(),
35
+ schema: z.unknown().optional(), // JS/TS-only; loader rejects in JSON/YAML
36
+ vars: stringMap.optional(), // public, global
37
+ envVars: envStringMap.optional(), // public, per-env
38
+ // Dynamic in config (JS/TS only). JSON/YAML loader will reject if set.
39
+ dynamic: z.unknown().optional(),
40
+ // Per-plugin config bag; validated by plugins/host when used.
41
+ plugins: z.record(z.string(), z.unknown()).optional(),
42
+ });
43
+ // Normalize paths to string[]
44
+ const normalizePaths = (p) => p === undefined ? undefined : Array.isArray(p) ? p : [p];
45
+ getDotenvConfigSchemaRaw.transform((raw) => ({
46
+ ...raw,
47
+ paths: normalizePaths(raw.paths),
48
+ }));
49
+
50
+ /**
51
+ * Zod schemas for programmatic GetDotenv options.
52
+ *
53
+ * Canonical source of truth for options shape. Public types are derived
54
+ * from these schemas (see consumers via z.output\<\>).
55
+ */
56
+ // Minimal process env representation: string values or undefined to indicate "unset".
57
+ const processEnvSchema = z.record(z.string(), z.string().optional());
58
+ // RAW: all fields optional — undefined means "inherit" from lower layers.
59
+ z.object({
60
+ defaultEnv: z.string().optional(),
61
+ dotenvToken: z.string().optional(),
62
+ dynamicPath: z.string().optional(),
63
+ // Dynamic map is intentionally wide for now; refine once sources are normalized.
64
+ dynamic: z.record(z.string(), z.unknown()).optional(),
65
+ env: z.string().optional(),
66
+ excludeDynamic: z.boolean().optional(),
67
+ excludeEnv: z.boolean().optional(),
68
+ excludeGlobal: z.boolean().optional(),
69
+ excludePrivate: z.boolean().optional(),
70
+ excludePublic: z.boolean().optional(),
71
+ loadProcess: z.boolean().optional(),
72
+ log: z.boolean().optional(),
73
+ logger: z.unknown().optional(),
74
+ outputPath: z.string().optional(),
75
+ paths: z.array(z.string()).optional(),
76
+ privateToken: z.string().optional(),
77
+ vars: processEnvSchema.optional(),
78
+ });
79
+
80
+ /**
81
+ * Instance-bound plugin config store.
82
+ * Host stores the validated/interpolated slice per plugin instance.
83
+ * The store is intentionally private to this module; definePlugin()
84
+ * provides a typed accessor that reads from this store for the calling
85
+ * plugin instance.
86
+ */
87
+ const PLUGIN_CONFIG_STORE = new WeakMap();
88
+ const _getPluginConfigForInstance = (plugin) => PLUGIN_CONFIG_STORE.get(plugin);
5
89
 
6
90
  /** src/cliHost/definePlugin.ts
7
91
  * Plugin contracts for the GetDotenv CLI host.
@@ -10,26 +94,59 @@ import { createInterface } from 'readline/promises';
10
94
  * should use (GetDotenvCliPublic). Using a structural type at the seam avoids
11
95
  * nominal class identity issues (private fields) in downstream consumers.
12
96
  */
13
- /**
14
- * Define a GetDotenv CLI plugin with compositional helpers.
15
- *
16
- * @example
17
- * const parent = definePlugin(\{ id: 'p', setup(cli) \{ /* ... *\/ \} \})
18
- * .use(childA)
19
- * .use(childB);
20
- */
21
- const definePlugin = (spec) => {
97
+ /* eslint-disable tsdoc/syntax */
98
+ function definePlugin(spec) {
22
99
  const { children = [], ...rest } = spec;
23
- const plugin = {
100
+ // Default to a strict empty-object schema so “no-config” plugins fail fast
101
+ // on unknown keys and provide a concrete {} at runtime.
102
+ const effectiveSchema = spec.configSchema ?? z.object({}).strict();
103
+ // Build base plugin first, then extend with instance-bound helpers.
104
+ const base = {
24
105
  ...rest,
106
+ // Always carry a schema (strict empty by default) to simplify host logic
107
+ // and improve inference/ergonomics for plugin authors.
108
+ configSchema: effectiveSchema,
25
109
  children: [...children],
26
110
  use(child) {
27
111
  this.children.push(child);
28
112
  return this;
29
113
  },
30
114
  };
31
- return plugin;
32
- };
115
+ // Attach instance-bound helpers on the returned plugin object.
116
+ const extended = base;
117
+ extended.readConfig = function (_cli) {
118
+ // Config is stored per-plugin-instance by the host (WeakMap in computeContext).
119
+ const value = _getPluginConfigForInstance(extended);
120
+ if (value === undefined) {
121
+ // Guard: host has not resolved config yet (incorrect lifecycle usage).
122
+ throw new Error('Plugin config not available. Ensure resolveAndLoad() has been called before readConfig().');
123
+ }
124
+ return value;
125
+ };
126
+ // Plugin-bound dynamic option factory
127
+ extended.createPluginDynamicOption = function (cli, flags, desc, parser, defaultValue) {
128
+ return cli.createDynamicOption(flags, (cfg) => {
129
+ // Prefer the validated slice stored per instance; fallback to help-bag
130
+ // (by-id) so top-level `-h` can render effective defaults before resolve.
131
+ const fromStore = _getPluginConfigForInstance(extended);
132
+ const id = extended.id;
133
+ let fromBag;
134
+ if (!fromStore && id) {
135
+ const maybe = cfg.plugins[id];
136
+ if (maybe && typeof maybe === 'object') {
137
+ fromBag = maybe;
138
+ }
139
+ }
140
+ // Always provide a concrete object to dynamic callbacks:
141
+ // - With a schema: computeContext stores the parsed object.
142
+ // - Without a schema: computeContext stores {}.
143
+ // - Help-time fallback: coalesce to {} when only a by-id bag exists.
144
+ const cfgVal = (fromStore ?? fromBag ?? {});
145
+ return desc(cfg, cfgVal);
146
+ }, parser, defaultValue);
147
+ };
148
+ return extended;
149
+ }
33
150
 
34
151
  const ensureDir = async (p) => {
35
152
  await fs.ensureDir(p);