diff --git a/packages/aws-cdk/lib/cli/cli-config.ts b/packages/aws-cdk/lib/cli/cli-config.ts index 3ea32e677..4947cc435 100644 --- a/packages/aws-cdk/lib/cli/cli-config.ts +++ b/packages/aws-cdk/lib/cli/cli-config.ts @@ -20,6 +20,7 @@ export async function makeConfig(): Promise { 'app': { type: 'string', alias: 'a', desc: 'REQUIRED WHEN RUNNING APP: command-line for executing your app or a cloud assembly directory (e.g. "node bin/my-app.js"). Can also be specified in cdk.json or ~/.cdk.json', requiresArg: true }, 'build': { type: 'string', desc: 'Command-line for a pre-synth build' }, 'context': { type: 'array', alias: 'c', desc: 'Add contextual string parameter (KEY=VALUE)' }, + 'context-file': { type: 'string', desc: 'Add contextual file path parameter' }, 'plugin': { type: 'array', alias: 'p', desc: 'Name or path of a node package that extend the CDK features. Can be specified multiple times' }, 'trace': { type: 'boolean', desc: 'Print trace for stack warnings' }, 'strict': { type: 'boolean', desc: 'Do not construct stacks with warnings' }, diff --git a/packages/aws-cdk/lib/cli/convert-to-user-input.ts b/packages/aws-cdk/lib/cli/convert-to-user-input.ts index 4edbfcb2e..ab70c87e7 100644 --- a/packages/aws-cdk/lib/cli/convert-to-user-input.ts +++ b/packages/aws-cdk/lib/cli/convert-to-user-input.ts @@ -12,6 +12,7 @@ export function convertYargsToUserInput(args: any): UserInput { app: args.app, build: args.build, context: args.context, + contextFile: args.contextFile, plugin: args.plugin, trace: args.trace, strict: args.strict, @@ -291,6 +292,7 @@ export function convertConfigToUserInput(config: any): UserInput { app: config.app, build: config.build, context: config.context, + contextFile: config.contextFile, plugin: config.plugin, trace: config.trace, strict: config.strict, diff --git a/packages/aws-cdk/lib/cli/parse-command-line-arguments.ts b/packages/aws-cdk/lib/cli/parse-command-line-arguments.ts index 9f9d428a1..3ce195361 100644 --- a/packages/aws-cdk/lib/cli/parse-command-line-arguments.ts +++ b/packages/aws-cdk/lib/cli/parse-command-line-arguments.ts @@ -30,6 +30,11 @@ export function parseCommandLineArguments(args: Array): any { nargs: 1, requiresArg: true, }) + .option('context-file', { + default: undefined, + type: 'string', + desc: 'Add contextual file path parameter', + }) .option('plugin', { type: 'array', alias: 'p', diff --git a/packages/aws-cdk/lib/cli/user-configuration.ts b/packages/aws-cdk/lib/cli/user-configuration.ts index 823353c25..d275d5465 100644 --- a/packages/aws-cdk/lib/cli/user-configuration.ts +++ b/packages/aws-cdk/lib/cli/user-configuration.ts @@ -2,16 +2,18 @@ import * as os from 'os'; import * as fs_path from 'path'; import { ToolkitError } from '@aws-cdk/toolkit-lib'; import * as fs from 'fs-extra'; -import { Context, PROJECT_CONTEXT } from '../api/context'; +import { Context, PROJECT_CONTEXT as DEFAULT_PROJECT_CONTEXT } from '../api/context'; import { Settings } from '../api/settings'; import type { Tag } from '../api/tags'; -import { debug, warning } from '../logging'; +import { info, debug, warning } from '../logging'; export const PROJECT_CONFIG = 'cdk.json'; -export { PROJECT_CONTEXT } from '../api/context'; export const USER_DEFAULTS = '~/.cdk.json'; const CONTEXT_KEY = 'context'; +let PROJECT_CONTEXT = DEFAULT_PROJECT_CONTEXT; +export { PROJECT_CONTEXT }; + export enum Command { LS = 'ls', LIST = 'list', @@ -122,6 +124,20 @@ export class Configuration { public async load(): Promise { const userConfig = await loadAndLog(USER_DEFAULTS); this._projectConfig = await loadAndLog(PROJECT_CONFIG); + var userProjectContextFile = this.commandLineArguments.subSettings(['contextFile']).get([]); + // Check if file exists + if ( + userProjectContextFile && + typeof userProjectContextFile === 'string' && + fs.existsSync(expandHomeDir(userProjectContextFile)) + ) { + info(`Using specified project context ${userProjectContextFile}`); + PROJECT_CONTEXT = userProjectContextFile; + } else { + warning( + `File ${userProjectContextFile} does not exist or is not a valid path. Using default: ${PROJECT_CONTEXT}`, + ); + } this._projectContext = await loadAndLog(PROJECT_CONTEXT); // @todo cannot currently be disabled by cli users @@ -278,6 +294,7 @@ export function commandLineArgumentsToSettings(argv: Arguments): Settings { build: argv.build, caBundlePath: argv.caBundlePath, context, + contextFile: argv.contextFile, debug: argv.debug, tags, language: argv.language, diff --git a/packages/aws-cdk/lib/cli/user-input.ts b/packages/aws-cdk/lib/cli/user-input.ts index 0aa41a901..523d09e79 100644 --- a/packages/aws-cdk/lib/cli/user-input.ts +++ b/packages/aws-cdk/lib/cli/user-input.ts @@ -157,6 +157,13 @@ export interface GlobalOptions { */ readonly context?: Array; + /** + * Add contextual file path parameter + * + * @default - undefined + */ + readonly contextFile?: string; + /** * Name or path of a node package that extend the CDK features. Can be specified multiple times * diff --git a/packages/aws-cdk/test/cli/cli-commands.test.ts b/packages/aws-cdk/test/cli/cli-commands.test.ts index 07cb3f891..4ab973a5b 100644 --- a/packages/aws-cdk/test/cli/cli-commands.test.ts +++ b/packages/aws-cdk/test/cli/cli-commands.test.ts @@ -5,6 +5,7 @@ import * as logging from '../../lib/logging'; jest.mock('../../lib/logging', () => ({ info: jest.fn(), debug: jest.fn(), + warning: jest.fn(), error: jest.fn(), print: jest.fn(), result: jest.fn(),