Skip to content

Commit

Permalink
Optionally capture packager logs during RNTester Xcode build
Browse files Browse the repository at this point in the history
Summary:
Changelog: [Internal]

* Sets a default custom reporter in `scripts/packager.sh` that redirects Metro's logs to a JSON file when the `RCT_PACKAGER_LOG_PATH` environment variable is set. Otherwise we use Metro's `TerminalReporter`, [like the CLI does by default](https://github.com/react-native-community/cli/blob/0993f627869f4de0e5a8a9a950d38a9f15e18fb2/packages/cli-plugin-metro/src/commands/start/runServer.ts#L124).
* Modifies `scripts/launchPackager.command` to write logs to `$RCT_PACKAGER_LOGS_DIR` (and in particular use the JSON reporter) if this environment var is set.
* Uses [`open -n`](https://ss64.com/osx/open.html#:~:text=Open%20a%20new%20instance%20of%20the%20application) to execute `launchPackager.command` from Xcode, so that environment variables are reliably passed to the script even if a Terminal process is already running.

Reviewed By: yungsters

Differential Revision: D32295290

fbshipit-source-id: 4d783e1f94d122d3a35c104b2b02dbfaf8e1a1a3
  • Loading branch information
motiz88 authored and facebook-github-bot committed Nov 11, 2021
1 parent 4ff16c6 commit 6df04ae
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "set -x\n\nexport RCT_METRO_PORT=\"${RCT_METRO_PORT:=8081}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/../../scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\n if nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\n if ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\n echo \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\n exit 2\n fi\n else\n open \"$SRCROOT/../../scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\n fi\nfi\n";
shellScript = "set -x\n\nexport RCT_METRO_PORT=\"${RCT_METRO_PORT:=8081}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/../../scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\n if nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\n if ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\n echo \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\n exit 2\n fi\n else\n open -n \"$SRCROOT/../../scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\n fi\nfi\n";
showEnvVarsInLog = 0;
};
68CD48B71D2BCB2C007E06A9 /* Build JS Bundle */ = {
Expand Down
14 changes: 11 additions & 3 deletions scripts/launchPackager.command
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ clear

THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)

# shellcheck source=/dev/null
. "$THIS_DIR/packager.sh"

if [ -n "${RCT_PACKAGER_LOGS_DIR}" ] ; then
echo "Writing logs to $RCT_PACKAGER_LOGS_DIR"
# shellcheck source=/dev/null
RCT_PACKAGER_LOG_PATH="$RCT_PACKAGER_LOGS_DIR/metro.log" \
. "$THIS_DIR/packager.sh" \
> "$RCT_PACKAGER_LOGS_DIR/packager.stdout.log" \
2> "$RCT_PACKAGER_LOGS_DIR/packager.stderr.log"
else
# shellcheck source=/dev/null
. "$THIS_DIR/packager.sh"
fi
if [[ -z "$CI" ]]; then
echo "Process terminated. Press <enter> to close the window"
read -r
Expand Down
27 changes: 27 additions & 0 deletions scripts/packager-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

'use strict';

const logPath = process.env.RCT_PACKAGER_LOG_PATH;
if (logPath != null && logPath !== '') {
const JsonReporter = require('metro/src/lib/JsonReporter');
const fs = require('fs');
const path = require('path');
module.exports = class extends JsonReporter {
constructor() {
fs.mkdirSync(path.dirname(logPath), {
recursive: true,
});
super(fs.createWriteStream(logPath));
}
};
} else {
module.exports = require('metro/src/lib/TerminalReporter');
}
2 changes: 1 addition & 1 deletion scripts/packager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ then
fi
# Start packager from PROJECT_ROOT
cd "$PROJECT_ROOT" || exit
"$NODE_BINARY" "$REACT_NATIVE_ROOT/cli.js" start "$@"
"$NODE_BINARY" "$REACT_NATIVE_ROOT/cli.js" start --custom-log-reporter-path "$THIS_DIR/packager-reporter.js" "$@"

0 comments on commit 6df04ae

Please sign in to comment.