forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip ci] Add metro app building support (facebook#44466)
Summary: Contains a *light* wrapper to help launch Metro and build bundles or wait for localhost requests against Metro's dev-server. Changelog: [Internal] Differential Revision: D57067040
- Loading branch information
1 parent
b3cbd4a
commit 082a23e
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import type {Task} from './types'; | ||
import type {ExecaPromise} from 'execa'; | ||
|
||
import {task} from './utils'; | ||
import execa from 'execa'; | ||
import path from 'path'; | ||
|
||
type AppOptions = { | ||
cwd: string, | ||
}; | ||
|
||
type BundleOptions = | ||
| {mode: 'bundle', ...AppOptions} | ||
| {mode: 'watch', callback?: (metro: ExecaPromise) => void, ...AppOptions}; | ||
|
||
const FIRST = 1, | ||
SECOND = 2; | ||
|
||
function getNodePackagePath(packageName: string): string { | ||
// $FlowFixMe[prop-missing] type definition is incomplete | ||
return require.resolve(packageName, {cwd: [process.cwd(), ...module.paths]}); | ||
} | ||
|
||
function metro(...args: $ReadOnlyArray<string>): ExecaPromise { | ||
return execa('node', [ | ||
getNodePackagePath(path.join('metro', 'src', 'cli.js')), | ||
...args, | ||
]); | ||
} | ||
|
||
const noMetro = new Error('Metro is not available'); | ||
|
||
export const tasks = { | ||
bundle: ( | ||
options: BundleOptions, | ||
...args: $ReadOnlyArray<string> | ||
): { | ||
validate: Task<void>, | ||
run: Task<ExecaPromise>, | ||
} => ({ | ||
/* eslint-disable sort-keys */ | ||
validate: task(FIRST, 'Check if Metro is available', () => { | ||
try { | ||
require('metro'); | ||
} catch { | ||
throw noMetro; | ||
} | ||
}), | ||
|
||
run: | ||
options.mode === 'bundle' | ||
? task(SECOND, 'Metro generating an .jsbundle', () => | ||
metro('bundle', ...args), | ||
) | ||
: task(SECOND, 'Metro watching for changes', () => { | ||
const proc = metro('serve', ...args); | ||
return proc; | ||
}), | ||
}), | ||
}; |