Skip to content

Commit

Permalink
add capability to loadConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgoss committed Jan 14, 2024
1 parent e7c7b6a commit 7080dfa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion exports/api/report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface IConfiguration {
export interface ILoadConfigurationOptions {
file?: string | false;
profiles?: string[];
provided?: Partial<IConfiguration>;
provided?: Partial<IConfiguration> | string;
}

// @public
Expand Down
3 changes: 2 additions & 1 deletion src/api/load_configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DEFAULT_CONFIGURATION,
fromFile,
mergeConfigurations,
parseConfiguration,
validateConfiguration,
} from '../configuration'
import { convertConfiguration } from './convert_configuration'
Expand Down Expand Up @@ -39,7 +40,7 @@ export async function loadConfiguration(
const original = mergeConfigurations(
DEFAULT_CONFIGURATION,
profileConfiguration,
options.provided
parseConfiguration(logger, 'Provided', options.provided)
)
logger.debug('Resolved configuration:', original)
validateConfiguration(original, logger)
Expand Down
9 changes: 9 additions & 0 deletions src/api/load_configuration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ describe('loadConfiguration', function () {
})
afterEach(async () => teardownEnvironment(environment))

it('should handle configuration directly provided as a string', async () => {
const { useConfiguration } = await loadConfiguration(
{ provided: `--world-parameters '{"foo":"bar"}'` },
environment
)

expect(useConfiguration.worldParameters).to.deep.eq({ foo: 'bar' })
})

it('should skip trying to resolve from a file if `file=false`', async () => {
const { useConfiguration } = await loadConfiguration(
{ file: false },
Expand Down
5 changes: 4 additions & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ export interface ILoadConfigurationOptions {
/**
* Ad-hoc configuration options to be merged over the top of whatever is
* loaded from the configuration file/profiles
* @remarks
* This can be provided as a string of argv-style arguments, though this is
* not really recommended.
*/
provided?: Partial<IConfiguration>
provided?: Partial<IConfiguration> | string
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/configuration/parse_configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { checkSchema } from './check_schema'
export function parseConfiguration(
logger: ILogger,
source: string,
definition: Partial<IConfiguration> | string
definition: Partial<IConfiguration> | string | undefined
): Partial<IConfiguration> {
if (!definition) {
return {}
}
if (typeof definition === 'string') {
logger.debug(`${source} configuration value is a string; parsing as argv`)
const { configuration } = ArgvParser.parse([
Expand Down

0 comments on commit 7080dfa

Please sign in to comment.