UNPKG

4.85 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7function _path() {
8 const data = _interopRequireDefault(require("path"));
9 _path = function () {
10 return data;
11 };
12 return data;
13}
14function _fs() {
15 const data = _interopRequireDefault(require("fs"));
16 _fs = function () {
17 return data;
18 };
19 return data;
20}
21function _execa() {
22 const data = _interopRequireDefault(require("execa"));
23 _execa = function () {
24 return data;
25 };
26 return data;
27}
28var _logger = _interopRequireDefault(require("./logger"));
29function _chalk() {
30 const data = _interopRequireDefault(require("chalk"));
31 _chalk = function () {
32 return data;
33 };
34 return data;
35}
36var _findPackageDependencyDir = require("./findPackageDependencyDir");
37function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
38const ERROR = `a dev server manually by running ${_chalk().default.bold('npm start')} or ${_chalk().default.bold('yarn start')} in other terminal window.`;
39function startServerInNewWindow(port, projectRoot, reactNativePath, terminal) {
40 if (!terminal) {
41 _logger.default.error('Cannot start server in new windows because no terminal app was specified, use --terminal to specify, or start ' + ERROR);
42 return;
43 }
44
45 /**
46 * Set up OS-specific filenames and commands
47 */
48 const isWindows = /^win/.test(process.platform);
49 const scriptFile = isWindows ? 'launchPackager.bat' : 'launchPackager.command';
50 const packagerEnvFilename = isWindows ? '.packager.bat' : '.packager.env';
51 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}"`;
52 let generatedPath = (0, _findPackageDependencyDir.findPackageDependencyDir)('.generated', {
53 startDir: projectRoot
54 });
55 if (!generatedPath) {
56 const newPath = _path().default.join(projectRoot, 'node_modules', '.generated');
57 _fs().default.mkdirSync(newPath, {
58 recursive: true,
59 mode: 0o755
60 });
61 generatedPath = newPath;
62 }
63 const cliPluginMetroPath = _path().default.join(_path().default.dirname(require.resolve('@react-native-community/cli-tools/package.json')), 'build');
64
65 /**
66 * Set up the `.packager.(env|bat)` file to ensure the packager starts on the right port and in right directory.
67 */
68 const packagerEnvFile = _path().default.join(generatedPath, `${packagerEnvFilename}`);
69
70 /**
71 * Set up the `launchPackager.(command|bat)` file.
72 * It lives next to `.packager.(bat|env)`
73 */
74 const launchPackagerScript = _path().default.join(generatedPath, scriptFile);
75 const procConfig = {
76 cwd: _path().default.dirname(packagerEnvFile)
77 };
78
79 /**
80 * Ensure we overwrite file by passing the `w` flag
81 */
82 _fs().default.writeFileSync(packagerEnvFile, packagerEnvFileExportContent, {
83 encoding: 'utf8',
84 flag: 'w'
85 });
86
87 /**
88 * Copy files into `node_modules/.generated`.
89 */
90
91 try {
92 if (isWindows) {
93 _fs().default.copyFileSync(_path().default.join(cliPluginMetroPath, 'launchPackager.bat'), _path().default.join(generatedPath, 'launchPackager.bat'));
94 } else {
95 _fs().default.copyFileSync(_path().default.join(cliPluginMetroPath, 'launchPackager.command'), _path().default.join(generatedPath, 'launchPackager.command'));
96 }
97 } catch (error) {
98 _logger.default.error(`Couldn't copy the script for running bundler. Please check if the "${scriptFile}" file exists in the "node_modules/@react-native-community/cli-tools" folder, or start ` + ERROR, error);
99 return;
100 }
101 if (process.platform === 'darwin') {
102 try {
103 return _execa().default.sync('open', ['-a', terminal, launchPackagerScript], procConfig);
104 } catch (error) {
105 return _execa().default.sync('open', [launchPackagerScript], procConfig);
106 }
107 }
108 if (process.platform === 'linux') {
109 try {
110 return _execa().default.sync(terminal, ['-e', `sh ${launchPackagerScript}`], {
111 ...procConfig,
112 detached: true
113 });
114 } catch (error) {
115 // By default, the child shell process will be attached to the parent
116 return _execa().default.sync('sh', [launchPackagerScript], procConfig);
117 }
118 }
119 if (isWindows) {
120 // Awaiting this causes the CLI to hang indefinitely, so this must execute without await.
121 return (0, _execa().default)(terminal, ['/C', launchPackagerScript], {
122 ...procConfig,
123 detached: true,
124 stdio: 'ignore'
125 });
126 }
127 _logger.default.error(`Cannot start the packager. Unknown platform ${process.platform}. Try starting ` + ERROR);
128 return;
129}
130var _default = startServerInNewWindow;
131exports.default = _default;
132
133//# sourceMappingURL=startServerInNewWindow.ts.map
\No newline at end of file