forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feat/generate source map #6
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2285b38
generated source map through
jessieAnhNguyen 652290d
fixed SHA-1 error
jessieAnhNguyen deffdd2
Merge branch 'master' of https://github.com/MLH-Fellowship/cli into f…
jessieAnhNguyen f560481
implemented the source map steps
jessieAnhNguyen e3fe062
added transformer package and profile-hermes.md
jessieAnhNguyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -6,6 +6,9 @@ import fs from 'fs'; | |
import path from 'path'; | ||
import os from 'os'; | ||
import {transformer} from './transformer'; | ||
import axios, {AxiosResponse} from 'axios'; | ||
import ip from 'ip'; | ||
import {SourceMap} from './EventInterfaces'; | ||
|
||
/** | ||
* Get the last modified hermes profile | ||
|
@@ -64,6 +67,83 @@ function validatePackageName(packageName: string) { | |
return /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/.test(packageName); | ||
} | ||
|
||
async function getSourcemapFromServer(): Promise<AxiosResponse<SourceMap>> { | ||
const DEBUG_SERVER_PORT = '8081'; | ||
const IP_ADDRESS = ip.address(); | ||
const PLATFORM = 'android'; | ||
const requestURL = `http://${IP_ADDRESS}:${DEBUG_SERVER_PORT}/index.map?platform=${PLATFORM}&dev=true`; | ||
return (await axios.get(requestURL)) as AxiosResponse<SourceMap>; | ||
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 don't think we need axios for this. Taking the dependency for a single http request is most likely not going to be popular among the react-native-cli maintainers |
||
} | ||
|
||
async function getSourcemapPath( | ||
ctx: Config, | ||
generateSourceMap?: boolean, | ||
): Promise<string> { | ||
const osTmpDir = os.tmpdir(); | ||
//If '--generate-sourcemap, generate the bundle and source map files and store them in os.tmpDir() | ||
if (generateSourceMap) { | ||
console.log('generate new source map'); | ||
//Store the file in os.tmpDir() | ||
const sourceMapPath = path.join(osTmpDir, 'index.map'); | ||
|
||
const sourceMapResult = await getSourcemapFromServer(); | ||
if (sourceMapResult) { | ||
console.log('Request from server: done finding the source map'); | ||
fs.writeFileSync( | ||
`${sourceMapPath}`, | ||
JSON.stringify(sourceMapResult.data), | ||
'utf-8', | ||
); | ||
console.log('Request from server: done writing the source map'); | ||
} else { | ||
console.log('begin to build bundle'); | ||
execSync( | ||
`react-native bundle --entry-file index.js --bundle-output ${path.join( | ||
osTmpDir, | ||
'index.bundle', | ||
)} --sourcemap-output ${sourceMapPath}`, | ||
); | ||
} | ||
logger.info( | ||
`Successfully generated the source map and store it in ${sourceMapPath}`, | ||
); | ||
return `${sourceMapPath}`; | ||
} | ||
|
||
//Find the sourcemap if it exists | ||
else { | ||
console.log('find the existing source map'); | ||
//Find from local machine | ||
//QUESTION: why is my source map path different from Jani's | ||
//(android/app/build/generated/sourcemaps/react/debug/index.android.bundle.map) | ||
const sourceMapDir = path.join( | ||
ctx.root, | ||
'android', | ||
'app', | ||
'build', | ||
'intermediates', //'generated', | ||
'sourcemaps', | ||
'react', | ||
'debug', | ||
'index.android.bundle.packager.map', | ||
); | ||
console.log(sourceMapDir); | ||
if (fs.existsSync(sourceMapDir)) { | ||
console.log('get the sourcemap if it exists from local machine'); | ||
return sourceMapDir; | ||
} else { | ||
//Request from server | ||
const sourceMapResult = await getSourcemapFromServer(); | ||
fs.writeFileSync( | ||
`${path.join(osTmpDir, 'index.map')}`, | ||
JSON.stringify(sourceMapResult.data), | ||
'utf-8', | ||
); | ||
return `${path.join(osTmpDir, 'index.map')}`; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Executes the commands to pull a hermes profile | ||
*/ | ||
|
@@ -73,8 +153,10 @@ export async function downloadProfile( | |
fileName?: string, | ||
sourceMapPath?: string, | ||
raw?: boolean, | ||
generateSourceMap?: boolean, | ||
) { | ||
try { | ||
//console.log('start the profile command'); | ||
const packageName = getPackageName(ctx); | ||
//if not specify fileName, pull the latest file | ||
const file = fileName || (await getLatestFile(packageName)); | ||
|
@@ -84,11 +166,12 @@ export async function downloadProfile( | |
); | ||
process.exit(1); | ||
} | ||
logger.info(`File to be pulled: ${file}`); | ||
|
||
//if not specify destination path, pull to the current directory | ||
if (!dstPath) { | ||
dstPath = ctx.root; | ||
} | ||
logger.info(`File to be pulled: ${file}`); | ||
//if '--verbose' is enabled | ||
if (logger.isVerbose()) { | ||
logger.info('Internal commands run to pull the file: '); | ||
|
@@ -103,13 +186,34 @@ export async function downloadProfile( | |
execSync(`adb pull /sdcard/${file} ${dstPath}`); | ||
logger.success(`Successfully pulled the file to ${dstPath}/${file}`); | ||
} | ||
|
||
//Else: transform the profile to Chrome format and pull it to dstPath | ||
else { | ||
const tmpDir = path.join(os.tmpdir(), file); | ||
execSync(`adb pull /sdcard/${file} ${tmpDir}`); | ||
const osTmpDir = os.tmpdir(); | ||
const fileTmpDir = path.join(osTmpDir, file); | ||
execSync(`adb pull /sdcard/${file} ${fileTmpDir}`); | ||
|
||
//If path to source map is not given | ||
if (!sourceMapPath) { | ||
//Get or generate the source map | ||
sourceMapPath = await getSourcemapPath(ctx, generateSourceMap); | ||
//Run without source map | ||
if (sourceMapPath && sourceMapPath.length === 0) { | ||
logger.warn( | ||
'Cannot generate or find bundle and source map, running the transformer without source map', | ||
); | ||
logger.info( | ||
'Instructions on how to get source map:\n Go to directory android/app/build.gradle \n Set bundleInDebug: true', | ||
); | ||
} | ||
} | ||
|
||
//Run transformer tool to convert from Hermes to Chrome format | ||
const events = await transformer(tmpDir, sourceMapPath, 'index.bundle'); | ||
const events = await transformer( | ||
fileTmpDir, | ||
sourceMapPath, | ||
'index.bundle', | ||
); | ||
const transformedFilePath = `${dstPath}/${path.basename( | ||
file, | ||
'.cpuprofile', | ||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing the settings.json changes weren't intentional?