-
Notifications
You must be signed in to change notification settings - Fork 8
fix: remove direct dependency @react-native-community/cli
#5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7d756de
5baac49
458ebf7
a8ca414
a52af5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { exec } from 'child_process'; | ||
|
|
||
| interface Config { | ||
| project: { | ||
| android: { | ||
| packageName: string; | ||
| }; | ||
| }; | ||
| root: string; | ||
| } | ||
|
kirillzyusko marked this conversation as resolved.
|
||
|
|
||
| function getConfig(): Promise<Config | null> { | ||
| return new Promise((resolve, reject) => { | ||
| exec('npx react-native config', (error, stdout, stderr) => { | ||
| if (error) { | ||
| console.error(`exec error: ${error}`); | ||
| reject(error); | ||
| return; | ||
| } | ||
|
|
||
| resolve(JSON.parse(stdout)); | ||
|
|
||
| if (stderr) { | ||
| resolve(null); | ||
| throw new Error(stderr); | ||
| } | ||
|
Comment on lines
+21
to
+26
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @szymonrybczak why exactly did we do that here? That on stderr we resolve but throw an error? (cc @kirillzyusko)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm entirely sure what do you mean? Can you specify?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of rejecting on an stderr?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's something inside |
||
| }); | ||
| }); | ||
| } | ||
|
|
||
| export default getConfig; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copy of https://github.com/react-native-community/cli/blob/13.x/packages/cli-hermes/src/profileHermes/metroBundleOptions.ts as `cli-hermes` was recently removed from React Native Community CLI | ||
|
|
||
| import fs from 'fs'; | ||
| import type { HermesCPUProfile } from 'hermes-profile-transformer/dist/types/HermesProfile'; | ||
|
|
||
| export interface MetroBundleOptions { | ||
| platform: string; | ||
| dev: boolean; | ||
| minify: boolean; | ||
| host: string; | ||
| } | ||
|
|
||
| export function getMetroBundleOptions( | ||
| downloadedProfileFilePath: string, | ||
| host: string | ||
| ): MetroBundleOptions { | ||
| let options: MetroBundleOptions = { | ||
| platform: 'android', | ||
| dev: true, | ||
| minify: false, | ||
| host, | ||
|
szymonrybczak marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| try { | ||
| const contents: HermesCPUProfile = JSON.parse( | ||
| fs.readFileSync(downloadedProfileFilePath, { | ||
| encoding: 'utf8', | ||
| }) | ||
| ); | ||
| const matchBundleUrl = /^.*\((.*index\.bundle.*)\)/; | ||
| let containsExpoDevMenu = false; | ||
| let hadMatch = false; | ||
| for (const frame of Object.values(contents.stackFrames)) { | ||
| if (frame.name.includes('EXDevMenuApp')) { | ||
| containsExpoDevMenu = true; | ||
| } | ||
| const match = matchBundleUrl.exec(frame.name); | ||
| if (match) { | ||
| // @ts-ignore | ||
| const parsed = new URL(match[1]); | ||
| const platform = parsed.searchParams.get('platform'), | ||
| dev = parsed.searchParams.get('dev'), | ||
| minify = parsed.searchParams.get('minify'); | ||
| if (platform) { | ||
| options.platform = platform; | ||
| } | ||
| if (dev) { | ||
| options.dev = dev === 'true'; | ||
| } | ||
| if (minify) { | ||
| options.minify = minify === 'true'; | ||
| } | ||
|
|
||
| hadMatch = true; | ||
| break; | ||
| } | ||
| } | ||
| if (containsExpoDevMenu && !hadMatch) { | ||
| console.warn(`Found references to the Expo Dev Menu in your profiling sample. | ||
| You might have accidentally recorded the Expo Dev Menu instead of your own application. | ||
| To work around this, please reload your app twice before starting a profiler recording.`); | ||
| } | ||
| } catch (e) { | ||
| throw e; | ||
| } | ||
|
|
||
| return options; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copy of https://github.com/react-native-community/cli/blob/13.x/packages/cli-hermes/src/profileHermes/metroBundleOptions.ts as `cli-hermes` was recently removed from React Native Community CLI | ||
|
|
||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import os from 'os'; | ||
| import type { SourceMap } from 'hermes-profile-transformer'; | ||
| import type { MetroBundleOptions } from './getMetroBundleOptions'; | ||
|
|
||
| type Config = any; | ||
|
|
||
| function getTempFilePath(filename: string) { | ||
| return path.join(os.tmpdir(), filename); | ||
| } | ||
|
|
||
| function writeJsonSync(targetPath: string, data: any) { | ||
| let json; | ||
| try { | ||
| json = JSON.stringify(data); | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Failed to serialize data to json before writing to ${targetPath}`, | ||
| e as Error | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| fs.writeFileSync(targetPath, json, 'utf-8'); | ||
| } catch (e) { | ||
| throw new Error(`Failed to write json to ${targetPath}`, e as Error); | ||
| } | ||
| } | ||
|
|
||
| async function getSourcemapFromServer( | ||
| port: string, | ||
| { platform, dev, minify, host }: MetroBundleOptions | ||
| ): Promise<SourceMap | undefined> { | ||
| console.log('Getting source maps from Metro packager server'); | ||
|
|
||
| const requestURL = `http://${host}:${port}/index.map?platform=${platform}&dev=${dev}&minify=${minify}`; | ||
| console.log(`Downloading from ${requestURL}`); | ||
|
hannojg marked this conversation as resolved.
|
||
| try { | ||
| // @ts-ignore | ||
| const { data } = await fetch(requestURL); | ||
| return data as SourceMap; | ||
| } catch (e) { | ||
| console.log(`Failed to fetch source map from "${requestURL}"`); | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generate a sourcemap by fetching it from a running metro server | ||
| */ | ||
| export async function generateSourcemap( | ||
| port: string, | ||
| bundleOptions: MetroBundleOptions | ||
| ): Promise<string | undefined> { | ||
| // Fetch the source map to a temp directory | ||
| const sourceMapPath = getTempFilePath('index.map'); | ||
| const sourceMapResult = await getSourcemapFromServer(port, bundleOptions); | ||
|
|
||
| if (sourceMapResult) { | ||
| console.log('Using source maps from Metro packager server'); | ||
| writeJsonSync(sourceMapPath, sourceMapResult); | ||
| console.log( | ||
| `Successfully obtained the source map and stored it in ${sourceMapPath}` | ||
| ); | ||
| return sourceMapPath; | ||
| } else { | ||
| console.log('Error: Cannot obtain source maps from Metro packager server'); | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @param ctx | ||
| */ | ||
| export async function findSourcemap( | ||
| ctx: Config, | ||
| port: string, | ||
| bundleOptions: MetroBundleOptions | ||
| ): Promise<string | undefined> { | ||
| const intermediateBuildPath = path.join( | ||
| ctx.root, | ||
| 'android', | ||
| 'app', | ||
| 'build', | ||
| 'intermediates', | ||
| 'sourcemaps', | ||
| 'react', | ||
| 'debug', | ||
| 'index.android.bundle.packager.map' | ||
| ); | ||
|
|
||
| const generatedBuildPath = path.join( | ||
| ctx.root, | ||
| 'android', | ||
| 'app', | ||
| 'build', | ||
| 'generated', | ||
| 'sourcemaps', | ||
| 'react', | ||
| 'debug', | ||
| 'index.android.bundle.map' | ||
| ); | ||
|
|
||
| if (fs.existsSync(generatedBuildPath)) { | ||
| console.log(`Getting the source map from ${generateSourcemap}`); | ||
| return generatedBuildPath; | ||
| } else if (fs.existsSync(intermediateBuildPath)) { | ||
| console.log(`Getting the source map from ${intermediateBuildPath}`); | ||
| return intermediateBuildPath; | ||
| } else { | ||
| return generateSourcemap(port, bundleOptions); | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.