Skip to content

Commit

Permalink
feat(config-file): Add support for reading a config file (#43)
Browse files Browse the repository at this point in the history
* feat(config-file): Add support for reading a config file

The config structure follows the options using JSON file format. Ex:
{
  "low": true,
  "advisories": [100, 101],
  "package-manager": "auto",
  "whitelist": ["example-package"]
}

Usage: audit-ci --settings <pathToFile.json> [extra args]

Also added config file tests for yarn and npm.

* feat(config-file): Change `--settings` to `--config`

Discussion: #43 (comment)

* feat(config-file): Add spec for config file. Fix config example.

The config example fails if copy-pasted due to JSON comments.
Instead, specify the implementation earlier and reference the example.

* feat(config-file): Fix config file name from settings to config
  • Loading branch information
quinnturner authored Jan 15, 2019
1 parent 654f51b commit 39c3c87
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ before_install:
| -r | --report | Shows the `npm audit --json` report (default `true`) |
| -a | --advisories | Vulnerable advisory ids to whitelist from preventing integration (default `none`) |
| -w | --whitelist | Vulnerable modules to whitelist from preventing integration (default `none`) |
| | --config | Path to JSON config file |

### (_Optional_) Config file specification

A config file can manage auditing preferences `audit-ci`. The config file's keys match the CLI arguments.

```
{
// Only use one of ["low": true, "moderate": true, "high": true, "critical": true]
"low": <boolean>, // [Optional] defaults `false`
"moderate": <boolean>, // [Optional] defaults `false`
"high": <boolean>, // [Optional] defaults `false`
"critical": <boolean>, // [Optional] defaults `false`
"report": <boolean>, // [Optional] defaults `true`
"package-manager": <string>, // [Optional] defaults `"auto"`
"advisories": <number[]>, // [Optional] defaults `[]`
"whitelist": <string[]> // [Optional] defaults `[]`
}
```
Review the examples section for an [example of config file usage](#example-config-file-and-usage).
## Examples
Expand Down Expand Up @@ -104,6 +125,23 @@ audit-ci --critical --report false
audit-ci
```

### Example config file and usage

**audit-ci.json**

```json
{
"low": true,
"package-manager": "auto",
"advisories": [100, 101],
"whitelist": ["example1", "example2"]
}
```

```sh
audit-ci --config audit-ci.json
```

## Q&A

#### Why run `audit-ci` on PR builds for `Travis-CI` and not the push builds?
Expand Down
1 change: 1 addition & 0 deletions lib/audit-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const yargs = require('yargs');
const fs = require('fs');

const { argv } = yargs
.config('config')
.options({
l: {
alias: 'low',
Expand Down
4 changes: 4 additions & 0 deletions test/npm-config-file/audit-ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"low": "true",
"whitelist": ["open"]
}
12 changes: 12 additions & 0 deletions test/npm-config-file/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/npm-config-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "audit-ci-npm-config",
"description": "Test package.json with config file",
"dependencies": {
"open": "0.0.5"
}
}
4 changes: 4 additions & 0 deletions test/yarn-config-file/audit-ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"low": "true",
"advisories": [663]
}
7 changes: 7 additions & 0 deletions test/yarn-config-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "audit-ci-yarn-config",
"description": "Test package.json with config file",
"dependencies": {
"open": "0.0.5"
}
}
8 changes: 8 additions & 0 deletions test/yarn-config-file/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


[email protected]:
version "0.0.5"
resolved "https://registry.yarnpkg.com/open/-/open-0.0.5.tgz#42c3e18ec95466b6bf0dc42f3a2945c3f0cad8fc"
integrity sha1-QsPhjslUZra/DcQvOilFw/DK2Pw=

0 comments on commit 39c3c87

Please sign in to comment.