Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
66 changes: 65 additions & 1 deletion bin/detect
Original file line number Diff line number Diff line change
@@ -1,10 +1,74 @@
#!/usr/bin/env bash
# bin/detect <build-dir>

error() {
local c="2,999 s/^/ ! /"
# send all of our output to stderr
exec 1>&2

echo -e "\033[1;31m" # bold; red
echo -n " ! ERROR: "
# this will be fed from stdin
case $(uname) in
Darwin) sed -l "$c";; # mac/bsd sed: -l buffers on line boundaries
*) sed -u "$c";; # unix/gnu sed: -u unbuffered (arbitrary) chunks of data
esac
echo -e "\033[0m" # reset style
exit 1
}

if [ -f $1/package.json ]; then
echo 'Node.js'
exit 0
fi

>&2 echo 'Node.js: package.json not found in application root'
if [[ -f "$1/.slugignore" ]] && grep -Fxq "package.json" "$1/.slugignore"; then
error << EOF
'package.json' listed in '.slugignore' file

The 'heroku/nodejs' buildpack is set on this application, but was
unable to detect a 'package.json' file. This is likely because
the '.slugignore' file is removing it before the build begins.

For more information, refer to the following documentation:
https://devcenter.heroku.com/articles/slug-compiler#ignoring-files-with-slugignore
EOF
elif [[ -f "$1/.gitignore" ]] && grep -Fxq "package.json" "$1/.gitignore"; then
error << EOF
'package.json' listed in '.gitignore' file

The 'heroku/nodejs' buildpack is set on this application, but was
unable to detect a 'package.json' file. This is likely because
the '.gitignore' file is preventing it from being checked in to
the git repo.

For more information, refer to the following documentation:
https://devcenter.heroku.com/articles/gitignore
EOF
else
error <<- EOF
Application not supported by 'heroku/nodejs' buildpack

The 'heroku/nodejs' buildpack is set on this application, but was
unable to detect a Node.js codebase.

A Node.js app on Heroku requires a 'package.json' at the root of
the directory structure.

If you are trying to deploy a Node.js application, ensure that this
file is present at the top level directory. This directory has the
following files:

$(ls -1p $1)

If you are trying to deploy an application written in another
language, you need to change the list of buildpacks set on your
Heroku app using the 'heroku buildpacks' command.

For more information, refer to the following documentation:
https://devcenter.heroku.com/articles/buildpacks
https://devcenter.heroku.com/articles/nodejs-support#activation
EOF
fi

exit 1
1 change: 1 addition & 0 deletions test/fixtures/gitignore-package-json/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package.json
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/slugignore-package-json/.slugignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package.json
25 changes: 25 additions & 0 deletions test/run
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,22 @@ testMemoryMetrics() {
assertFileNotContains "measure#buildpack.nodejs.exec.heroku-postbuild.memory=" $metrics_log
}

testBinDetectWarnings() {
detect "slugignore-package-json"
assertCapturedError "'package.json' listed in '.slugignore' file"
assertCapturedError "https://devcenter.heroku.com/articles/slug-compiler#ignoring-files-with-slugignore"

detect "gitignore-package-json"
assertCapturedError "'package.json' listed in '.gitignore' file"
assertCapturedError "https://devcenter.heroku.com/articles/gitignore"

detect "node-project-missing-package-json"
assertCapturedError "Application not supported by 'heroku/nodejs' buildpack"
assertCapturedError "https://devcenter.heroku.com/articles/nodejs-support#activation"
assertCapturedError "index.js"
assertCapturedError "src/"
}

# Utils

pushd "$(dirname 0)" >/dev/null
Expand Down Expand Up @@ -1018,6 +1034,15 @@ default_process_types_cleanup() {
fi
}

detect() {
default_process_types_cleanup
bp_dir=$(mktmpdir)
compile_dir=$(mktmpdir)
cp -a "$(pwd)"/* ${bp_dir}
cp -a ${bp_dir}/test/fixtures/$1/. ${compile_dir}
capture ${bp_dir}/bin/detect ${compile_dir}
}

compile() {
default_process_types_cleanup
bp_dir=$(mktmpdir)
Expand Down