|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) 2015-present, Facebook, Inc. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. An additional grant |
| 7 | +# of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + |
| 9 | +# ****************************************************************************** |
| 10 | +# This creates an app with the global CLI and `react-scripts` from the source. |
| 11 | +# It is useful for testing the end-to-end flow locally. |
| 12 | +# ****************************************************************************** |
| 13 | + |
| 14 | +# Start in tasks/ even if run from root directory |
| 15 | +cd "$(dirname "$0")" |
| 16 | + |
| 17 | +function cleanup { |
| 18 | + echo 'Cleaning up.' |
| 19 | + cd $root_path |
| 20 | + # Uncomment when snapshot testing is enabled by default: |
| 21 | + # rm ./template/src/__snapshots__/App.test.js.snap |
| 22 | + rm -rf $clean_path |
| 23 | +} |
| 24 | + |
| 25 | +# Error messages are redirected to stderr |
| 26 | +function handle_error { |
| 27 | + echo "$(basename $0): ERROR! An error was encountered executing line $1." 1>&2; |
| 28 | + cleanup |
| 29 | + echo 'Exiting with error.' 1>&2; |
| 30 | + exit 1 |
| 31 | +} |
| 32 | + |
| 33 | +function handle_exit { |
| 34 | + cleanup |
| 35 | + echo 'Exiting without error.' 1>&2; |
| 36 | + exit |
| 37 | +} |
| 38 | + |
| 39 | +# Exit the script with a helpful error message when any error is encountered |
| 40 | +trap 'set +x; handle_error $LINENO $BASH_COMMAND' ERR |
| 41 | + |
| 42 | +# Cleanup before exit on any termination signal |
| 43 | +trap 'set +x; handle_exit' SIGQUIT SIGTERM SIGINT SIGKILL SIGHUP |
| 44 | + |
| 45 | +# Echo every command being executed |
| 46 | +set -x |
| 47 | + |
| 48 | +# Go to root |
| 49 | +cd .. |
| 50 | +root_path=$PWD |
| 51 | + |
| 52 | +# ****************************************************************************** |
| 53 | +# Pack react-scripts so we can verify they work. |
| 54 | +# ****************************************************************************** |
| 55 | + |
| 56 | +# Packing react-scripts takes some work because we want to clean it up first. |
| 57 | +# Create a temporary clean folder that contains production only code. |
| 58 | +# Do not overwrite any files in the current folder. |
| 59 | +clean_path=`mktemp -d 2>/dev/null || mktemp -d -t 'clean_path'` |
| 60 | + |
| 61 | +# Copy some of the project files to the temporary folder. |
| 62 | +# Exclude folders that definitely won’t be part of the package from processing. |
| 63 | +# We will strip the dev-only code there, `npm pack`, and copy the package back. |
| 64 | +cd $root_path |
| 65 | +rsync -av --exclude='.git' --exclude=$clean_path\ |
| 66 | + --exclude='node_modules' --exclude='build'\ |
| 67 | + './' $clean_path >/dev/null |
| 68 | + |
| 69 | +# Open the clean folder |
| 70 | +cd $clean_path |
| 71 | +# Now remove all the code relevant to development of Create React App. |
| 72 | +files="$(find -L . -name "*.js" -type f)" |
| 73 | +for file in $files; do |
| 74 | + sed -i.bak '/\/\/ @remove-on-publish-begin/,/\/\/ @remove-on-publish-end/d' $file |
| 75 | + rm $file.bak |
| 76 | +done |
| 77 | + |
| 78 | +# Finally, pack react-scripts |
| 79 | +cp -rf $root_path/node_modules $clean_path |
| 80 | +scripts_path=$clean_path/`npm pack` |
| 81 | + |
| 82 | +# ****************************************************************************** |
| 83 | +# Now that we have packed them, call the global CLI. |
| 84 | +# ****************************************************************************** |
| 85 | + |
| 86 | +# Go back to the root directory and run the command from here |
| 87 | +cd $root_path |
| 88 | +node global-cli/index.js --scripts-version=$scripts_path "$@" |
| 89 | + |
| 90 | +# Cleanup |
| 91 | +cleanup |
0 commit comments