Skip to content

Commit 5ffa084

Browse files
Saadnajmiclaude
andcommitted
refactor(macos-init): replace chalk with node:util styleText
Drop the chalk dependency in favor of Node's built-in styleText API (available since Node 20.12). Add @types/node@^22 as a devDependency for proper type resolution. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c4d0efa commit 5ffa084

5 files changed

Lines changed: 33 additions & 72 deletions

File tree

packages/react-native-macos-init/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
"prepublishOnly": "npm run build"
1818
},
1919
"bin": "./bin.js",
20+
"engines": {
21+
"node": ">=20.12.0"
22+
},
2023
"dependencies": {
21-
"chalk": "^3",
2224
"find-up": "^4.1.0",
2325
"npm-registry-fetch": "^14.0.0",
2426
"prompts": "^2.3.0",
@@ -27,7 +29,7 @@
2729
},
2830
"devDependencies": {
2931
"@rnx-kit/tsconfig": "^2.0.0",
30-
"@types/chalk": "^2.2.0",
32+
"@types/node": "^22.0.0",
3133
"@types/npm-registry-fetch": "^8.0.0",
3234
"@types/prompts": "^2.0.3",
3335
"@types/semver": "^7.1.0",

packages/react-native-macos-init/src/cli.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* @format
66
*/
77

8-
import chalk from 'chalk';
98
import {execSync} from 'child_process';
109
import * as findUp from 'find-up';
10+
import {styleText} from 'node:util';
1111
import * as fs from 'fs';
1212
import * as npmFetch from 'npm-registry-fetch';
1313
import prompts from 'prompts';
@@ -60,7 +60,7 @@ function getNpmRegistryUrl(): string {
6060
}
6161

6262
function getReactNativeAppName() {
63-
console.log(`Reading ${chalk.cyan('application name')} from package.json…`);
63+
console.log(`Reading ${styleText('cyan', 'application name')} from package.json…`);
6464
const cwd = process.cwd();
6565
const pkgJsonPath = findUp.sync('package.json', {cwd});
6666
if (!pkgJsonPath) {
@@ -73,7 +73,7 @@ function getReactNativeAppName() {
7373
if (!name) {
7474
const appJsonPath = findUp.sync('app.json', {cwd});
7575
if (appJsonPath) {
76-
console.log(`Reading ${chalk.cyan('application name')} from app.json…`);
76+
console.log(`Reading ${styleText('cyan', 'application name')} from app.json…`);
7777
name = JSON.parse(fs.readFileSync(appJsonPath, 'utf8')).name;
7878
}
7979
}
@@ -84,7 +84,7 @@ function getReactNativeAppName() {
8484
}
8585

8686
function getPackageVersion(packageName: string, exitOnError: boolean = true) {
87-
console.log(`Reading ${chalk.cyan(packageName)} version from node_modules…`);
87+
console.log(`Reading ${styleText('cyan', packageName)} version from node_modules…`);
8888

8989
try {
9090
const pkgJsonPath = require.resolve(`${packageName}/package.json`, {
@@ -112,8 +112,8 @@ function getReactNativeMacOSVersion() {
112112
}
113113

114114
function errorOutOnUnsupportedVersionOfReactNative(rnVersion: string): never {
115-
const version = chalk.cyan(rnVersion);
116-
const supportedVersions = chalk.cyan('>=0.60');
115+
const version = styleText('cyan', rnVersion);
116+
const supportedVersions = styleText('cyan', '>=0.60');
117117
printError(
118118
`Unsupported version of ${RNPKG}: ${version}\n${MACOSPKG} supports ${RNPKG} versions ${supportedVersions}`,
119119
);
@@ -208,16 +208,16 @@ function isProjectUsingYarn(cwd: string) {
208208
* Outputs decorated version of the package for the CLI
209209
*/
210210
function printPkg(name: string, version?: string) {
211-
return `${chalk.yellow(name)}${
212-
version ? `${chalk.grey('@')}${chalk.cyan(version)}` : ''
211+
return `${styleText('yellow', name)}${
212+
version ? `${styleText('gray', '@')}${styleText('cyan', version)}` : ''
213213
}`;
214214
}
215215

216216
/**
217217
* Prints decorated version of console.error to the CLI
218218
*/
219219
function printError(message: string, ...optionalParams: any[]) {
220-
console.error(chalk.red(chalk.bold(message)), ...optionalParams);
220+
console.error(styleText(['red', 'bold'], message), ...optionalParams);
221221
}
222222

223223
/**
@@ -237,8 +237,8 @@ async function validatePeerDependencies(
237237
const requiredRN = peerDeps[RNPKG];
238238
if (!semver.satisfies(installedRNVersion, requiredRN)) {
239239
console.warn(
240-
chalk.yellow(
241-
`\n${chalk.bold('Warning:')} ${printPkg(
240+
styleText('yellow',
241+
`\n${styleText('bold', 'Warning:')} ${printPkg(
242242
MACOSPKG,
243243
macosVersion,
244244
)} requires ${printPkg(RNPKG, requiredRN)}, ` +
@@ -278,7 +278,7 @@ async function validatePeerDependencies(
278278

279279
if (!argv.version) {
280280
console.log(
281-
`Latest matching version of ${chalk.green(MACOSPKG)} for ${printPkg(
281+
`Latest matching version of ${styleText('green', MACOSPKG)} for ${printPkg(
282282
RNPKG,
283283
reactNativeVersion,
284284
)} is ${printPkg(MACOSPKG, reactNativeMacOSResolvedVersion)}.`,
@@ -287,20 +287,20 @@ async function validatePeerDependencies(
287287
if (semver.prerelease(reactNativeMacOSResolvedVersion)) {
288288
console.warn(
289289
`
290-
${printPkg(MACOSPKG, reactNativeMacOSResolvedVersion)} is a ${chalk.bgYellow(
290+
${printPkg(MACOSPKG, reactNativeMacOSResolvedVersion)} is a ${styleText('bgYellow',
291291
'pre-release',
292292
)} version.
293293
The latest supported version is ${printPkg(
294294
MACOSPKG,
295295
reactNativeMacOSLatestVersion,
296296
)}.
297-
You can either downgrade your version of ${chalk.yellow(RNPKG)} to ${chalk.cyan(
297+
You can either downgrade your version of ${styleText('yellow', RNPKG)} to ${styleText('cyan',
298298
getMatchingReactNativeSemVerForReactNativeMacOSVersion(
299299
reactNativeMacOSLatestVersion,
300300
),
301-
)}, or continue with a ${chalk.bgYellow(
301+
)}, or continue with a ${styleText('bgYellow',
302302
'pre-release',
303-
)} version of ${chalk.yellow(MACOSPKG)}.
303+
)} version of ${styleText('yellow', MACOSPKG)}.
304304
`,
305305
);
306306

@@ -358,16 +358,16 @@ You can either downgrade your version of ${chalk.yellow(RNPKG)} to ${chalk.cyan(
358358
`Failed to install ${printPkg(MACOSPKG, version)}.\n` +
359359
`This can happen if there is a peer dependency mismatch between ` +
360360
`${RNPKG} and ${MACOSPKG}.\n` +
361-
`Check that your installed version of ${chalk.yellow(RNPKG)} is compatible ` +
361+
`Check that your installed version of ${styleText('yellow', RNPKG)} is compatible ` +
362362
`with ${printPkg(MACOSPKG, version)}.`,
363363
);
364364
process.exit(EXITCODE_UNKNOWN_ERROR);
365365
}
366366

367-
console.log(`${pkgLatest} ${chalk.green('successfully installed!')}`);
367+
console.log(`${pkgLatest} ${styleText('green', 'successfully installed!')}`);
368368
} else {
369369
console.log(
370-
`${chalk.green('Latest version')} of ${pkgLatest} already installed.`,
370+
`${styleText('green', 'Latest version')} of ${pkgLatest} already installed.`,
371371
);
372372
}
373373

packages/react-native/local-cli/generate-macos.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const {
55
copyProjectTemplateAndReplace,
6-
runPodInstall,
76
printFinishMessage,
87
} = require('./generator-macos');
98
const fs = require('fs');
@@ -28,7 +27,6 @@ function generateMacOS (projectDir, name, options) {
2827
{ overwrite: options.overwrite }
2928
);
3029

31-
runPodInstall(options);
3230
printFinishMessage(name);
3331
}
3432

packages/react-native/local-cli/generator-common/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22

33
// @noflow
4-
const chalk = require('chalk');
4+
const { styleText } = require('node:util');
55
const fs = require('fs');
66
const path = require('path');
77

@@ -330,11 +330,11 @@ function alwaysOverwriteContentChangedCallback(
330330
contentChanged
331331
) {
332332
if (contentChanged === 'new') {
333-
console.log(`${chalk.bold('new')} ${relativeDestPath}`);
333+
console.log(`${styleText('bold','new')} ${relativeDestPath}`);
334334
return 'overwrite';
335335
}
336336
if (contentChanged === 'changed') {
337-
console.log(`${chalk.bold('changed')} ${relativeDestPath} ${chalk.yellow('[overwriting]')}`);
337+
console.log(`${styleText('bold','changed')} ${relativeDestPath} ${styleText('yellow','[overwriting]')}`);
338338
return 'overwrite';
339339
}
340340
if (contentChanged === 'identical') {
@@ -351,12 +351,12 @@ function upgradeFileContentChangedCallback(
351351
contentChanged
352352
) {
353353
if (contentChanged === 'new') {
354-
console.log(`${chalk.bold('new')} ${relativeDestPath}`);
354+
console.log(`${styleText('bold','new')} ${relativeDestPath}`);
355355
return 'overwrite';
356356
}
357357
if (contentChanged === 'changed') {
358358
console.log(
359-
`${chalk.bold(relativeDestPath)} ` +
359+
`${styleText('bold',relativeDestPath)} ` +
360360
`has changed in the new version.\nDo you want to keep your ${relativeDestPath} or replace it with the ` +
361361
'latest version?\nIf you ever made any changes ' +
362362
'to this file, you\'ll probably want to keep it.\n' +

packages/react-native/local-cli/generator-macos/index.js

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ const {
77
copyAndReplaceAll,
88
createDir,
99
} = require('../generator-common');
10-
const chalk = require('chalk');
11-
const childProcess = require('child_process');
12-
const fs = require('fs');
10+
const { styleText } = require('node:util');
1311
const path = require('path');
1412

1513
const macOSDir = 'macos';
@@ -102,58 +100,21 @@ function schemePath(basename, platform) {
102100
return path.join(schemesPath(basename), projectName(basename, platform) + '.xcscheme');
103101
}
104102

105-
/**
106-
* @param {{ verbose?: boolean }=} options
107-
*/
108-
function runPodInstall(options) {
109-
const verbose = options && options.verbose;
110-
console.log(`\nRunning ${chalk.bold('pod install --project-directory=macos')}...`);
111-
try {
112-
// Check if pod is available
113-
childProcess.execSync('which pod', { stdio: 'ignore' });
114-
} catch {
115-
console.warn(
116-
chalk.yellow(
117-
`\n${chalk.bold('CocoaPods')} not found. Please install it and run:\n` +
118-
` ${chalk.cyan('pod install --project-directory=macos')}\n`
119-
)
120-
);
121-
return;
122-
}
123-
124-
try {
125-
/** @type {{ stdio?: 'inherit' }} */
126-
const execOptions = verbose ? { stdio: 'inherit' } : {};
127-
childProcess.execSync('pod install --project-directory=macos', execOptions);
128-
console.log(chalk.green('Successfully installed CocoaPods dependencies.'));
129-
} catch (e) {
130-
if (!verbose && e.stderr) {
131-
console.error(e.stderr.toString());
132-
}
133-
console.warn(
134-
chalk.yellow(
135-
`\n${chalk.bold('pod install')} failed. You can retry manually:\n` +
136-
` ${chalk.cyan('pod install --project-directory=macos')}\n`
137-
)
138-
);
139-
}
140-
}
141-
142103
/**
143104
* @param {string} newProjectName
144105
*/
145106
function printFinishMessage(newProjectName) {
146107
console.log(`
147-
${chalk.blue(`Run instructions for ${chalk.bold('macOS')}`)}:
148-
${chalk.cyan('npx react-native run-macos')}
108+
${styleText('blue', `Run instructions for ${styleText('bold', 'macOS')}`)}:
109+
${styleText('cyan', 'pod install --project-directory=macos')}
110+
${styleText('cyan', 'npx react-native run-macos')}
149111
150112
To start the Metro bundler separately:
151-
${chalk.cyan('npx react-native start')}
113+
${styleText('cyan', 'npx react-native start')}
152114
`);
153115
}
154116

155117
module.exports = {
156118
copyProjectTemplateAndReplace,
157-
runPodInstall,
158119
printFinishMessage,
159120
};

0 commit comments

Comments
 (0)