Last active
May 10, 2018 01:28
-
-
Save ORESoftware/0fa7e3d6b75a65b17b6b126a7bec3397 to your computer and use it in GitHub Desktop.
r2g calls r2g_internal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r2g_internal(){ | |
local my_args=( "$@" ); | |
local r2g_keep_temp=$(r2g_match_arg "--keep" "${my_args[@]}"); | |
local r2g_multi_temp=$(r2g_match_arg "--multi" "${my_args[@]}"); | |
local r2g_multi=""; | |
if [[ "$r2g_multi_temp" || "$r2g_keep_temp" ]]; then | |
r2g_multi="yes" | |
fi | |
local exit_code=""; | |
local gmx_gray='\033[1;30m' | |
local gmx_magenta='\033[1;35m' | |
local gmx_cyan='\033[1;36m' | |
local gmx_orange='\033[1;33m' | |
local gmx_yellow='\033[1;33m' | |
local gmx_green='\033[1;32m' | |
local gmx_no_color='\033[0m' | |
if [[ -z "$(which prepend-with)" ]]; then | |
npm install -g prepend; | |
fi | |
rm -rf "$HOME/.r2g/logs"; | |
mkdir -p "$HOME/.r2g/logs" | |
mkdir -p "$HOME/.r2g/temp/project" | |
if [[ -z "$r2g_multi" ]]; then | |
rm -rf "$HOME/.r2g/temp/project"; | |
else | |
echo "We are keeping the previously installed modules because '--keep' / '--multi' was used."; | |
fi | |
local my_cwd="$PWD"; | |
if [[ ! -f package.json ]]; then | |
echo "Could not find a package.json file in your current working directory."; | |
my_cwd="$(node "$HOME/.r2g/node/find-root.js")" | |
if [[ -z "$my_cwd" ]]; then | |
echo -e "${gmx_magenta}You are not within an NPM project.${gmx_no_color}"; | |
return 1; | |
fi | |
fi | |
cd "$my_cwd"; | |
local result="$(npm pack)" | |
if [[ -z "$result" ]]; then | |
echo -e "${gmx_magenta}NPM pack command did not appear to yield a .tgz file.${gmx_no_color}"; | |
return 1; | |
fi | |
local tgz_path="$my_cwd/$result"; | |
local dest="$HOME/.r2g/temp/project" | |
echo "r2g will install this package: '$tgz_path'" | |
echo "to this project: '$dest'..." | |
mkdir -p "$dest" | |
local copy_test="$(node "$HOME/.r2g/node/axxel.js" package.json 'r2g.copy-tests')" | |
if [[ -z "$copy_test" ]]; then | |
echo -e "${gmx_yellow}No NPM script at 'r2g.copy-tests' in your package.json file.${gmx_no_color}"; | |
fi | |
local run_test="$(node "$HOME/.r2g/node/axxel.js" package.json 'r2g.run-tests')"; | |
if [[ -z "$run_test" ]]; then | |
echo -e "${gmx_yellow}No NPM script at 'r2g.run-tests' in your package.json file.${gmx_no_color}"; | |
fi | |
( | |
set -e; | |
cd "$dest"; | |
( npm init --yes ) &> /dev/null || { echo "warning: package.json file already existed in \$HOME/.r2g/temp/project"; } | |
cat "$HOME/.r2g/node/smoke-tester.js" > smoke-tester.js; | |
echo "now running: 'npm install "${tgz_path}"'..."; | |
npm install "$tgz_path" # --silent >> "$HOME/.r2g/logs/r2g.log" 2>&1; | |
) | |
exit_code="$?" | |
if [[ "$exit_code" != "0" ]]; then | |
echo "warning: npm install command failed, to see log, run: r2g_view_log"; | |
return 1; | |
fi | |
( | |
set -eo pipefail | |
if [[ ! -z "$copy_test" ]]; then | |
echo "Copying r2g smoke test fixtures to '\$HOME/.r2g/temp/project'..."; | |
echo "$copy_test" | bash | |
fi | |
) | |
exit_code="$?" | |
if [[ "$exit_code" != "0" ]]; then | |
echo "warning: your copy command failed, to see log, run: r2g_view_log"; | |
return 1; | |
fi | |
( | |
# run the tests | |
cd "$dest"; | |
set -eo pipefail | |
if [[ ! -z "$run_test" ]]; then | |
echo "$run_test" | bash | |
else | |
node smoke-tester.js | |
fi | |
local exit_code="$?" | |
if [[ "$exit_code" == "0" ]]; then | |
echo -e "${gmx_green}r2g tests passed.${gmx_no_color}" | |
return 0; | |
fi | |
echo -e "${gmx_magenta}===============================${gmx_no_color}" | |
echo -e "${gmx_magenta} => Your r2g test(s) have failed.${gmx_no_color}" | |
echo -e "${gmx_magenta}===============================${gmx_no_color}" | |
return 1; | |
) | |
exit_code="$?" | |
if [[ "$exit_code" != "0" ]]; then | |
echo "warning: your test command failed, to see log, run: r2g_view_log"; | |
return 1; | |
fi | |
} | |
r2g(){ | |
echo "executable is: $0" | |
( | |
set -e; | |
r2g_internal "$@" \ | |
2> >( while read line; do echo "r2g error: $line"; done ) \ | |
1> >( while read line; do echo "r2g: $line"; done ) | |
) | |
exit_code="$?" | |
if [[ "$exit_code" != "0" ]]; then | |
echo "something experienced an error, to see log, run: r2g_view_log"; | |
return 1; | |
fi | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment