Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/community-cli-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@react-native/dev-middleware": "0.77.0-main",
"@react-native/metro-babel-transformer": "0.77.0-main",
"chalk": "^4.0.0",
"execa": "^5.1.1",
"invariant": "^2.2.4",
"metro": "^0.81.0-alpha.2",
"metro-config": "^0.81.0-alpha.2",
"metro-core": "^0.81.0-alpha.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import type {Config} from '@react-native-community/cli-types';
import type TerminalReporter from 'metro/src/lib/TerminalReporter';

import {KeyPressHandler} from '../../utils/KeyPressHandler';
import {logger} from '../../utils/logger';
import OpenDebuggerKeyboardHandler from './OpenDebuggerKeyboardHandler';
import chalk from 'chalk';
import execa from 'execa';
import {spawn} from 'child_process';
import invariant from 'invariant';
import readline from 'readline';
import {ReadStream} from 'tty';

const CTRL_C = '\u0003';
const CTRL_D = '\u0004';
Expand All @@ -33,6 +35,18 @@ const throttle = (callback: () => void, timeout: number) => {
};
};

type KeyEvent = {
sequence: string,
name: string,
ctrl: boolean,
meta: boolean,
shift: boolean,
};

const spawnOptions = {
env: {...process.env, FORCE_COLOR: chalk.supportsColor ? 'true' : 'false'},
};

export default function attachKeyHandlers({
cliConfig,
devServerUrl,
Expand All @@ -52,9 +66,8 @@ export default function attachKeyHandlers({
return;
}

const execaOptions = {
env: {FORCE_COLOR: chalk.supportsColor ? 'true' : 'false'},
};
readline.emitKeypressEvents(process.stdin);
setRawMode(true);

const reload = throttle(() => {
logger.info('Reloading connected app(s)...');
Expand All @@ -66,12 +79,14 @@ export default function attachKeyHandlers({
devServerUrl,
});

const onPress = async (key: string) => {
if (openDebuggerKeyboardHandler.maybeHandleTargetSelection(key)) {
process.stdin.on('keypress', (str: string, key: KeyEvent) => {
logger.debug(`Key pressed: ${key.sequence}`);

if (openDebuggerKeyboardHandler.maybeHandleTargetSelection(key.name)) {
return;
}

switch (key.toLowerCase()) {
switch (key.sequence) {
case 'r':
reload();
break;
Expand All @@ -81,44 +96,42 @@ export default function attachKeyHandlers({
break;
case 'i':
logger.info('Opening app on iOS...');
execa(
spawn(
'npx',
[
'react-native',
'run-ios',
...(cliConfig.project.ios?.watchModeCommandParams ?? []),
],
execaOptions,
spawnOptions,
).stdout?.pipe(process.stdout);
break;
case 'a':
logger.info('Opening app on Android...');
execa(
spawn(
'npx',
[
'react-native',
'run-android',
...(cliConfig.project.android?.watchModeCommandParams ?? []),
],
execaOptions,
spawnOptions,
).stdout?.pipe(process.stdout);
break;
case 'j':
await openDebuggerKeyboardHandler.handleOpenDebugger();
// eslint-disable-next-line no-void
void openDebuggerKeyboardHandler.handleOpenDebugger();
break;
case CTRL_C:
case CTRL_D:
openDebuggerKeyboardHandler.dismiss();
logger.info('Stopping server');
keyPressHandler.stopInterceptingKeyStrokes();
setRawMode(false);
process.stdin.pause();
process.emit('SIGINT');
process.exit();
}
};

const keyPressHandler = new KeyPressHandler(onPress);
keyPressHandler.createInteractionListener();
keyPressHandler.startInterceptingKeyStrokes();
});

logger.log(
[
Expand All @@ -132,3 +145,11 @@ export default function attachKeyHandlers({
].join('\n'),
);
}

function setRawMode(enable: boolean) {
invariant(
process.stdin instanceof ReadStream,
'process.stdin must be a readable stream to modify raw mode',
);
process.stdin.setRawMode(enable);
}
90 changes: 0 additions & 90 deletions packages/community-cli-plugin/src/utils/KeyPressHandler.js

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4204,7 +4204,7 @@ eventemitter3@^5.0.1:
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-5.0.1.tgz#53f5ffd0a492ac800721bb42c66b841de96423c4"
integrity sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==

execa@^5.0.0, execa@^5.1.1:
execa@^5.0.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
integrity sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==
Expand Down