Skip to content

Commit c6264ef

Browse files
committed
chore(*) ad npm run test-build task
Will test the build task: - runs the build task (based on env vars) - if /build folder is under git management, will stash your un-commited modifications (and unstash them at the end) Inspired by topheman/vanilla-es6-jspm : https://github.com/topheman/vanilla-es6-jspm#unit
1 parent 78f57a6 commit c6264ef

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

bin/test-build.sh

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "npm run lint && npm run unit-test",
8+
"test-build": "./bin/test-build.sh",
89
"unit-test": "mocha 'test/unit/**/*.js' --compilers js:babel-core/register --recursive --require ./test/unit/setup.js",
910
"unit-test-watch": "npm run lint-watch & npm run unit-test -- --watch",
1011
"test-e2e": "./node_modules/.bin/protractor protractor.config.js",
@@ -13,6 +14,7 @@
1314
"dev": "echo 'deprecated, use npm run webpack-dev' && npm run webpack-dev",
1415
"dev-mock": "echo 'deprecated, use npm run webpack-mock' && npm run webpack-mock",
1516
"build": "./node_modules/.bin/webpack --progress && ./node_modules/.bin/gulp build",
17+
"clean": "./node_modules/.bin/webpack --clean-only",
1618
"build-prod": "NODE_ENV=production ./node_modules/.bin/webpack --progress -p && NODE_ENV=production ./node_modules/.bin/gulp build",
1719
"build-prod-owner": "API_ROOT_URL='https://topheman-apis-proxy.herokuapp.com/github' npm run build-prod",
1820
"build-prod-all": "npm run build-prod && DEVTOOLS=true NODE_ENV=production npm run build",

webpack.config.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ const webpack = require('webpack');
44
const ExtractTextPlugin = require("extract-text-webpack-plugin");
55
const common = require('./common');
66
const plugins = [];
7+
8+
const root = __dirname;
9+
710
const MODE_DEV_SERVER = process.argv[1].indexOf('webpack-dev-server') > -1 ? true : false;
811
const LAZY_MODE = process.argv.indexOf('--lazy') > -1 ? true : false;
12+
const CLEAN_ONLY = process.argv.indexOf('--clean-only') > -1 ? true : false;// webpack --clean-only (useful to cleanup the build folder)
913

1014
console.log('Launched in ' + (MODE_DEV_SERVER ? 'dev-server' : 'build') + ' mode');
1115

@@ -74,6 +78,7 @@ if(NODE_ENV === 'production' && DEVTOOLS !== true){
7478
}
7579

7680
if(MODE_DEV_SERVER === false){
81+
console.log('root', root);
7782
//write infos about the build (to retrieve the hash) https://webpack.github.io/docs/long-term-caching.html#get-filenames-from-stats
7883
plugins.push(function() {
7984
this.plugin("done", function(stats) {
@@ -105,10 +110,18 @@ else{
105110
//in build mode, cleanup build folder before - since we can build two versions (production & devtools) in a row, skip delete for the devtools
106111
if(MODE_DEV_SERVER === false && DEVTOOLS === false){
107112
console.log('Cleaning ...');
108-
const deleted = require('del').sync(['build/*','build/**/*',"!.git/**/*"]);
113+
const deleted = require('del').sync([
114+
root + '/build/*',
115+
root + '/build/**/*',
116+
root + '/build/!.git/**/*'
117+
]);
109118
deleted.forEach(function(e){
110119
console.log(e);
111120
});
121+
if (CLEAN_ONLY) {
122+
console.log('CLEAN_ONLY mode, exiting clean without going further');
123+
process.exit(0);
124+
}
112125
}
113126
else if(MODE_DEV_SERVER === false && DEVTOOLS === true){
114127
console.log('[INFO] Not cleaning up build/ folder for this pass (not in devtools mode)');

0 commit comments

Comments
 (0)