forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartServerInNewWindow.ts
More file actions
112 lines (105 loc) · 3.31 KB
/
startServerInNewWindow.ts
File metadata and controls
112 lines (105 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import path from 'path';
import fs from 'fs';
import execa from 'execa';
import {
CLIError,
logger,
resolveNodeModuleDir,
} from '@react-native-community/cli-tools';
export function startServerInNewWindow(
port: number,
terminal: string,
projectRoot: string,
reactNativePath: string,
) {
/**
* Set up OS-specific filenames and commands
*/
const isWindows = /^win/.test(process.platform);
const scriptFile = isWindows
? 'launchPackager.bat'
: 'launchPackager.command';
const packagerEnvFilename = isWindows ? '.packager.bat' : '.packager.env';
const packagerEnvFileExportContent = isWindows
? `set RCT_METRO_PORT=${port}\nset PROJECT_ROOT=${projectRoot}\nset REACT_NATIVE_PATH=${reactNativePath}`
: `export RCT_METRO_PORT=${port}\nexport PROJECT_ROOT=${projectRoot}\nexport REACT_NATIVE_PATH=${reactNativePath}`;
const nodeModulesPath = resolveNodeModuleDir(projectRoot, '.bin');
const cliPluginMetroPath = path.join(
path.dirname(
require.resolve('@react-native-community/cli-plugin-metro/package.json'),
),
'build',
);
/**
* Set up the `.packager.(env|bat)` file to ensure the packager starts on the right port and in right directory.
*/
const packagerEnvFile = path.join(nodeModulesPath, `${packagerEnvFilename}`);
/**
* Set up the `launchPackager.(command|bat)` file.
* It lives next to `.packager.(bat|env)`
*/
const launchPackagerScript = path.join(nodeModulesPath, scriptFile);
const procConfig: execa.SyncOptions = {cwd: path.dirname(packagerEnvFile)};
/**
* Ensure we overwrite file by passing the `w` flag
*/
fs.writeFileSync(packagerEnvFile, packagerEnvFileExportContent, {
encoding: 'utf8',
flag: 'w',
});
/**
* Copy files into `node_modules/.bin`.
*/
try {
if (isWindows) {
fs.copyFileSync(
path.join(cliPluginMetroPath, 'launchPackager.bat'),
path.join(nodeModulesPath, 'launchPackager.bat'),
);
} else {
fs.copyFileSync(
path.join(cliPluginMetroPath, 'launchPackager.command'),
path.join(nodeModulesPath, 'launchPackager.command'),
);
}
} catch (error) {
return new CLIError(
`Couldn't copy the script for running bundler. Please check if the "${scriptFile}" file exists in the "node_modules/@react-native-community/cli-plugin-metro" folder and try again.`,
error as any,
);
}
if (process.platform === 'darwin') {
try {
return execa.sync(
'open',
['-a', terminal, launchPackagerScript],
procConfig,
);
} catch (error) {
return execa.sync('open', [launchPackagerScript], procConfig);
}
}
if (process.platform === 'linux') {
try {
return execa.sync(terminal, ['-e', `sh ${launchPackagerScript}`], {
...procConfig,
detached: true,
});
} catch (error) {
// By default, the child shell process will be attached to the parent
return execa.sync('sh', [launchPackagerScript], procConfig);
}
}
if (isWindows) {
// Awaiting this causes the CLI to hang indefinitely, so this must execute without await.
return execa('cmd.exe', ['/C', launchPackagerScript], {
...procConfig,
detached: true,
stdio: 'ignore',
});
}
logger.error(
`Cannot start the packager. Unknown platform ${process.platform}`,
);
return;
}