|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script is inspired from my previous project topheman/vanilla-es6-jspm |
| 4 | +# |
| 5 | +# https://github.com/topheman/vanilla-es6-jspm/blob/master/bin/test-build.sh |
| 6 | +# |
| 7 | +# This script will launch the `npm run build` task (based on the env vars) |
| 8 | +# |
| 9 | +# If your build/ folder is under git management, |
| 10 | +# it will git stash your modifications before doing anything and restore them |
| 11 | +# at the end of the test (wether it passed or not) |
| 12 | + |
| 13 | +# don't put this flag, we need to go through |
| 14 | +# always stop on errors |
| 15 | +# set -e |
| 16 | + |
| 17 | +WEBPACK_PATH="$(npm bin)/webpack" |
| 18 | + |
| 19 | +BUILD_IS_GIT=0 |
| 20 | +BUILD_IS_GIT_DIRTY=0 |
| 21 | + |
| 22 | +# vars retrieving the exit codes of the commands run |
| 23 | +NPM_RUN_BUILD_EXIT_CODE=0 |
| 24 | +WEBPACK_CLEAN_EXIT_CODE=0 |
| 25 | + |
| 26 | +echo "###### TEST npm run build" |
| 27 | + |
| 28 | +# If build folder is under git, stash modification - fail if can't stash |
| 29 | +if [ -d $(dirname $0)/../build/.git ] |
| 30 | +then |
| 31 | + BUILD_IS_GIT=1 |
| 32 | + echo "[INFO] build folder is under git management" |
| 33 | + cd $(dirname $0)/../build |
| 34 | + echo "[INFO] $(pwd)" |
| 35 | + |
| 36 | + if [[ -n $(git status --porcelain) ]] |
| 37 | + then |
| 38 | + BUILD_IS_GIT_DIRTY=1 |
| 39 | + echo "[INFO] build folder has un-committed changes, stashing them" |
| 40 | + |
| 41 | + cmd="git stash save -u" |
| 42 | + echo "[RUN] $cmd" |
| 43 | + eval $cmd |
| 44 | + if [ $? -gt 0 ] |
| 45 | + then |
| 46 | + echo "[WARN] Couldn't stash modifications please commit your files in build folder before proceeding" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + else |
| 50 | + echo "[INFO] build folder repo is clean, nothing to stash" |
| 51 | + fi |
| 52 | +fi |
| 53 | + |
| 54 | +cmd="npm run build" |
| 55 | +echo "[RUN] $cmd" |
| 56 | +eval $cmd |
| 57 | +NPM_RUN_BUILD_EXIT_CODE=$? |
| 58 | +echo "[DEBUG] npm run build exit code : $NPM_RUN_BUILD_EXIT_CODE"; |
| 59 | + |
| 60 | +cmd="npm run clean" |
| 61 | +echo "[RUN] $cmd" |
| 62 | +eval $cmd |
| 63 | +WEBPACK_CLEAN_EXIT_CODE=$? |
| 64 | +echo "[DEBUG] npm run clean exit code : $WEBPACK_CLEAN_EXIT_CODE"; |
| 65 | + |
| 66 | +if [ $WEBPACK_CLEAN_EXIT_CODE -gt 0 ] && [ $BUILD_IS_GIT_DIRTY -gt 0 ] |
| 67 | +then |
| 68 | + echo "[WARN] Couldn't clean the build folder repo before git unstash" |
| 69 | + echo "[WARN] Run the following commands manually to get back your repo in build folder" |
| 70 | + echo "[INFO] ./node_modules/.bin/webpack --clean-only" |
| 71 | + echo "[INFO] git reset --hard HEAD" |
| 72 | + echo "[INFO] git stash pop --index" |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +# After cleaning build folder, if it is a git repo, point it back to the HEAD |
| 77 | +if [ $BUILD_IS_GIT -gt 0 ] |
| 78 | +then |
| 79 | + echo "[INFO] build folder is under git management, pointing back to HEAD" |
| 80 | + |
| 81 | + cmd="git reset --hard HEAD" |
| 82 | + echo "[RUN] $cmd" |
| 83 | + eval $cmd |
| 84 | + if [ $? -gt 0 ] |
| 85 | + then |
| 86 | + echo "[WARN] Couldn't reset --hard HEAD build folder repo" |
| 87 | + echo "[WARN] Run the following command manually to get back your repo in build folder" |
| 88 | + echo "[INFO] git reset --hard HEAD" |
| 89 | + echo "[INFO] git stash pop --index" |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +fi |
| 93 | + |
| 94 | +# If build folder is a git repo and was dirty, retrieve the stash |
| 95 | +if [ $BUILD_IS_GIT_DIRTY -gt 0 ] |
| 96 | +then |
| 97 | + echo "[INFO] build folder is under git management & has stashed files, retrieving stash" |
| 98 | + |
| 99 | + cmd="git stash pop --index" |
| 100 | + echo "[RUN] $cmd" |
| 101 | + eval $cmd |
| 102 | + if [ $? -gt 0 ] |
| 103 | + then |
| 104 | + echo "[WARN] Couldn't unstash build folder repo" |
| 105 | + echo "[WARN] Run the following command manually to get back your repo in build folder" |
| 106 | + echo "[INFO] git stash pop --index" |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | +else |
| 110 | + if [ $BUILD_IS_GIT -gt 0 ] |
| 111 | + then |
| 112 | + echo "[INFO] build folder is under git management but directory was clean at start, nothing to unstash" |
| 113 | + fi |
| 114 | +fi |
| 115 | + |
| 116 | +#finally return an exit code according to the npm run build task |
| 117 | +if [ $NPM_RUN_BUILD_EXIT_CODE -gt 0 ] |
| 118 | +then |
| 119 | + echo "[FAILED] npm run build failed. Exiting with code $NPM_RUN_BUILD_EXIT_CODE" |
| 120 | + echo "###### END TEST npm run build" |
| 121 | + exit $NPM_RUN_BUILD_EXIT_CODE |
| 122 | +else |
| 123 | + echo "[PASSED] npm run build passed" |
| 124 | + echo "###### END TEST npm run build" |
| 125 | + exit 0 |
| 126 | +fi |
0 commit comments