Skip to content

Commit

Permalink
chalk support the smart way
Browse files Browse the repository at this point in the history
  • Loading branch information
HBobertz committed Jan 8, 2025
1 parent 7e45655 commit b9a8c60
Show file tree
Hide file tree
Showing 28 changed files with 109 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import { minimatch } from 'minimatch';
import * as semver from 'semver';
import { error, print, warning } from '../../logging';
import { ToolkitError } from '../../toolkit/error';
import { flatten } from '../../util';
import * as chalk from '../../util/cdk-chalk';

export enum DefaultSelection {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
UpdateStackCommandInput,
Tag,
} from '@aws-sdk/client-cloudformation';
import * as chalk from 'chalk';
import * as uuid from 'uuid';
import type { SDK, SdkProvider, ICloudFormationClient } from './aws-auth';
import type { EnvironmentResources } from './environment-resources';
Expand All @@ -32,6 +31,7 @@ import { type TemplateBodyParameter, makeBodyParameter } from './util/template-b
import { AssetManifestBuilder } from '../util/asset-manifest-builder';
import { determineAllowCrossAccountAssetPublishing } from './util/checks';
import { publishAssets } from '../util/asset-publishing';
import * as chalk from '../util/cdk-chalk';
import { StringWithoutPlaceholders } from './util/placeholders';

export type DeployStackResult =
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { randomUUID } from 'crypto';
import * as cxapi from '@aws-cdk/cx-api';
import * as cdk_assets from 'cdk-assets';
import { AssetManifest, IManifestEntry } from 'cdk-assets';
import * as chalk from 'chalk';
import * as chalk from '../util/cdk-chalk';
import type { SdkProvider } from './aws-auth/sdk-provider';
import { type DeploymentMethod, deployStack, DeployStackResult, destroyStack } from './deploy-stack';
import { type EnvironmentResources } from './environment-resources';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as cxapi from '@aws-cdk/cx-api';
import { ImageIdentifier } from '@aws-sdk/client-ecr';
import { Tag } from '@aws-sdk/client-s3';
import * as chalk from 'chalk';
import * as promptly from 'promptly';
import { debug, print } from '../../logging';
import * as chalk from '../../util/cdk-chalk';
import { IECRClient, IS3Client, SDK, SdkProvider } from '../aws-auth';
import { DEFAULT_TOOLKIT_STACK_NAME, ToolkitInfo } from '../toolkit-info';
import { ProgressPrinter } from './progress-printer';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as chalk from 'chalk';
import { GcAsset as GCAsset } from './garbage-collector';
import { print } from '../../logging';
import * as chalk from '../../util/cdk-chalk';

export class ProgressPrinter {
private totalAssets: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/hotswap-deployments.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as cfn_diff from '@aws-cdk/cloudformation-diff';
import * as cxapi from '@aws-cdk/cx-api';
import { WaiterResult } from '@smithy/util-waiter';
import * as chalk from 'chalk';
import type { SDK, SdkProvider } from './aws-auth';
import type { SuccessfulDeployStackResult } from './deploy-stack';
import { EvaluateCloudFormationTemplate } from './evaluate-cloudformation-template';
import { print } from '../logging';
import * as chalk from '../util/cdk-chalk';
import { isHotswappableAppSyncChange } from './hotswap/appsync-mapping-templates';
import { isHotswappableCodeBuildProjectChange } from './hotswap/code-build-projects';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/logs/logs-monitor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as util from 'util';
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import { print, error } from '../../logging';
import { flatten } from '../../util/arrays';
import * as chalk from '../../util/cdk-chalk';
import type { SDK } from '../aws-auth';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { inspect } from 'util';
import type { CredentialProviderSource, IPluginHost, Plugin } from '@aws-cdk/cli-plugin-contract';
import * as chalk from 'chalk';

import { type ContextProviderPlugin, isContextProviderPlugin } from './context-provider-plugin';
import { error } from '../../logging';
import { ToolkitError } from '../../toolkit/error';
import * as chalk from '../../util/cdk-chalk';

export let TESTING = false;

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/toolkit-info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import type { SDK } from './aws-auth';
import { debug } from '../logging';
import * as chalk from '../util/cdk-chalk';
import {
BOOTSTRAP_VARIANT_PARAMETER,
BOOTSTRAP_VERSION_OUTPUT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as util from 'util';
import { ArtifactMetadataEntryType, type MetadataEntry } from '@aws-cdk/cloud-assembly-schema';
import type { CloudFormationStackArtifact } from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import { ResourceEvent, StackEventPoller } from './stack-event-poller';
import { error, setIoMessageThreshold, info } from '../../../logging';
import { IoMessageLevel } from '../../../toolkit/cli-io-host';
import * as chalk from '../../../util/cdk-chalk';
import type { ICloudFormationClient } from '../../aws-auth';
import { RewritableBlock } from '../display';

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/util/template-body-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as path from 'path';
import { type CloudFormationStackArtifact, type Environment, EnvironmentPlaceholders } from '@aws-cdk/cx-api';
import { HeadObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { getEndpointFromInstructions } from '@smithy/middleware-endpoint';
import * as chalk from 'chalk';
import * as fs from 'fs-extra';
import { debug, error } from '../../logging';
import { toYAML } from '../../serialize';
import { AssetManifestBuilder } from '../../util/asset-manifest-builder';
import * as chalk from '../../util/cdk-chalk';
import { contentHash } from '../../util/content-hash';
import { EnvironmentResources } from '../environment-resources';

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import * as path from 'path';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import { EnvironmentResources } from './api/environment-resources';
import { ToolkitInfo } from './api/toolkit-info';
import { debug } from './logging';
import { ToolkitError } from './toolkit/error';
import { AssetManifestBuilder } from './util/asset-manifest-builder';
import * as chalk from './util/cdk-chalk';

/**
* Take the metadata assets from the given stack and add them to the given asset manifest
Expand Down
6 changes: 3 additions & 3 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as path from 'path';
import { format } from 'util';
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import * as chokidar from 'chokidar';
import * as fs from 'fs-extra';
import * as promptly from 'promptly';
Expand Down Expand Up @@ -50,6 +49,7 @@ import { deserializeStructure, serializeStructure } from './serialize';
import { Configuration, PROJECT_CONFIG } from './settings';
import { ToolkitError } from './toolkit/error';
import { numberFromBool, partition } from './util';
import * as chalk from './util/cdk-chalk';
import { validateSnsTopicArn } from './util/validate-notification-arn';
import { Concurrency, WorkGraph } from './util/work-graph';
import { WorkGraphBuilder } from './util/work-graph-builder';
Expand Down Expand Up @@ -601,9 +601,9 @@ export class CdkToolkit {
anyRollbackable = true;
}
const elapsedRollbackTime = new Date().getTime() - startRollbackTime;
print('\n✨ Rollback time: %ss\n', formatTime(elapsedRollbackTime).toString());
print(`\n✨ Rollback time: ${formatTime(elapsedRollbackTime).toString()}s\n`);
} catch (e: any) {
error('\n ❌ %s failed: %s', chalk.bold(stack.displayName), e.message);
error(`\n ❌ ${chalk.bold(stack.displayName)} failed: ${e.message}`);
throw new ToolkitError('Rollback failed (use --force to orphan failing resources)');
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as cxapi from '@aws-cdk/cx-api';
import '@jsii/check-node/run';
import * as chalk from 'chalk';

import { DeploymentMethod } from './api';
import { HotswapMode } from './api/hotswap/common';
import { ILock } from './api/util/rwlock';
import { parseCommandLineArguments } from './parse-command-line-arguments';
import { checkForPlatformWarnings } from './platform-warnings';
import { IoMessageLevel, CliIoHost } from './toolkit/cli-io-host';
import * as chalk from './util/cdk-chalk';

import { enableTracing } from './util/tracing';
import { SdkProvider } from '../lib/api/aws-auth';
import { BootstrapSource, Bootstrapper } from '../lib/api/bootstrap';
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/commands/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as chalk from 'chalk';
import { minimatch } from 'minimatch';
import * as version from '../../lib/version';
import { print, error, warning, info } from '../logging';
import { Context, PROJECT_CONFIG, PROJECT_CONTEXT, USER_DEFAULTS } from '../settings';
import { renderTable } from '../util';
import * as chalk from '../util/cdk-chalk';

/**
* Options for the context command
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/commands/docs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as childProcess from 'child_process';
import * as chalk from 'chalk';
import { debug, print, warning } from '../../lib/logging';
import * as chalk from '../util/cdk-chalk';

export const command = 'docs';
export const describe = 'Opens the reference documentation in a browser';
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/commands/doctor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as process from 'process';
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import { print } from '../../lib/logging';
import * as version from '../../lib/version';
import * as chalk from '../util/cdk-chalk';

export async function doctor(): Promise<number> {
let exitStatus: number = 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import type {
ScannedResourceIdentifier,
} from '@aws-sdk/client-cloudformation';
import * as cdk_from_cfn from 'cdk-from-cfn';
import * as chalk from 'chalk';
import { cliInit } from '../../lib/init';
import { print } from '../../lib/logging';
import type { ICloudFormationClient, SdkProvider } from '../api/aws-auth';
import { CloudFormationStack } from '../api/util/cloudformation';
import { zipDirectory } from '../util/archive';
import * as chalk from '../util/cdk-chalk';
const camelCase = require('camelcase');
const decamelize = require('decamelize');
/** The list of languages supported by the built-in noctilucent binary. */
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
mangleLikeCloudFormation,
} from '@aws-cdk/cloudformation-diff';
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import { NestedStackTemplates } from './api/nested-stack-helpers';
import { print, warning } from './logging';
import { ToolkitError } from './toolkit/error';
import * as chalk from './util/cdk-chalk';

/**
* Pretty-prints the differences between two template states to the console.
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DeployOptions } from '@aws-cdk/cloud-assembly-schema';
import * as cfnDiff from '@aws-cdk/cloudformation-diff';
import { ResourceDifference } from '@aws-cdk/cloudformation-diff';
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import * as fs from 'fs-extra';
import * as promptly from 'promptly';
import { DeploymentMethod } from './api';
Expand All @@ -13,6 +12,7 @@ import { StackActivityProgress } from './api/util/cloudformation/stack-activity-
import { Tag } from './cdk-toolkit';
import { error, print, success, warning } from './logging';
import { ToolkitError } from './toolkit/error';
import * as chalk from './util/cdk-chalk';

export interface ImportDeploymentOptions extends DeployOptions {
deploymentMethod?: DeploymentMethod;
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/init.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as childProcess from 'child_process';
import * as path from 'path';
import * as chalk from 'chalk';
import * as fs from 'fs-extra';
import { invokeBuiltinHooks } from './init-hooks';
import { error, print, warning } from './logging';
import { ToolkitError } from './toolkit/error';
import * as chalk from './util/cdk-chalk';
import { cdkHomeDir, rootDir } from './util/directories';
import { rangeFromSemver } from './util/version-range';

Expand Down
10 changes: 5 additions & 5 deletions packages/aws-cdk/lib/logging.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as util from 'util';
import * as chalk from 'chalk';
import { IoMessageLevel, IoMessage, CliIoHost, validateMessageCode } from './toolkit/cli-io-host';
import * as chalk from './util/cdk-chalk';

// Corking mechanism
let CORK_COUNTER = 0;
Expand Down Expand Up @@ -70,10 +70,10 @@ interface LogOptions {
*/
forceStdout?: boolean;
/**
* Message code of the format [CATEGORY]_[NUMBER_CODE]
* @pattern [A-Z]+_[0-2][0-9]{3}
* @default TOOLKIT_[0/1/2]000
*/
* Message code of the format [CATEGORY]_[NUMBER_CODE]
* @pattern [A-Z]+_[0-2][0-9]{3}
* @default TOOLKIT_[0/1/2]000
*/
code?: string;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/os.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as child_process from 'child_process';
import * as chalk from 'chalk';
import { debug, data } from './logging';
import { ToolkitError } from './toolkit/error';
import * as chalk from './util/cdk-chalk';

/**
* OS helpers
Expand Down
9 changes: 7 additions & 2 deletions packages/aws-cdk/lib/toolkit/cli-io-host.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as chalk from 'chalk';
import * as chalk from '../util/cdk-chalk';

/**
* Basic message structure for toolkit notifications.
Expand Down Expand Up @@ -97,13 +97,17 @@ export class CliIoHost {

/**
* Whether the host should apply chalk styles to messages. Defaults to false if the host is not running in a TTY.
*
* @default false
*/
private isTTY: boolean;

/**
* Whether the CliIoHost is running in CI mode. In CI mode, all non-error output goes to stdout instead of stderr.
*
* @default false
*/
private ci: boolean = false;
private ci: boolean;

/**
* the current {@link IoAction} set by the CLI.
Expand All @@ -112,6 +116,7 @@ export class CliIoHost {

private constructor() {
this.isTTY = process.stdout.isTTY ?? false;
this.ci = false;
}

public static get currentAction(): IoAction | undefined {
Expand Down
55 changes: 55 additions & 0 deletions packages/aws-cdk/lib/util/cdk-chalk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as chalk from 'chalk';

// Create a new chalk instance with color support disabled if we're not in a TTY
const wrapper = new chalk.Instance({
level: process.stdout.isTTY ? 2 : 0,
});

export default wrapper;

// Export individual methods
export const {
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
gray,
grey,
blackBright,
redBright,
greenBright,
yellowBright,
blueBright,
magentaBright,
cyanBright,
whiteBright,
bgBlack,
bgRed,
bgGreen,
bgYellow,
bgBlue,
bgMagenta,
bgCyan,
bgWhite,
bgBlackBright,
bgRedBright,
bgGreenBright,
bgYellowBright,
bgBlueBright,
bgMagentaBright,
bgCyanBright,
bgWhiteBright,
bold,
dim,
italic,
underline,
inverse,
hidden,
strikethrough,
visible,
reset,
} = wrapper;
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import * as chalk from 'chalk';
import * as fs from 'fs-extra';
import * as semver from 'semver';
import * as chalk from './util/cdk-chalk';
import { cdkCacheDir, rootDir } from './util/directories';
import { getLatestVersionFromNpm } from './util/npm';
import { debug, print } from '../lib/logging';
Expand Down
Loading

0 comments on commit b9a8c60

Please sign in to comment.