Skip to content

Commit aba3d72

Browse files
committed
more flexible options for config source plugins
1 parent e301330 commit aba3d72

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

docs/guide/extending.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,41 @@ export class MyCustomSource implements ConfigSource {
155155
}
156156
```
157157

158+
### Plugin Configuration
159+
160+
Plugins cannot add flags to commands they don't own (this is an oclif limitation). Instead, plugins should accept configuration via environment variables:
161+
162+
```bash
163+
# Configure the example plugin's env file location
164+
export B2C_ENV_FILE_PATH=/path/to/custom/.env.b2c
165+
b2c code deploy
166+
```
167+
168+
**Plugin authors should:**
169+
170+
1. Document supported environment variables in your plugin README
171+
2. Use sensible defaults when env vars are not set
172+
3. Access the `flags` property in hook options for future extensibility
173+
174+
**Hook Options:**
175+
176+
The hook receives a `flags` property containing all parsed CLI flags from the current command:
177+
178+
```typescript
179+
const hook: ConfigSourcesHook = async function(options) {
180+
// Access parsed flags (read-only)
181+
this.debug(`Debug mode: ${options.flags?.debug}`);
182+
183+
// Use env vars for plugin-specific configuration
184+
const customPath = process.env.MY_PLUGIN_CONFIG_PATH;
185+
186+
return {
187+
sources: [new MySource(customPath)],
188+
priority: 'after',
189+
};
190+
};
191+
```
192+
158193
### NormalizedConfig Fields
159194

160195
Your `ConfigSource` can return any of these configuration fields:

packages/b2c-plugin-example-config/src/sources/env-file-source.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ import type {ConfigSource, NormalizedConfig, ResolveConfigOptions} from '@salesf
1616
/**
1717
* ConfigSource implementation that loads from .env.b2c files.
1818
*
19-
* Supports the following environment variables:
19+
* ## Configuration
20+
*
21+
* Set `B2C_ENV_FILE_PATH` to override the default file location:
22+
*
23+
* ```bash
24+
* export B2C_ENV_FILE_PATH=/path/to/custom/.env.b2c
25+
* b2c code deploy
26+
* ```
27+
*
28+
* ## Supported Fields
29+
*
30+
* The .env.b2c file supports the following environment variables:
2031
* - HOSTNAME: B2C instance hostname
2132
* - WEBDAV_HOSTNAME: Separate WebDAV hostname (optional)
2233
* - CODE_VERSION: Code version
@@ -47,12 +58,23 @@ export class EnvFileSource implements ConfigSource {
4758
/**
4859
* Load configuration from .env.b2c file.
4960
*
61+
* File location priority:
62+
* 1. B2C_ENV_FILE_PATH environment variable (explicit override)
63+
* 2. .env.b2c in startDir (from options)
64+
* 3. .env.b2c in current working directory
65+
*
5066
* @param options - Resolution options (startDir used for file lookup)
5167
* @returns Parsed configuration or undefined if file not found
5268
*/
5369
load(options: ResolveConfigOptions): NormalizedConfig | undefined {
54-
const searchDir = options.startDir ?? process.cwd();
55-
this.envFilePath = join(searchDir, '.env.b2c');
70+
// Check for explicit path override via environment variable
71+
const envOverride = process.env.B2C_ENV_FILE_PATH;
72+
if (envOverride) {
73+
this.envFilePath = envOverride;
74+
} else {
75+
const searchDir = options.startDir ?? process.cwd();
76+
this.envFilePath = join(searchDir, '.env.b2c');
77+
}
5678

5779
if (!existsSync(this.envFilePath)) {
5880
return undefined;

packages/b2c-tooling-sdk/src/cli/base-command.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
196196
const hookOptions: ConfigSourcesHookOptions = {
197197
instance: this.flags.instance,
198198
configPath: this.flags.config,
199+
flags: this.flags as Record<string, unknown>,
199200
resolveOptions: {
200201
instance: this.flags.instance,
201202
configPath: this.flags.config,

packages/b2c-tooling-sdk/src/cli/hooks.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ export interface ConfigSourcesHookOptions {
4848
configPath?: string;
4949
/** Full ResolveConfigOptions for advanced sources that need more context */
5050
resolveOptions: ResolveConfigOptions;
51+
/**
52+
* All parsed CLI flags from the current command.
53+
*
54+
* Plugins can check for flags they care about. Note that plugins cannot
55+
* add flags to commands - they can only read flags that the CLI defines.
56+
* For plugin-specific configuration, use environment variables instead.
57+
*/
58+
flags?: Record<string, unknown>;
5159
/** Index signature for oclif hook compatibility */
5260
[key: string]: unknown;
5361
}

0 commit comments

Comments
 (0)