Skip to content

Commit bd4ad74

Browse files
authored
refactor(cli): make notices display autodect part of yargs (#450)
`configuration.settings.get(['notices'])` looks like it should have the correct value already and subsequently `shouldDisplayNotices` also implies this. But the previous code had the auto-detection logical _after_ this and so the config could unexpectedly be `undefined`. This PR fixes this by moving the auto-detection logic into `yargs` default parsing. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 1d4fd87 commit bd4ad74

File tree

6 files changed

+23
-21
lines changed

6 files changed

+23
-21
lines changed

packages/@aws-cdk/user-input-gen/lib/yargs-gen.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class CliHelpers extends ExternalModule {
1414
public readonly browserForPlatform = makeCallableExpr(this, 'browserForPlatform');
1515
public readonly cliVersion = makeCallableExpr(this, 'cliVersion');
1616
public readonly isCI = makeCallableExpr(this, 'isCI');
17+
public readonly shouldDisplayNotices = makeCallableExpr(this, 'shouldDisplayNotices');
1718
public readonly yargsNegativeAlias = makeCallableExpr(this, 'yargsNegativeAlias');
1819
}
1920

@@ -24,7 +25,7 @@ function makeCallableExpr(scope: IScope, name: string) {
2425
export async function renderYargs(config: CliConfig, helpers: CliHelpers): Promise<string> {
2526
const scope = new Module('aws-cdk');
2627

27-
scope.documentation.push( '-------------------------------------------------------------------------------------------');
28+
scope.documentation.push('-------------------------------------------------------------------------------------------');
2829
scope.documentation.push(`GENERATED FROM ${SOURCE_OF_TRUTH}.`);
2930
scope.documentation.push('Do not edit by hand; all changes will be overwritten at build time from the config file.');
3031
scope.documentation.push('-------------------------------------------------------------------------------------------');

packages/aws-cdk/lib/cli/cli-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function makeConfig(): Promise<CliConfig> {
3838
'role-arn': { type: 'string', alias: 'r', desc: 'ARN of Role to use when invoking CloudFormation', default: undefined, requiresArg: true },
3939
'staging': { type: 'boolean', desc: 'Copy assets to the output directory (use --no-staging to disable the copy of assets which allows local debugging via the SAM CLI to reference the original source files)', default: true },
4040
'output': { type: 'string', alias: 'o', desc: 'Emits the synthesized cloud assembly into a directory (default: cdk.out)', requiresArg: true },
41-
'notices': { type: 'boolean', desc: 'Show relevant notices' },
41+
'notices': { type: 'boolean', desc: 'Show relevant notices', default: YARGS_HELPERS.shouldDisplayNotices() },
4242
'no-color': { type: 'boolean', desc: 'Removes colors and other style from console output', default: false },
4343
'ci': { type: 'boolean', desc: 'Force CI detection. If CI=true then logs will be sent to stdout instead of stderr', default: YARGS_HELPERS.isCI() },
4444
'unstable': { type: 'array', desc: 'Opt in to unstable features. The flag indicates that the scope and API of a feature might still change. Otherwise the feature is generally production ready and fully supported. Can be specified multiple times.', default: [] },

packages/aws-cdk/lib/cli/cli.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as cxapi from '@aws-cdk/cx-api';
22
import * as chalk from 'chalk';
33
import { CdkToolkit, AssetBuildTime } from './cdk-toolkit';
4-
import { ciSystemIsStdErrSafe } from './ci-systems';
54
import type { IoMessageLevel } from './io-host';
65
import { CliIoHost } from './io-host';
76
import { parseCommandLineArguments } from './parse-command-line-arguments';
@@ -92,19 +91,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
9291
await configuration.load();
9392

9493
const shouldDisplayNotices = configuration.settings.get(['notices']);
95-
if (shouldDisplayNotices !== undefined) {
96-
// Notices either go to stderr, or nowhere
97-
ioHost.noticesDestination = shouldDisplayNotices ? 'stderr' : 'drop';
98-
} else {
99-
// If the user didn't supply either `--notices` or `--no-notices`, we do
100-
// autodetection. The autodetection currently is: do write notices if we are
101-
// not on CI, or are on a CI system where we know that writing to stderr is
102-
// safe. We fail "closed"; that is, we decide to NOT print for unknown CI
103-
// systems, even though technically we maybe could.
104-
const safeToWriteToStdErr = !argv.ci || Boolean(ciSystemIsStdErrSafe());
105-
ioHost.noticesDestination = safeToWriteToStdErr ? 'stderr' : 'drop';
106-
}
107-
94+
ioHost.noticesDestination = shouldDisplayNotices ? 'stderr' : 'drop';
10895
const notices = Notices.create({
10996
ioHost,
11097
context: configuration.context,
@@ -160,8 +147,8 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
160147

161148
loadPlugins(configuration.settings);
162149

163-
if (typeof(cmd) !== 'string') {
164-
throw new ToolkitError(`First argument should be a string. Got: ${cmd} (${typeof(cmd)})`);
150+
if ((typeof cmd) !== 'string') {
151+
throw new ToolkitError(`First argument should be a string. Got: ${cmd} (${typeof cmd})`);
165152
}
166153

167154
try {
@@ -579,7 +566,7 @@ function determineHotswapMode(hotswap?: boolean, hotswapFallback?: boolean, watc
579566
let hotswapMode: HotswapMode;
580567
if (hotswap) {
581568
hotswapMode = HotswapMode.HOTSWAP_ONLY;
582-
/* if (hotswapFallback)*/
569+
/* if (hotswapFallback)*/
583570
} else {
584571
hotswapMode = HotswapMode.FALL_BACK;
585572
}

packages/aws-cdk/lib/cli/parse-command-line-arguments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function parseCommandLineArguments(args: Array<string>): any {
134134
requiresArg: true,
135135
})
136136
.option('notices', {
137-
default: undefined,
137+
default: helpers.shouldDisplayNotices(),
138138
type: 'boolean',
139139
desc: 'Show relevant notices',
140140
})

packages/aws-cdk/lib/cli/util/yargs-helpers.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ciSystemIsStdErrSafe } from '../ci-systems';
2+
import { isCI } from '../io-host';
13
import * as version from '../version';
24

35
export { isCI } from '../io-host';
@@ -47,3 +49,15 @@ export function browserForPlatform(): string {
4749
}
4850
}
4951

52+
/**
53+
* The default value for displaying (and refreshing) notices on all commands.
54+
*
55+
* If the user didn't supply either `--notices` or `--no-notices`, we do
56+
* autodetection. The autodetection currently is: do write notices if we are
57+
* not on CI, or are on a CI system where we know that writing to stderr is
58+
* safe. We fail "closed"; that is, we decide to NOT print for unknown CI
59+
* systems, even though technically we maybe could.
60+
*/
61+
export function shouldDisplayNotices(): boolean {
62+
return !isCI() || Boolean(ciSystemIsStdErrSafe());
63+
}

packages/aws-cdk/test/cli/cli-arguments.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('yargs', () => {
3333
lookups: true,
3434
trace: undefined,
3535
unstable: [],
36-
notices: undefined,
36+
notices: expect.any(Boolean),
3737
output: undefined,
3838
},
3939
deploy: {

0 commit comments

Comments
 (0)