/
start.impl.ts
119 lines (107 loc) · 3.16 KB
/
start.impl.ts
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
113
114
115
116
117
118
119
import * as chalk from 'chalk';
import { ExecutorContext, logger } from '@nrwl/devkit';
import { ChildProcess, fork } from 'child_process';
import { join } from 'path';
import { ensureNodeModulesSymlink } from '../../utils/ensure-node-modules-symlink';
import { isPackagerRunning } from './lib/is-packager-running';
import { ReactNativeStartOptions } from './schema';
export interface ReactNativeStartOutput {
baseUrl?: string;
success: boolean;
}
let childProcess: ChildProcess;
export default async function* startExecutor(
options: ReactNativeStartOptions,
context: ExecutorContext
): AsyncGenerator<ReactNativeStartOutput> {
const projectRoot = context.workspace.projects[context.projectName].root;
ensureNodeModulesSymlink(context.root, projectRoot);
try {
const baseUrl = `http://localhost:${options.port}`;
const appName = context.projectName;
logger.info(chalk.cyan(`Packager is ready at ${baseUrl}`));
logger.info(
`Use ${chalk.bold(`nx run-android ${appName}`)} or ${chalk.bold(
`nx run-ios ${appName}`
)} to run the native app.`
);
await runCliStart(context.root, projectRoot, options);
yield {
baseUrl,
success: true,
};
} finally {
if (childProcess) {
childProcess.kill();
}
}
}
/*
* Starts the JS bundler and checks for "running" status before notifying
* that packager has started.
*/
export async function runCliStart(
workspaceRoot: string,
projectRoot: string,
options: ReactNativeStartOptions
): Promise<void> {
const result = await isPackagerRunning(options.port);
if (result === 'running') {
logger.info('JS server already running.');
} else if (result === 'unrecognized') {
logger.warn('JS server not recognized.');
} else {
// result === 'not_running'
logger.info('Starting JS server...');
try {
await startAsync(workspaceRoot, projectRoot, options);
} catch (error) {
logger.error(
`Failed to start the packager server. Error details: ${error.message}`
);
throw error;
}
}
}
function startAsync(
workspaceRoot: string,
projectRoot: string,
options: ReactNativeStartOptions
): Promise<number> {
return new Promise((resolve, reject) => {
childProcess = fork(
join(workspaceRoot, './node_modules/react-native/cli.js'),
['start', ...createStartOptions(options)],
{ cwd: join(workspaceRoot, projectRoot) }
);
// Ensure the child process is killed when the parent exits
process.on('exit', () => childProcess.kill());
process.on('SIGTERM', () => childProcess.kill());
childProcess.on('error', (err) => {
reject(err);
});
childProcess.on('exit', (code) => {
if (code === 0) {
resolve(code);
} else {
reject(code);
}
});
});
}
function createStartOptions(options) {
return Object.keys(options).reduce((acc, k) => {
if (k === 'resetCache') {
if (options[k] === true) {
acc.push(`--reset-cache`);
}
} else if (k === 'interactive') {
if (options[k] === false) {
acc.push(`--no-interactive`);
}
} else {
acc.push(`--${k}`, options[k]);
}
return acc;
}, []);
}