Skip to content

Commit

Permalink
Add yarn custom path and cache layer
Browse files Browse the repository at this point in the history
  • Loading branch information
fagiani committed Aug 8, 2021
1 parent d5d73aa commit b302e60
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# main

## 0.1.2 (2021-07-08)
### Added
- Add cache layer for yarn build
- Allow run build from a specific path

## 0.1.1 (2021-03-28)
### Added
- Add node requirement to build plan ([#7](https://github.com/heroku/nodejs-yarn-buildpack/pull/7))
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ cd .. # change from nodejs-npm-buildpack directory
git clone [email protected]:heroku/nodejs-engine-buildpack.git
```

### Define a custom path for yarn (optional)

You can optionally create a `yarn.lock` file in the root directory with a line like this:

```
# yarn_path:public/my-custom-path
```

Make sure you place the actual `package.json` and `yarn.lock` in that path

### Build the image

#### with buildpacks
Expand Down
6 changes: 5 additions & 1 deletion bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ platform_dir=$2

# shellcheck source=/dev/null
source "$bp_dir/lib/build.sh"
# shellcheck source=/dev/null
source "$bp_dir/lib/utils/yarn.sh"
rm -rf "$build_dir/node_modules"

build_dir=$(extract_path_from_yarn_lock "$build_dir")

export_env "$platform_dir/env" "" ""
run_prebuild "$build_dir"
install_or_reuse_node_modules "$build_dir" "$layers_dir/node_modules"
run_build "$build_dir"
run_build "$build_dir" "$layers_dir"
write_launch_toml "$build_dir/package.json" "$layers_dir/launch.toml"
33 changes: 31 additions & 2 deletions lib/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ run_prebuild() {
heroku_prebuild_script=$(json_get_key "$build_dir/package.json" ".scripts[\"heroku-prebuild\"]")

if [[ $heroku_prebuild_script ]] ; then
cd $build_dir
yarn heroku-prebuild
cd -
fi
}

install_modules() {
local build_dir=$1

cd $build_dir
if detect_yarn_lock "$build_dir" ; then
echo "---> Installing node modules from ./yarn.lock"
echo "---> Installing node modules from $build_dir/yarn.lock"
yarn install
else
echo "---> Installing node modules"
yarn install --no-lockfile
fi
cd -
}

install_or_reuse_node_modules() {
Expand Down Expand Up @@ -69,17 +73,43 @@ install_or_reuse_node_modules() {

run_build() {
local build_dir=$1
local layer_dir=$2
local build_cache
local build_script
local heroku_postbuild_script

build_cache=$(json_get_key "$build_dir/package.json" ".directories[\"heroku-buildcache\"]")
if [[ $build_cache ]] ; then
layer_dir="$layer_dir/$build_cache"
cat <<TOML > "$layer_dir.toml"
cache = true
build = false
launch = false
TOML
mkdir -p "${layer_dir}"
cp -r "$layer_dir" "$build_dir/$build_cache"
echo "$build_dir/$build_cache"
ls -lsa $build_dir/$build_cache
fi

build_script=$(json_get_key "$build_dir/package.json" ".scripts.build")
heroku_postbuild_script=$(json_get_key "$build_dir/package.json" ".scripts[\"heroku-postbuild\"]")

cd $build_dir
if [[ $heroku_postbuild_script ]] ; then
yarn heroku-postbuild
elif [[ $build_script ]] ; then
yarn build
fi
cd -

echo "$build_dir/$build_cache"
ls -lsa $build_dir/$build_cache
ls -lsa $build_dir/resources/assets/build

if [[ $build_cache && -d "$build_dir/$build_cache" && -n "$(ls -A "$build_dir/$build_cache")" ]] ; then
cp -r "$build_dir/$build_cache/." "$layer_dir"
fi
}

write_launch_toml() {
Expand All @@ -93,5 +123,4 @@ type = "web"
command = "yarn start"
TOML
fi

}
11 changes: 11 additions & 0 deletions lib/utils/yarn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

extract_path_from_yarn_lock() {
local build_dir=$1

if grep -q -e "^# :yarn_path:" $build_dir/yarn.lock; then
echo "$1/$(cat $build_dir/yarn.lock | grep -s -e "^# :yarn_path:" | sed 's/^# :yarn_path:\(.*\)\s*$/\1/g')"
else
echo "$1"
fi
}
5 changes: 5 additions & 0 deletions package.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[buildpack]
uri = "."

[[dependencies]]
uri = "docker://public.ecr.aws/heroku-buildpacks/heroku-nodejs-engine-buildpack@sha256:4fa85f23b1df1138039ec6f5667da48da2af7858767b8840a754c54b39b42ca1"

0 comments on commit b302e60

Please sign in to comment.