Skip to content

Commit b9dde72

Browse files
authored
fix(cli): fix undeclared chalk dependency (microsoft#2819)
## Summary: macOS bundling fails because it tries to import `chalk` but does not declare it. ``` /~/packages/app/example/node_modules/react-native-macos/react-native.config.js: node:internal/modules/cjs/loader:1421 const err = new Error(message); ^ Error: Cannot find module 'chalk' Require stack: - /~/node_modules/.store/react-native-macos-virtual-89732d1908/package/local-cli/runMacOS/runMacOS.js - /~/node_modules/.store/react-native-macos-virtual-89732d1908/package/react-native.config.js at Module._resolveFilename (node:internal/modules/cjs/loader:1421:15) at defaultResolveImpl (node:internal/modules/cjs/loader:1059:19) at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1064:22) at Module._load (node:internal/modules/cjs/loader:1227:37) at TracingChannel.traceSync (node:diagnostics_channel:328:14) at wrapModuleLoad (node:internal/modules/cjs/loader:245:24) at Module.require (node:internal/modules/cjs/loader:1504:12) at require (node:internal/modules/helpers:152:16) at Object.<anonymous> (/~/node_modules/.store/react-native-macos-virtual-89732d1908/package/local-cli/runMacOS/runMacOS.js:38:15) at Module._compile (node:internal/modules/cjs/loader:1761:14) { code: 'MODULE_NOT_FOUND', requireStack: [ '/~/node_modules/.store/react-native-macos-virtual-89732d1908/package/local-cli/runMacOS/runMacOS.js', '/~/node_modules/.store/react-native-macos-virtual-89732d1908/package/react-native.config.js' ] } ``` Instead of adding `chalk` as a dependency (and introducing a diff), we inline the functions we use. ## Test Plan: n/a
1 parent c597bf8 commit b9dde72

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

packages/react-native/local-cli/runMacOS/runMacOS.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,27 @@
3535
* }} ProjectConfig
3636
*/
3737

38-
const chalk = require('chalk');
39-
const child_process = require('child_process');
40-
const path = require('path');
38+
const child_process = require('node:child_process');
39+
const path = require('node:path');
4140

42-
const {logger, CLIError, getDefaultUserTerminal} = (() => {
43-
const cli = require.resolve('@react-native-community/cli/package.json');
41+
const colors = (() => {
42+
const {WriteStream} = require('node:tty');
43+
if (WriteStream.prototype.hasColors() &&
44+
!process.env.NODE_TEST_CONTEXT &&
45+
process.env.NODE_ENV !== 'test'
46+
) {
47+
return {
48+
bold: (s) => '\u001B[1m' + s + '\u001B[22m',
49+
dim: (s) => '\u001B[2m' + s + '\u001B[22m',
50+
}
51+
}
52+
53+
const passthrough = (s) => s;
54+
return { bold: passthrough, dim: passthrough };
55+
})();
56+
57+
const {logger, CLIError, getDefaultUserTerminal} = ((projectRoot = process.cwd()) => {
58+
const cli = require.resolve('@react-native-community/cli/package.json', {paths: [projectRoot]});
4459
const options = {paths: [path.dirname(cli)]};
4560
const tools = require.resolve('@react-native-community/cli-tools', options);
4661
return require(tools);
@@ -92,7 +107,7 @@ function parseArgs(ctx, args) {
92107
logger.info(
93108
`Found Xcode ${
94109
xcodeProject.isWorkspace ? 'workspace' : 'project'
95-
} "${chalk.bold(xcodeProject.name)}"`,
110+
} "${colors.bold(xcodeProject.name)}"`,
96111
);
97112

98113
return {sourceDir, xcodeProject, scheme};
@@ -146,7 +161,7 @@ async function run(sourceDir, xcodeProject, scheme, args) {
146161
.trim();
147162

148163
logger.info(
149-
`Launching app "${chalk.bold(bundleID)}" from "${chalk.bold(appPath)}"`,
164+
`Launching app "${colors.bold(bundleID)}" from "${colors.bold(appPath)}"`,
150165
);
151166

152167
child_process.exec(
@@ -179,7 +194,7 @@ function buildProject(sourceDir, xcodeProject, scheme, args) {
179194
scheme,
180195
];
181196
logger.info(
182-
`Building ${chalk.dim(
197+
`Building ${colors.dim(
183198
`(using "xcodebuild ${xcodebuildArgs.join(' ')}")`,
184199
)}`,
185200
);

0 commit comments

Comments
 (0)