Skip to content

Commit 8ca3424

Browse files
authored
refactor: migrate to styleText from chalk (#29005)
1 parent a9b1071 commit 8ca3424

38 files changed

Lines changed: 352 additions & 262 deletions

lint/common/overlap.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
33

4-
import chalk from 'chalk-template';
4+
import { styleText } from 'node:util';
5+
56
import { compareVersions } from 'compare-versions';
67

78
import { createStatementGroupKey } from '../utils.js';
@@ -93,7 +94,7 @@ export const checkOverlap = (data, browser, { logger, fix = false }) => {
9394

9495
if (!fixed && logger) {
9596
logger.error(
96-
chalk`{bold ${browser}} statements overlap for {bold ${groupKey}}: ` +
97+
`${styleText('bold', browser)} statements overlap for ${styleText('bold', groupKey)}: ` +
9798
`[${formatRange(next)}, ${formatRange(current)}]`,
9899
);
99100
}

lint/fix.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import { readdir, readFile, stat, writeFile } from 'node:fs/promises';
55
import path from 'node:path';
66
import { fileURLToPath } from 'node:url';
7+
import { styleText } from 'node:util';
78

89
import esMain from 'es-main';
910
import yargs from 'yargs';
1011
import { hideBin } from 'yargs/helpers';
11-
import chalk from 'chalk-template';
1212

1313
import dataFolders from '../scripts/lib/data-folders.js';
1414

@@ -69,7 +69,9 @@ const load = async (options, ...files) => {
6969
try {
7070
fsStats = await stat(file);
7171
} catch {
72-
console.warn(chalk`{yellow File {bold ${file}} doesn't exist!}`);
72+
console.warn(
73+
styleText('yellow', `File ${styleText('bold', file)} doesn't exist!`),
74+
);
7375
continue;
7476
}
7577

lint/lint.js

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import fs from 'node:fs/promises';
55
import path from 'node:path';
66
import { fileURLToPath } from 'node:url';
7+
import { styleText } from 'node:util';
78

89
import esMain from 'es-main';
910
import yargs from 'yargs';
1011
import { hideBin } from 'yargs/helpers';
11-
import chalk from 'chalk-template';
1212

1313
import dataFolders from '../scripts/lib/data-folders.js';
1414
import extend from '../scripts/lib/extend.js';
@@ -67,7 +67,9 @@ const loadAndCheckFiles = async (...files) => {
6767
try {
6868
fsStats = await fs.stat(file);
6969
} catch {
70-
console.warn(chalk`{yellow File {bold ${file}} doesn't exist!}`);
70+
console.warn(
71+
styleText('yellow', `File ${styleText('bold', file)} doesn't exist!`),
72+
);
7173
continue;
7274
}
7375

@@ -118,10 +120,10 @@ const main = async (files = dataFolders, options = {}) => {
118120

119121
let hasErrors = false;
120122

121-
console.log(chalk`{cyan Loading and checking files...}`);
123+
console.log(styleText('cyan', 'Loading and checking files...'));
122124
const data = await loadAndCheckFiles(...files);
123125

124-
console.log(chalk`{cyan Testing browser data...}`);
126+
console.log(styleText('cyan', 'Testing browser data...'));
125127
for (const browser in data?.browsers) {
126128
await linters.runScope('browser', {
127129
data: data.browsers[browser],
@@ -134,7 +136,7 @@ const main = async (files = dataFolders, options = {}) => {
134136
});
135137
}
136138

137-
console.log(chalk`{cyan Testing feature data...}`);
139+
console.log(styleText('cyan', 'Testing feature data...'));
138140
const walker = walk(undefined, data);
139141
for (const feature of walker) {
140142
await linters.runScope('feature', {
@@ -147,7 +149,7 @@ const main = async (files = dataFolders, options = {}) => {
147149
});
148150
}
149151

150-
console.log(chalk`{cyan Testing all features together...}`);
152+
console.log(styleText('cyan', 'Testing all features together...'));
151153
await linters.runScope('tree', {
152154
data,
153155
rawdata: '',
@@ -186,30 +188,35 @@ const main = async (files = dataFolders, options = {}) => {
186188
.join(', ');
187189

188190
console.error(
189-
chalk`{${
190-
messagesByLevel.error.length ? 'red' : 'yellow'
191-
} ${linter} - {bold ${pluralize(
192-
'problem',
193-
messages.length,
194-
true,
195-
)}} (${errorCounts}):}`,
191+
styleText(
192+
messagesByLevel.error.length ? 'red' : 'yellow',
193+
`${linter} - ${styleText('bold', pluralize('problem', messages.length, true))} (${errorCounts}):`,
194+
),
196195
);
197196

198197
for (const message of messages) {
198+
const levelColor =
199+
message.level === 'error'
200+
? 'red'
201+
: message.level === 'warning'
202+
? 'yellow'
203+
: 'blue';
199204
console.error(
200-
chalk`{${message.level === 'error' ? 'red' : message.level === 'warning' ? 'yellow' : 'blue'}${
201-
message.path
202-
} - ${message.level[0].toUpperCase() + message.level.substring(1)}${
203-
message.message
204-
}}`,
205+
styleText(
206+
levelColor,
207+
` ✖ ${message.path} - ${message.level[0].toUpperCase() + message.level.substring(1)}${message.message}`,
208+
),
205209
);
206210
if (message.fixable) {
207211
console.error(
208-
chalk`{blue ◆ Tip: Run {bold npm run fix} to fix this problem automatically}`,
212+
styleText(
213+
'blue',
214+
` ◆ Tip: Run ${styleText('bold', 'npm run fix')} to fix this problem automatically`,
215+
),
209216
);
210217
}
211218
if (message.tip) {
212-
console.error(chalk`{blue ◆ Tip: ${message.tip}}`);
219+
console.error(styleText('blue', ` ◆ Tip: ${message.tip}`));
213220
}
214221
}
215222
}
@@ -222,30 +229,37 @@ const main = async (files = dataFolders, options = {}) => {
222229
if (missingExceptions[exception]) {
223230
hasErrors = true;
224231
console.error(
225-
chalk`{red ✖ ${linter.name} - Unnecessary exception → ${exception}}`,
232+
styleText(
233+
'red',
234+
` ✖ ${linter.name} - Unnecessary exception → ${exception}`,
235+
),
226236
);
227237
}
228238
}
229239
}
230240
}
231241

232242
if (!hasErrors) {
233-
console.log(chalk`{green All data {bold passed} linting!}`);
243+
console.log(
244+
styleText('green', `All data ${styleText('bold', 'passed')} linting!`),
245+
);
234246
if (linters.linters.some((linter) => linter.exceptions)) {
235247
console.log(
236-
chalk`{yellow Linters have some exceptions, please help us remove them!}`,
248+
styleText(
249+
'yellow',
250+
'Linters have some exceptions, please help us remove them!',
251+
),
237252
);
238253
for (const linter of linters.linters) {
239254
if (linter.exceptions) {
240255
console.log(
241-
chalk`{yellow ${linter.name} has ${pluralize(
242-
'exception',
243-
linter.exceptions.length,
244-
true,
245-
)}}`,
256+
styleText(
257+
'yellow',
258+
` ${linter.name} has ${pluralize('exception', linter.exceptions.length, true)}`,
259+
),
246260
);
247261
for (const exception of linter.exceptions) {
248-
console.log(chalk`{yellow - ${exception}}`);
262+
console.log(styleText('yellow', ` - ${exception}`));
249263
}
250264
}
251265
}

lint/linter/test-browsers-data.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
33

4-
import chalk from 'chalk-template';
4+
import { styleText } from 'node:util';
55

66
import bcd from '../../index.js';
77
const { browsers } = bcd;
@@ -25,9 +25,10 @@ const processData = (browser, data, logger) => {
2525

2626
if (releasesForStatus.length > 1) {
2727
logger.error(
28-
chalk`{red {bold ${browser}} has multiple {bold ${status}} releases (${releasesForStatus.join(
29-
', ',
30-
)}), which is {bold not} allowed.}`,
28+
styleText(
29+
'red',
30+
`${styleText('bold', browser)} has multiple ${styleText('bold', status)} releases (${releasesForStatus.join(', ')}), which is ${styleText('bold', 'not')} allowed.`,
31+
),
3132
);
3233
}
3334
}
@@ -36,15 +37,19 @@ const processData = (browser, data, logger) => {
3637
if (data.upstream) {
3738
if (data.upstream === browser) {
3839
logger.error(
39-
chalk`{red The upstream for {bold ${browser}} is set to itself.}`,
40+
styleText(
41+
'red',
42+
`The upstream for ${styleText('bold', browser)} is set to itself.`,
43+
),
4044
);
4145
}
4246

4347
if (!Object.keys(browsers).includes(data.upstream)) {
4448
logger.error(
45-
chalk`{red The upstream for {bold ${browser}} is an unknown browser (${
46-
data.upstream
47-
}) Valid options are: ${Object.keys(browsers).join(', ')}.}`,
49+
styleText(
50+
'red',
51+
`The upstream for ${styleText('bold', browser)} is an unknown browser (${data.upstream}) Valid options are: ${Object.keys(browsers).join(', ')}.`,
52+
),
4853
);
4954
}
5055
}
@@ -64,9 +69,10 @@ const processData = (browser, data, logger) => {
6469

6570
if (releasesWithoutDate.length > 0) {
6671
logger.error(
67-
chalk`{red {bold ${browser}} has {bold ${status}} releases without release date (${releasesWithoutDate.join(
68-
', ',
69-
)}), which is {bold not} allowed.}`,
72+
styleText(
73+
'red',
74+
`${styleText('bold', browser)} has ${styleText('bold', status)} releases without release date (${releasesWithoutDate.join(', ')}), which is ${styleText('bold', 'not')} allowed.`,
75+
),
7076
);
7177
}
7278
}

lint/linter/test-browsers-presence.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
33

4-
import chalk from 'chalk-template';
4+
import { styleText } from 'node:util';
55

66
import bcd from '../../index.js';
77
const { browsers } = bcd;
@@ -50,9 +50,7 @@ const processData = (data, category, logger) => {
5050
);
5151
if (undefEntries.length > 0) {
5252
logger.error(
53-
chalk`Has the following browsers, which are not defined in BCD: {bold ${undefEntries.join(
54-
', ',
55-
)}}`,
53+
`Has the following browsers, which are not defined in BCD: ${styleText('bold', undefEntries.join(', '))}`,
5654
);
5755
}
5856

@@ -61,9 +59,7 @@ const processData = (data, category, logger) => {
6159
).filter((value) => !displayBrowsers.includes(value));
6260
if (invalidEntries.length > 0) {
6361
logger.error(
64-
chalk`Has the following browsers, which are invalid for {bold ${category}} compat data: {bold ${invalidEntries.join(
65-
', ',
66-
)}}`,
62+
`Has the following browsers, which are invalid for ${styleText('bold', category)} compat data: ${styleText('bold', invalidEntries.join(', '))}`,
6763
);
6864
}
6965

@@ -72,9 +68,7 @@ const processData = (data, category, logger) => {
7268
);
7369
if (missingEntries.length > 0) {
7470
logger.error(
75-
chalk`Missing the following browsers, which are required for {bold ${category}} compat data: {bold ${missingEntries.join(
76-
', ',
77-
)}}`,
71+
`Missing the following browsers, which are required for ${styleText('bold', category)} compat data: ${styleText('bold', missingEntries.join(', '))}`,
7872
);
7973
}
8074
}

lint/linter/test-consistency.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
33

4+
import { styleText } from 'node:util';
5+
46
import { compare } from 'compare-versions';
5-
import chalk from 'chalk-template';
67

78
import { query } from '../../utils/index.js';
89
import mirrorSupport from '../../scripts/build/mirror.js';
@@ -426,15 +427,16 @@ export default {
426427
for (const { type, browser, parentValue, subfeatures } of errors) {
427428
let errorMessage = '';
428429
if (type == 'unsupported') {
429-
errorMessage += chalk`No support in {bold ${browser}}, but support is declared in the following sub-feature(s):`;
430+
errorMessage += `No support in ${styleText('bold', browser)}, but support is declared in the following sub-feature(s):`;
430431
} else if (type == 'subfeature_earlier_implementation') {
431-
errorMessage += chalk`Basic support in {bold ${browser}} was declared implemented in a later version ({bold ${parentValue}}) than the following sub-feature(s):`;
432+
errorMessage += `Basic support in ${styleText('bold', browser)} was declared implemented in a later version (${styleText('bold', String(parentValue))}) than the following sub-feature(s):`;
432433
}
433434

434435
for (const subfeature of subfeatures) {
435-
errorMessage += chalk`\n{red → {bold ${path.join('.')}.${
436-
subfeature[0]
437-
}}: ${subfeature[1] === undefined ? '[Array]' : subfeature[1]}}`;
436+
errorMessage += styleText(
437+
'red',
438+
`\n → ${styleText('bold', `${path.join('.')}.${subfeature[0]}`)}: ${subfeature[1] === undefined ? '[Array]' : subfeature[1]}`,
439+
);
438440
}
439441

440442
logger.error(errorMessage, { path: path.join('.') });

lint/linter/test-descriptions.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
33

4-
import chalk from 'chalk-template';
4+
import { styleText } from 'node:util';
55

66
import { validateHTML } from './test-notes.js';
77

@@ -142,12 +142,15 @@ export default {
142142

143143
for (const error of errors) {
144144
if (typeof error === 'string') {
145-
logger.error(chalk`{red ${error}}`);
145+
logger.error(styleText('red', error));
146146
} else {
147147
logger.error(
148-
chalk`{red Incorrect ${error.ruleName} description for {bold ${error.path}}
149-
Actual: {yellow "${error.actual}"}
150-
Expected: {green "${error.expected}"}}`,
148+
styleText(
149+
'red',
150+
`Incorrect ${error.ruleName} description for ${styleText('bold', error.path)}
151+
Actual: ${styleText('yellow', `"${error.actual}"`)}
152+
Expected: ${styleText('green', `"${error.expected}"`)}`,
153+
),
151154
{ fixable: true },
152155
);
153156
}

lint/linter/test-flags.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
33

4-
import chalk from 'chalk-template';
4+
import { styleText } from 'node:util';
5+
56
import { compare } from 'compare-versions';
67

78
/** @import {Linter, LinterData} from '../types.js' */
@@ -133,11 +134,7 @@ export default {
133134

134135
for (const error of errors) {
135136
logger.error(
136-
chalk`Irrelevant flag data detected for {bold ${
137-
error.browser
138-
}}. Remove statement with {bold ${error.flagData
139-
.map((flag) => flag.name)
140-
.join(', ')}} flag`,
137+
`Irrelevant flag data detected for ${styleText('bold', error.browser)}. Remove statement with ${styleText('bold', error.flagData.map((flag) => flag.name).join(', '))} flag`,
141138
{ fixable: true },
142139
);
143140
}

0 commit comments

Comments
 (0)