From f111aa9c851d6dc692d1049f957ea479269acf83 Mon Sep 17 00:00:00 2001 From: Blake Friedman Date: Mon, 15 Jul 2024 03:29:10 -0700 Subject: [PATCH] remove rnc/cli-tools version & errors deps (#45380) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45380 Removed the use of version checking and error code that is in react-native-community/cli-tools. Changelog: [Internal] [Changed] - Removed community-cli-plugin version & error dependencies Reviewed By: robhogan Differential Revision: D59378012 --- .../src/commands/start/runServer.js | 2 +- .../src/utils/KeyPressHandler.js | 2 +- .../community-cli-plugin/src/utils/errors.js | 39 +++++++++++++++++++ .../src/utils/loadMetroConfig.js | 2 +- .../community-cli-plugin/src/utils/logger.js | 25 +++++++++++- 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 packages/community-cli-plugin/src/utils/errors.js diff --git a/packages/community-cli-plugin/src/commands/start/runServer.js b/packages/community-cli-plugin/src/commands/start/runServer.js index a89c192db5fff8..9103350e2bb538 100644 --- a/packages/community-cli-plugin/src/commands/start/runServer.js +++ b/packages/community-cli-plugin/src/commands/start/runServer.js @@ -17,12 +17,12 @@ import typeof TerminalReporter from 'metro/src/lib/TerminalReporter'; import isDevServerRunning from '../../utils/isDevServerRunning'; import loadMetroConfig from '../../utils/loadMetroConfig'; import {logger} from '../../utils/logger'; +import * as version from '../../utils/version'; import attachKeyHandlers from './attachKeyHandlers'; import { createDevServerMiddleware, indexPageMiddleware, } from '@react-native-community/cli-server-api'; -import {version} from '@react-native-community/cli-tools'; import {createDevMiddleware} from '@react-native/dev-middleware'; import chalk from 'chalk'; import Metro from 'metro'; diff --git a/packages/community-cli-plugin/src/utils/KeyPressHandler.js b/packages/community-cli-plugin/src/utils/KeyPressHandler.js index bd7ae9bb044c7c..af3609f8da4427 100644 --- a/packages/community-cli-plugin/src/utils/KeyPressHandler.js +++ b/packages/community-cli-plugin/src/utils/KeyPressHandler.js @@ -9,8 +9,8 @@ * @oncall react_native */ +import {CLIError} from './errors'; import {logger} from './logger'; -import {CLIError} from '@react-native-community/cli-tools'; const CTRL_C = '\u0003'; diff --git a/packages/community-cli-plugin/src/utils/errors.js b/packages/community-cli-plugin/src/utils/errors.js new file mode 100644 index 00000000000000..9f18ec69c793da --- /dev/null +++ b/packages/community-cli-plugin/src/utils/errors.js @@ -0,0 +1,39 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + * @oncall react_native + */ + +/** + * A custom Error that creates a single-lined message to match current styling inside CLI. + * Uses original stack trace when `originalError` is passed or erase the stack if it's not defined. + */ +export class CLIError extends Error { + constructor(msg: string, originalError?: Error | string) { + super(inlineString(msg)); + if (originalError != null) { + this.stack = + typeof originalError === 'string' + ? originalError + : originalError.stack || ''.split('\n').slice(0, 2).join('\n'); + } else { + // When the "originalError" is not passed, it means that we know exactly + // what went wrong and provide means to fix it. In such cases showing the + // stack is an unnecessary clutter to the CLI output, hence removing it. + this.stack = ''; + } + } +} + +/** + * Raised when we're unable to find a package.json + */ +export class UnknownProjectError extends Error {} + +export const inlineString = (str: string = ''): string => + str.replace(/(\s{2,})/gm, ' ').trim(); diff --git a/packages/community-cli-plugin/src/utils/loadMetroConfig.js b/packages/community-cli-plugin/src/utils/loadMetroConfig.js index 8e31a49c109d70..52fb398795a51e 100644 --- a/packages/community-cli-plugin/src/utils/loadMetroConfig.js +++ b/packages/community-cli-plugin/src/utils/loadMetroConfig.js @@ -12,9 +12,9 @@ import type {Config} from '@react-native-community/cli-types'; import type {ConfigT, InputConfigT, YargArguments} from 'metro-config'; +import {CLIError} from './errors'; import {logger} from './logger'; import {reactNativePlatformResolver} from './metroPlatformResolver'; -import {CLIError} from '@react-native-community/cli-tools'; import {loadConfig, mergeConfig, resolveConfig} from 'metro-config'; import path from 'path'; diff --git a/packages/community-cli-plugin/src/utils/logger.js b/packages/community-cli-plugin/src/utils/logger.js index 3a401b8768b5fc..6b30a5287ebe10 100644 --- a/packages/community-cli-plugin/src/utils/logger.js +++ b/packages/community-cli-plugin/src/utils/logger.js @@ -74,7 +74,26 @@ const enable = () => { const hasDebugMessages = (): boolean => hidden; -export const logger = { +let communityLogger; +try { + const {logger} = require('@react-native-community/cli-tools'); + logger.debug("Using @react-naive-community/cli-tools' logger"); + communityLogger = logger; +} catch { + // This is no longer a required dependency in react-native projects, but use it instead of + // our forked version if it's available. Fail silently otherwise. +} + +type Logger = $ReadOnly<{ + debug: (...message: Array) => void, + error: (...message: Array) => void, + log: (...message: Array) => void, + info: (...message: Array) => void, + warn: (...message: Array) => void, + ... +}>; + +export const logger: Logger = communityLogger ?? { success, info, warn, @@ -87,3 +106,7 @@ export const logger = { disable, enable, }; + +if (communityLogger == null) { + logger.debug("Using @react-native/communityu-cli-plugin's logger"); +}