1 | /**
|
2 | * Copyright (c) Facebook, Inc. and its affiliates.
|
3 | *
|
4 | * This source code is licensed under the MIT license found in the
|
5 | * LICENSE file in the root directory of this source tree.
|
6 | *
|
7 | */
|
8 |
|
9 | import {fetch} from './fetch';
|
10 |
|
11 | /**
|
12 | * Indicates whether or not the packager is running. It returns a promise that
|
13 | * returns one of these possible values:
|
14 | * - `running`: the packager is running
|
15 | * - `not_running`: the packager nor any process is running on the expected port.
|
16 | * - `unrecognized`: one other process is running on the port we expect the packager to be running.
|
17 | */
|
18 | async function isPackagerRunning(
|
19 | packagerPort: string | number = process.env.RCT_METRO_PORT || '8081',
|
20 | ): Promise<'running' | 'not_running' | 'unrecognized'> {
|
21 | try {
|
22 | const {data} = await fetch(`http://localhost:${packagerPort}/status`);
|
23 |
|
24 | return data === 'packager-status:running' ? 'running' : 'unrecognized';
|
25 | } catch (_error) {
|
26 | return 'not_running';
|
27 | }
|
28 | }
|
29 |
|
30 | export default isPackagerRunning;
|