Skip to content

Commit

Permalink
remove rnc/cli-tools version & errors deps (#45380)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
blakef authored and facebook-github-bot committed Jul 12, 2024
1 parent fd40341 commit fa23de7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/community-cli-plugin/src/utils/KeyPressHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
39 changes: 39 additions & 0 deletions packages/community-cli-plugin/src/utils/errors.js
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 1 addition & 1 deletion packages/community-cli-plugin/src/utils/loadMetroConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
25 changes: 24 additions & 1 deletion packages/community-cli-plugin/src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>) => void,
error: (...message: Array<string>) => void,
log: (...message: Array<string>) => void,
info: (...message: Array<string>) => void,
warn: (...message: Array<string>) => void,
...
}>;

export const logger: Logger = communityLogger ?? {
success,
info,
warn,
Expand All @@ -87,3 +106,7 @@ export const logger = {
disable,
enable,
};

if (communityLogger == null) {
logger.debug("Using @react-native/communityu-cli-plugin's logger");
}

0 comments on commit fa23de7

Please sign in to comment.