clean_pack.sh 2.28 KB
Newer Older
1
2
3
4
5
6
7
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

Dan Abramov's avatar
Dan Abramov committed
8
9
10
# This script cleans up the code from blocks only used during local development.
# We call this as part of the `release.sh` script.
# On success, the only output to stdout is the package name.
11

Dan Abramov's avatar
Dan Abramov committed
12
# Start in /tasks even if run from root directory
13
14
cd "$(dirname "$0")"

Dan Abramov's avatar
Dan Abramov committed
15
16
# Print error messages to stderr.
# The calling script may then handle them.
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function handle_error {
  echo "$(basename $0): \033[31mERROR!\033[m An error was encountered executing \033[36mline $1\033[m." 1>&2;
  cleanup
  echo 'Exiting with error.' 1>&2;
  exit 1
}

function handle_exit {
  cleanup
  echo 'Exiting without error.' 1>&2;
  exit
}

function cleanup {
  cd $initial_path
Dan Abramov's avatar
Dan Abramov committed
32
33
  # Uncomment when snapshot testing is enabled by default:
  # rm ../template/src/__snapshots__/App.test.js.snap
34
35
36
37
38
39
40
41
42
43
  rm -rf ../$clean_path
}

# Exit the script with a helpful error message when any error is encountered
trap 'set +x; handle_error $LINENO $BASH_COMMAND' ERR

# Cleanup before exit on any termination signal
trap 'set +x; handle_exit' SIGQUIT SIGTERM SIGINT SIGKILL SIGHUP

# Go to root
Dan Abramov's avatar
Dan Abramov committed
44
initial_path=$PWD
45
cd ..
Dan Abramov's avatar
Dan Abramov committed
46
47
48

# Create a temporary clean folder that contains production-only code.
# Do not overwrite any files in the current folder.
Dan Abramov's avatar
Dan Abramov committed
49
clean_path=`mktemp -d 2>/dev/null || mktemp -d -t 'clean_path'`
50

Dan Abramov's avatar
Dan Abramov committed
51
52
53
# Copy some of the project files to the temporary folder.
# Exclude folders that definitely won’t be part of the package from processing.
# We will strip the dev-only code there, and then copy files back.
54
rsync -av --exclude='.git' --exclude=$clean_path\
Dan Abramov's avatar
Dan Abramov committed
55
56
  --exclude='node_modules' --exclude='build'\
  './' $clean_path  >/dev/null
57

Dan Abramov's avatar
Dan Abramov committed
58
# Now remove all the code relevant to development of Create React App.
59
60
61
62
63
64
65
66
67
68
cd $clean_path
files="$(find -L . -name "*.js" -type f)"
for file in $files; do
  sed -i.bak '/\/\/ @remove-on-publish-begin/,/\/\/ @remove-on-publish-end/d' $file
  rm $file.bak
done

# Pack!
packname=`npm pack`

Dan Abramov's avatar
Dan Abramov committed
69
# Now we can copy the package back.
70
71
72
cd ..
cp -f $clean_path/$packname ./
cleanup
Dan Abramov's avatar
Dan Abramov committed
73
74

# Output the package name so `release.sh` can pick it up.
75
echo $packname