Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add build flag option #859

Merged
merged 8 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Use jq from the stack image ([#854](https://github.com/heroku/heroku-buildpack-nodejs/pull/854))
- Start testing new `resolve` binaries with inventory lists for Node and Yarn ([#855](https://github.com/heroku/heroku-buildpack-nodejs/pull/855))
- Follow up to [#855](https://github.com/heroku/heroku-buildpack-nodejs/pull/855) to send captured data with bin/report ([#858](https://github.com/heroku/heroku-buildpack-nodejs/pull/858))
- Add NODE_BUILD_FLAG env var ([#859](https://github.com/heroku/heroku-buildpack-nodejs/pull/859))

## v176 (2020-09-10)
- Only run immutable cache in yarn 2 if caching enabled ([#832](https://github.com/heroku/heroku-buildpack-nodejs/pull/832))
Expand Down
35 changes: 33 additions & 2 deletions lib/dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ run_if_present() {
fi
}

run_build_if_present() {
local build_dir=${1:-}
local script_name=${2:-}
local has_script_name
local script

has_script_name=$(has_script "$build_dir/package.json" "$script_name")
script=$(read_json "$build_dir/package.json" ".scripts[\"$script_name\"]")

if [[ "$has_script_name" == "true" ]]; then
if $YARN || $YARN_2; then
echo "Running $script_name (yarn)"
# yarn will throw an error if the script is an empty string, so check for this case
if [[ -n "$script" ]]; then
if [[ -n $NODE_BUILD_FLAG ]]; then
echo "Running with $NODE_BUILD_FLAG flags"
monitor "${script_name}-script" yarn run "$script_name" "$NODE_BUILD_FLAG"
fi
monitor "${script_name}-script" yarn run "$script_name"
fi
else
echo "Running $script_name"
if [[ -n $NODE_BUILD_FLAG ]]; then
echo "Running with $NODE_BUILD_FLAG flags"
monitor "${script_name}-script" npm run "$script_name" "$NODE_BUILD_FLAG" --if-present
else
monitor "${script_name}-script" npm run "$script_name" --if-present
fi
fi
fi
}

run_prebuild_script() {
local build_dir=${1:-}
local has_heroku_prebuild_script
Expand All @@ -59,7 +91,6 @@ run_build_script() {

has_build_script=$(has_script "$build_dir/package.json" "build")
has_heroku_build_script=$(has_script "$build_dir/package.json" "heroku-postbuild")

if [[ "$has_heroku_build_script" == "true" ]] && [[ "$has_build_script" == "true" ]]; then
echo "Detected both \"build\" and \"heroku-postbuild\" scripts"
mcount "scripts.heroku-postbuild-and-build"
Expand All @@ -69,7 +100,7 @@ run_build_script() {
run_if_present "$build_dir" 'heroku-postbuild'
elif [[ "$has_build_script" == "true" ]]; then
mcount "scripts.build"
run_if_present "$build_dir" 'build'
run_build_if_present "$build_dir" 'build'
fi
}

Expand Down
11 changes: 11 additions & 0 deletions test/run
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ testBuildScriptYarn() {
assertCapturedSuccess
}

testBuildFlags() {
cache=$(mktmpdir)
env_dir=$(mktmpdir)

echo "--prod --optimize" > $env_dir/NODE_BUILD_FLAG
compile "build-script" $cache $env_dir
assertCaptured "Running build"
assertCaptured "Running with --prod --optimize flags"
assertCapturedSuccess
}

testPreferEmptyHerokuPostbuildOverBuild() {
compile "empty-heroku-postbuild"
assertCaptured "Detected both \"build\" and \"heroku-postbuild\" scripts"
Expand Down