Skip to content

Commit 856d170

Browse files
authored
chore: transition from using colors to chalk (#18363)
The stability and long-term prospects of colors is unclear; remove our dependency and start using chalk instead. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 24f8f74 commit 856d170

37 files changed

+220
-190
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@
123123
"aws-cdk-lib/@balena/dockerignore/**",
124124
"aws-cdk-lib/case",
125125
"aws-cdk-lib/case/**",
126-
"aws-cdk-lib/colors",
127-
"aws-cdk-lib/colors/**",
126+
"aws-cdk-lib/chalk",
127+
"aws-cdk-lib/chalk/**",
128128
"aws-cdk-lib/diff",
129129
"aws-cdk-lib/diff/**",
130130
"aws-cdk-lib/fast-deep-equal",
@@ -151,8 +151,8 @@
151151
"monocdk/@balena/dockerignore/**",
152152
"monocdk/case",
153153
"monocdk/case/**",
154-
"monocdk/colors",
155-
"monocdk/colors/**",
154+
"monocdk/chalk",
155+
"monocdk/chalk/**",
156156
"monocdk/diff",
157157
"monocdk/diff/**",
158158
"monocdk/fast-deep-equal",
@@ -180,4 +180,4 @@
180180
"dependencies": {
181181
"string-width": "^4.2.3"
182182
}
183-
}
183+
}

packages/@aws-cdk/aws-cloudtrail/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
"@aws-cdk/pkglint": "0.0.0",
8787
"@types/jest": "^27.4.0",
8888
"aws-sdk": "^2.848.0",
89-
"colors": "1.4.0",
9089
"jest": "^27.4.7"
9190
},
9291
"dependencies": {

packages/@aws-cdk/cloudformation-diff/lib/format-table.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as colors from 'colors/safe';
1+
import * as chalk from 'chalk';
22
import * as stringWidth from 'string-width';
33
import * as table from 'table';
44

@@ -93,7 +93,7 @@ function sum(xs: number[]): number {
9393
}
9494

9595
// What color the table is going to be
96-
const tableColor = colors.gray;
96+
const tableColor = chalk.gray;
9797

9898
// Unicode table characters with a color
9999
const TABLE_BORDER_CHARACTERS = {

packages/@aws-cdk/cloudformation-diff/lib/format.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { format } from 'util';
2-
import * as colors from 'colors/safe';
2+
import * as chalk from 'chalk';
33
import { Difference, isPropertyDifference, ResourceDifference, ResourceImpact } from './diff-template';
44
import { DifferenceCollection, TemplateDiff } from './diff/types';
55
import { deepEqual } from './diff/util';
@@ -75,10 +75,10 @@ function formatSecurityChangesWithBanner(formatter: Formatter, templateDiff: Tem
7575
formatter.printSectionFooter();
7676
}
7777

78-
const ADDITION = colors.green('[+]');
79-
const CONTEXT = colors.grey('[ ]');
80-
const UPDATE = colors.yellow('[~]');
81-
const REMOVAL = colors.red('[-]');
78+
const ADDITION = chalk.green('[+]');
79+
const CONTEXT = chalk.grey('[ ]');
80+
const UPDATE = chalk.yellow('[~]');
81+
const REMOVAL = chalk.red('[-]');
8282

8383
class Formatter {
8484
constructor(
@@ -93,11 +93,11 @@ class Formatter {
9393
}
9494

9595
public print(fmt: string, ...args: any[]) {
96-
this.stream.write(colors.white(format(fmt, ...args)) + '\n');
96+
this.stream.write(chalk.white(format(fmt, ...args)) + '\n');
9797
}
9898

9999
public warning(fmt: string, ...args: any[]) {
100-
this.stream.write(colors.yellow(format(fmt, ...args)) + '\n');
100+
this.stream.write(chalk.yellow(format(fmt, ...args)) + '\n');
101101
}
102102

103103
public formatSection<V, T extends Difference<V>>(
@@ -116,7 +116,7 @@ class Formatter {
116116
}
117117

118118
public printSectionHeader(title: string) {
119-
this.print(colors.underline(colors.bold(title)));
119+
this.print(chalk.underline(chalk.bold(title)));
120120
}
121121

122122
public printSectionFooter() {
@@ -134,8 +134,8 @@ class Formatter {
134134

135135
let value;
136136

137-
const oldValue = this.formatValue(diff.oldValue, colors.red);
138-
const newValue = this.formatValue(diff.newValue, colors.green);
137+
const oldValue = this.formatValue(diff.oldValue, chalk.red);
138+
const newValue = this.formatValue(diff.newValue, chalk.green);
139139
if (diff.isAddition) {
140140
value = newValue;
141141
} else if (diff.isUpdate) {
@@ -144,7 +144,7 @@ class Formatter {
144144
value = oldValue;
145145
}
146146

147-
this.print(`${this.formatPrefix(diff)} ${colors.cyan(type)} ${this.formatLogicalId(logicalId)}: ${value}`);
147+
this.print(`${this.formatPrefix(diff)} ${chalk.cyan(type)} ${this.formatLogicalId(logicalId)}: ${value}`);
148148
}
149149

150150
/**
@@ -159,7 +159,7 @@ class Formatter {
159159
const resourceType = diff.isRemoval ? diff.oldResourceType : diff.newResourceType;
160160

161161
// eslint-disable-next-line max-len
162-
this.print(`${this.formatPrefix(diff)} ${this.formatValue(resourceType, colors.cyan)} ${this.formatLogicalId(logicalId)} ${this.formatImpact(diff.changeImpact)}`);
162+
this.print(`${this.formatPrefix(diff)} ${this.formatValue(resourceType, chalk.cyan)} ${this.formatLogicalId(logicalId)} ${this.formatImpact(diff.changeImpact)}`);
163163

164164
if (diff.isUpdate) {
165165
const differenceCount = diff.differenceCount;
@@ -175,7 +175,7 @@ class Formatter {
175175
if (diff.isAddition) { return ADDITION; }
176176
if (diff.isUpdate) { return UPDATE; }
177177
if (diff.isRemoval) { return REMOVAL; }
178-
return colors.white('[?]');
178+
return chalk.white('[?]');
179179
}
180180

181181
/**
@@ -197,13 +197,13 @@ class Formatter {
197197
public formatImpact(impact: ResourceImpact) {
198198
switch (impact) {
199199
case ResourceImpact.MAY_REPLACE:
200-
return colors.italic(colors.yellow('may be replaced'));
200+
return chalk.italic(chalk.yellow('may be replaced'));
201201
case ResourceImpact.WILL_REPLACE:
202-
return colors.italic(colors.bold(colors.red('replace')));
202+
return chalk.italic(chalk.bold(chalk.red('replace')));
203203
case ResourceImpact.WILL_DESTROY:
204-
return colors.italic(colors.bold(colors.red('destroy')));
204+
return chalk.italic(chalk.bold(chalk.red('destroy')));
205205
case ResourceImpact.WILL_ORPHAN:
206-
return colors.italic(colors.yellow('orphan'));
206+
return chalk.italic(chalk.yellow('orphan'));
207207
case ResourceImpact.WILL_UPDATE:
208208
case ResourceImpact.WILL_CREATE:
209209
case ResourceImpact.NO_CHANGE:
@@ -249,13 +249,13 @@ class Formatter {
249249
this.print('%s %s %s', linePrefix, i === 0 ? '└─' : ' ', diff[i]);
250250
}
251251
} else {
252-
this.print('%s ├─ %s %s', linePrefix, REMOVAL, this.formatValue(oldObject, colors.red));
253-
this.print('%s └─ %s %s', linePrefix, ADDITION, this.formatValue(newObject, colors.green));
252+
this.print('%s ├─ %s %s', linePrefix, REMOVAL, this.formatValue(oldObject, chalk.red));
253+
this.print('%s └─ %s %s', linePrefix, ADDITION, this.formatValue(newObject, chalk.green));
254254
}
255255
} else if (oldObject !== undefined /* && newObject === undefined */) {
256-
this.print('%s └─ %s', linePrefix, this.formatValue(oldObject, colors.red));
256+
this.print('%s └─ %s', linePrefix, this.formatValue(oldObject, chalk.red));
257257
} else /* if (oldObject === undefined && newObject !== undefined) */ {
258-
this.print('%s └─ %s', linePrefix, this.formatValue(newObject, colors.green));
258+
this.print('%s └─ %s', linePrefix, this.formatValue(newObject, chalk.green));
259259
}
260260
return;
261261
}
@@ -268,12 +268,12 @@ class Formatter {
268268
const newValue = newObject[key];
269269
const treePrefix = key === lastKey ? '└' : '├';
270270
if (oldValue !== undefined && newValue !== undefined) {
271-
this.print('%s %s─ %s %s:', linePrefix, treePrefix, this.changeTag(oldValue, newValue), colors.blue(`.${key}`));
271+
this.print('%s %s─ %s %s:', linePrefix, treePrefix, this.changeTag(oldValue, newValue), chalk.blue(`.${key}`));
272272
this.formatObjectDiff(oldValue, newValue, `${linePrefix} ${key === lastKey ? ' ' : '│'}`);
273273
} else if (oldValue !== undefined /* && newValue === undefined */) {
274-
this.print('%s %s─ %s Removed: %s', linePrefix, treePrefix, REMOVAL, colors.blue(`.${key}`));
274+
this.print('%s %s─ %s Removed: %s', linePrefix, treePrefix, REMOVAL, chalk.blue(`.${key}`));
275275
} else /* if (oldValue === undefined && newValue !== undefined */ {
276-
this.print('%s %s─ %s Added: %s', linePrefix, treePrefix, ADDITION, colors.blue(`.${key}`));
276+
this.print('%s %s─ %s Added: %s', linePrefix, treePrefix, ADDITION, chalk.blue(`.${key}`));
277277
}
278278
}
279279
}
@@ -322,7 +322,7 @@ class Formatter {
322322
const normalized = this.normalizedLogicalIdPath(logicalId);
323323

324324
if (normalized) {
325-
return `${normalized} ${colors.gray(logicalId)}`;
325+
return `${normalized} ${chalk.gray(logicalId)}`;
326326
}
327327

328328
return logicalId;
@@ -430,7 +430,7 @@ function _diffStrings(oldStr: string, newStr: string, context: number): string[]
430430
const patch: Patch = structuredPatch(null, null, oldStr, newStr, null, null, { context });
431431
const result = new Array<string>();
432432
for (const hunk of patch.hunks) {
433-
result.push(colors.magenta(`@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`));
433+
result.push(chalk.magenta(`@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`));
434434
const baseIndent = _findIndent(hunk.lines);
435435
for (const line of hunk.lines) {
436436
// Don't care about termination newline.
@@ -442,10 +442,10 @@ function _diffStrings(oldStr: string, newStr: string, context: number): string[]
442442
result.push(`${CONTEXT} ${text}`);
443443
break;
444444
case '+':
445-
result.push(colors.bold(`${ADDITION} ${colors.green(text)}`));
445+
result.push(chalk.bold(`${ADDITION} ${chalk.green(text)}`));
446446
break;
447447
case '-':
448-
result.push(colors.bold(`${REMOVAL} ${colors.red(text)}`));
448+
result.push(chalk.bold(`${REMOVAL} ${chalk.red(text)}`));
449449
break;
450450
default:
451451
throw new Error(`Unexpected diff marker: ${marker} (full line: ${line})`);

packages/@aws-cdk/cloudformation-diff/lib/iam/iam-changes.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as cfnspec from '@aws-cdk/cfnspec';
2-
import * as colors from 'colors/safe';
2+
import * as chalk from 'chalk';
33
import { PropertyChange, PropertyMap, ResourceChange } from '../diff/types';
44
import { DiffableCollection } from '../diffable';
55
import { renderIntrinsics } from '../render-intrinsics';
@@ -77,18 +77,18 @@ export class IamChanges {
7777
renderedStatement.action,
7878
renderedStatement.principal,
7979
renderedStatement.condition,
80-
].map(s => colors.green(s)));
80+
].map(s => chalk.green(s)));
8181
}
8282
for (const statement of this.statements.removals) {
8383
const renderedStatement = statement.render();
8484
ret.push([
85-
colors.red('-'),
85+
chalk.red('-'),
8686
renderedStatement.resource,
8787
renderedStatement.effect,
8888
renderedStatement.action,
8989
renderedStatement.principal,
9090
renderedStatement.condition,
91-
].map(s => colors.red(s)));
91+
].map(s => chalk.red(s)));
9292
}
9393

9494
// Sort by 2nd column
@@ -108,14 +108,14 @@ export class IamChanges {
108108
'+',
109109
att.identityArn,
110110
att.managedPolicyArn,
111-
].map(s => colors.green(s)));
111+
].map(s => chalk.green(s)));
112112
}
113113
for (const att of this.managedPolicies.removals) {
114114
ret.push([
115115
'-',
116116
att.identityArn,
117117
att.managedPolicyArn,
118-
].map(s => colors.red(s)));
118+
].map(s => chalk.red(s)));
119119
}
120120

121121
// Sort by 2nd column

packages/@aws-cdk/cloudformation-diff/lib/network/security-group-changes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as colors from 'colors/safe';
1+
import * as chalk from 'chalk';
22
import { PropertyChange, ResourceChange } from '../diff/types';
33
import { DiffableCollection } from '../diffable';
44
import { renderIntrinsics } from '../render-intrinsics';
@@ -66,7 +66,7 @@ export class SecurityGroupChanges {
6666
inOut,
6767
rule.describeProtocol(),
6868
rule.describePeer(),
69-
].map(s => plusMin === '+' ? colors.green(s) : colors.red(s));
69+
].map(s => plusMin === '+' ? chalk.green(s) : chalk.red(s));
7070

7171
// First generate all lines, sort later
7272
ret.push(...this.ingress.additions.map(renderRule('+', inWord)));

packages/@aws-cdk/cloudformation-diff/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"dependencies": {
2626
"@aws-cdk/cfnspec": "0.0.0",
2727
"@types/node": "^10.17.60",
28-
"colors": "1.4.0",
28+
"chalk": "^4",
2929
"diff": "^5.0.0",
3030
"fast-deep-equal": "^3.1.3",
3131
"string-width": "^4.2.3",
@@ -58,4 +58,4 @@
5858
"publishConfig": {
5959
"tag": "latest-1"
6060
}
61-
}
61+
}

packages/aws-cdk/bin/cdk.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import 'source-map-support/register';
33
import * as cxapi from '@aws-cdk/cx-api';
44
import '@jsii/check-node/run';
5-
import * as colors from 'colors/safe';
5+
import * as chalk from 'chalk';
66
import * as yargs from 'yargs';
77

88
import { SdkProvider } from '../lib/api/aws-auth';
@@ -190,7 +190,8 @@ async function parseCommandLineArguments() {
190190
}
191191

192192
if (!process.stdout.isTTY) {
193-
colors.disable();
193+
// Disable chalk color highlighting
194+
process.env.FORCE_COLOR = '0';
194195
}
195196

196197
async function initCommandLine() {
@@ -234,7 +235,7 @@ async function initCommandLine() {
234235
for (const plugin of plugins) {
235236
const resolved = tryResolve(plugin);
236237
if (loaded.has(resolved)) { continue; }
237-
debug(`Loading plug-in: ${colors.green(plugin)} from ${colors.blue(resolved)}`);
238+
debug(`Loading plug-in: ${chalk.green(plugin)} from ${chalk.blue(resolved)}`);
238239
PluginHost.instance.load(plugin);
239240
loaded.add(resolved);
240241
}
@@ -244,7 +245,7 @@ async function initCommandLine() {
244245
try {
245246
return require.resolve(plugin);
246247
} catch (e) {
247-
error(`Unable to resolve plugin ${colors.green(plugin)}: ${e.stack}`);
248+
error(`Unable to resolve plugin ${chalk.green(plugin)}: ${e.stack}`);
248249
throw new Error(`Unable to resolve plug-in: ${plugin}`);
249250
}
250251
}
@@ -278,7 +279,7 @@ async function initCommandLine() {
278279

279280
async function main(command: string, args: any): Promise<number | string | {} | void> {
280281
const toolkitStackName: string = ToolkitInfo.determineName(configuration.settings.get(['toolkitStackName']));
281-
debug(`Toolkit stack: ${colors.bold(toolkitStackName)}`);
282+
debug(`Toolkit stack: ${chalk.bold(toolkitStackName)}`);
282283

283284
if (args.all && args.STACKS) {
284285
throw new Error('You must either specify a list of Stacks or the `--all` argument');

packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as cxapi from '@aws-cdk/cx-api';
2-
import * as colors from 'colors/safe';
2+
import * as chalk from 'chalk';
33
import * as minimatch from 'minimatch';
44
import * as semver from 'semver';
55
import { error, print, warning } from '../../logging';
@@ -134,7 +134,7 @@ export class CloudAssembly {
134134
if (minimatch(stack.hierarchicalId, pattern)) {
135135
return true;
136136
} else if (!disableLegacy && stack.id === pattern && semver.major(versionNumber()) < 2) {
137-
warning('Selecting stack by identifier "%s". This identifier is deprecated and will be removed in v2. Please use "%s" instead.', colors.bold(stack.id), colors.bold(stack.hierarchicalId));
137+
warning('Selecting stack by identifier "%s". This identifier is deprecated and will be removed in v2. Please use "%s" instead.', chalk.bold(stack.id), chalk.bold(stack.hierarchicalId));
138138
warning('Run "cdk ls" to see a list of all stack identifiers');
139139
return true;
140140
}
@@ -342,7 +342,7 @@ function includeDownstreamStacks(
342342
} while (madeProgress);
343343

344344
if (added.length > 0) {
345-
print('Including depending stacks: %s', colors.bold(added.join(', ')));
345+
print('Including depending stacks: %s', chalk.bold(added.join(', ')));
346346
}
347347
}
348348

@@ -372,12 +372,12 @@ function includeUpstreamStacks(
372372
}
373373

374374
if (added.length > 0) {
375-
print('Including dependency stacks: %s', colors.bold(added.join(', ')));
375+
print('Including dependency stacks: %s', chalk.bold(added.join(', ')));
376376
}
377377
}
378378

379379
function sanitizePatterns(patterns: string[]): string[] {
380380
let sanitized = patterns.filter(s => s != null); // filter null/undefined
381381
sanitized = [...new Set(sanitized)]; // make them unique
382382
return sanitized;
383-
}
383+
}

0 commit comments

Comments
 (0)