Skip to content

Commit

Permalink
fix: add base terminal guessing function to launch metro
Browse files Browse the repository at this point in the history
  • Loading branch information
rams23 committed Dec 22, 2023
1 parent 94e0b89 commit a7f0777
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions packages/cli/src/application/metroManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,66 @@ import path from 'path';
import { getProjectRootDir, sleep } from './utils';
import { getDefaultUserTerminal, startServerInNewWindow } from '@react-native-community/cli-tools';


import { execSync } from 'child_process';

function getTerminalPath(): string {
try {
if (process.platform === 'win32') {
// Default to PowerShell on Windows
let defaultTerminal = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe';

try {
// Attempt to identify if it's Command Prompt
const parentProcessId = process.ppid;
const query = `wmic process where (ProcessId=${parentProcessId}) get CommandLine`;
const parentProcessCmdLine = execSync(query).toString();

if (parentProcessCmdLine.includes('cmd.exe')) {
defaultTerminal = 'C:\\Windows\\System32\\cmd.exe';
}
} catch (error) {
console.warn('Could not determine specific terminal, defaulting to PowerShell.');
}

return defaultTerminal;
} else {
// Default to /bin/bash or /bin/sh on Unix-like systems
let defaultTerminal = '/bin/bash';

try {
const ppid = process.ppid;
const command = `ps -o comm= -p ${ppid}`;
const terminalPath = execSync(command).toString().trim();

if (terminalPath) {
defaultTerminal = terminalPath;
}
} catch (error) {
console.warn('Could not determine specific terminal, defaulting to /bin/bash.');
}

return defaultTerminal;
}
} catch (error) {
console.error('Error obtaining terminal path:', error);
return 'Error';
}
}
export async function startMetro(resetCache = false, port = '8081') {
startServerInNewWindow(
Number(port),
getProjectRootDir(),
path.join(getProjectRootDir(), 'node_modules/react-native'),
getDefaultUserTerminal()!,
);
try {
console.log('getDefaultUserTerminal()', getTerminalPath());
startServerInNewWindow(
Number(port),
getProjectRootDir(),
path.join(getProjectRootDir(), 'node_modules/react-native'),
getTerminalPath(),
);
}catch (e){
console.error(e);
throw e;
}


while (!(await checkIsMetroRunning(port))) {
await sleep(100);
Expand Down

0 comments on commit a7f0777

Please sign in to comment.