Skip to content
Open
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ with a commit message of `<branch-name> npm audit fix`. This will fix any proble

# Example with Husky

This will run auditing as a pre-push hook using [husky](https://www.npmjs.com/package/husky):
This will run auditing as a pre-commit hook using [husky](https://www.npmjs.com/package/husky):

```json
{
Expand All @@ -19,7 +19,7 @@ This will run auditing as a pre-push hook using [husky](https://www.npmjs.com/pa
"main": "index.js",
"husky": {
"hooks": {
"pre-push": "auditmated"
"pre-commit": "auditmated"
}
},
"devDependencies": {
Expand All @@ -29,15 +29,15 @@ This will run auditing as a pre-push hook using [husky](https://www.npmjs.com/pa
}
```

If you are using Husky v0, define as a `prepush` script.
If you are using Husky v0.14, define as a `precommit` script.

```json
{
"name": "audit-test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"prepush": "auditmated"
"precommit": "auditmated"
},
"devDependencies": {
"auditmated": "0.1.0",
Expand Down
23 changes: 12 additions & 11 deletions bin/audit.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#!/bin/bash
BRANCH=`git rev-parse --abbrev-ref HEAD`
#!/bin/env bash

BRANCH="$(git rev-parse --abbrev-ref HEAD)"
MESSAGE="$BRANCH npm audit fix"

if [[ $BRANCH = 'master' ]] || [[ $BRANCH = 'develop' ]] ; then
echo 'skipping audit on '$BRANCH' branch'
if [[ $BRANCH = 'master' ]] || [[ $BRANCH = 'develop' ]]; then
echo 'skipping audit on '"$BRANCH"' branch'
exit 0
fi

npm audit fix
git add package.json package-lock.json
git commit --no-verify -m "$MESSAGE"

# if audit fix didn't change anything the commit will exit with non-0 exit code
# catch that error code and exit successfully
if [[ $? -ne 0 ]] ; then
if [[ ! "$(npm audit fix)" ]]; then
# if audit fix didn't change anything the commit will exit with non-0 exit code
# catch that error code and exit successfully
echo 'audit: minor and patch version of deps have no known security issues'
exit 0
fi

git add package.json package-lock.json &&
git commit --no-verify -m "$MESSAGE"