Skip to content

Commit a1d142b

Browse files
authored
Add quiet cli option (#32)
* Add quiet cli option Fixes #30 Bump version to 2.2.0 Add new quiet (-q) option to cli that suppresses output if no errors are found Add tests for the CLI!! * Update changelog with quiet cli feature
1 parent f152ace commit a1d142b

File tree

14 files changed

+469
-9
lines changed

14 files changed

+469
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
### Removed
1313

14+
## [2.2.0] - 2017-04-12
15+
### Added
16+
- Add new quiet (-q) option to cli that suppresses output if no errors
17+
1418
## [2.1.2] - 2017-04-10
1519
### Fixed
1620
- Array style rules, like `valid-values-author`, so they can be easily turned off by setting `valid-values-author: 'off'`

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ First thing first, let's make sure you have the necessary pre-requisites.
4949
| pjl-cli --rule <rule name> | -r | Valid rule name to check. Defaults to nothing |
5050
| pjl-cli --rules-file <file path> | -c | File path of .npmpackagejsonlintrc |
5151
| pjl-cli --rule-severity <rule severity> | -s | "error" or "warning". Defaults to "error" |
52+
| pjl-cli --quiet | -q | Report errors only |
5253
| pjl-cli --ignore-warnings | -w | Ignore warnings |
5354

5455
### Examples

grunt/mocha.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = function(grunt) {
33
grunt.config('mochaTest', {
44
local: {
55
options: {
6-
reporter: 'spec'
6+
reporter: 'spec',
7+
timeout: 5000
78
},
89
src: ['tests/unit/**/*.js']
910
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "2.1.2",
3+
"version": "2.2.0",
44
"description": "CLI app for linting package.json files.",
55
"keywords": [
66
"lint",

src/cli.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,26 @@ const handleError = function(err) {
2828
throw new Error(err);
2929
};
3030

31+
/**
32+
* Helper function to determine if output should be logged
33+
*
34+
* @param {boolean} quietFlag Flag indicating if output should be logged if no error occur
35+
* @param {boolean} hasErrors True if the linter found errors. False if it didn't.
36+
* @return {boolean} True if the message should be logged. False if the message shouldn't be logged.
37+
*/
38+
const shouldLogOutput = function(quietFlag, hasErrors) {
39+
/* eslint no-extra-parens: "off" */
40+
return !quietFlag || (quietFlag && hasErrors);
41+
};
42+
3143
// configure cli options
3244
cliApp.version(pkg.version);
3345
cliApp.usage(pkg.name);
3446
cliApp.option('-f, --file <filePath>', `File path including name. Defaults to ${DEFAULT_FILE_NAME}`, DEFAULT_FILE_NAME);
3547
cliApp.option('-r, --rule <rule name>', 'Valid rule name to check. Defaults to nothing');
3648
cliApp.option('-s, --rule-severity <rule severity>', `"error" or "warning". Defaults to ${DEFAULT_RULE_SEVERITY}`, DEFAULT_RULE_SEVERITY);
3749
cliApp.option('-c, --rules-file <filePath>', 'File path of .npmpackagejsonlintrc');
50+
cliApp.option('-q, --quiet', 'Report errors only');
3851
cliApp.option('-w, --ignore-warnings', 'Ignore warnings');
3952
cliApp.parse(process.argv);
4053

@@ -90,21 +103,28 @@ try {
90103
const npmPackageJsonLint = new NpmPackageJsonLint(fileData, rulesConfig, options);
91104
const output = npmPackageJsonLint.lint();
92105
const reporter = new Reporter();
106+
let hasErrors = false;
93107

94108
for (const issueType in output) {
95109
const issues = output[issueType];
96110

97111
if (issues.length > noIssues && issueType === 'errors') {
98112
exitCode = issuesDetectedErrorCode;
113+
hasErrors = true;
99114
}
100115

101-
reporter.write(output[issueType], issueType);
116+
if (shouldLogOutput(cliApp.quiet, hasErrors)) {
117+
reporter.write(output[issueType], issueType);
118+
}
102119
}
103120

104121
const formattedFileName = chalk.bold.green(filePath);
105122

106123
process.exitCode = exitCode;
107-
console.log(`${formattedFileName} check complete`);
124+
125+
if (shouldLogOutput(cliApp.quiet, hasErrors)) {
126+
console.log(`${formattedFileName} check complete`);
127+
}
108128
} catch (err) {
109129
handleError(err);
110130
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"rules": {
3+
"require-author": "error",
4+
"require-description": "error",
5+
"require-devDependencies": "error",
6+
"require-homepage": "error",
7+
"require-keywords": "error",
8+
"require-name": "error",
9+
"require-repository": "error",
10+
"require-scripts": "error",
11+
"require-version": "error",
12+
"description-type": "error",
13+
"devDependencies-type": "error",
14+
"homepage-type": "error",
15+
"keywords-type": "error",
16+
"name-type": "error",
17+
"repository-type": "error",
18+
"version-type": "error",
19+
"valid-values-author": ["error", [
20+
"Thomas Lindner"
21+
]],
22+
"name-format": "error",
23+
"version-format": "error"
24+
}
25+
}

tests/fixtures/errors/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "npm-package-json-lint",
3+
"version": "0.1.0",
4+
"description": "CLI app for linting package.json files.",
5+
"keywords": [
6+
"lint"
7+
],
8+
"homepage": "https://github.com/tclindner/npm-package-json-lint",
9+
"author": "Thomas Lindner",
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/tclindner/npm-package-json-lint"
13+
},
14+
"devDependencies": {
15+
"mocha": "^2.4.5"
16+
}
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"rules": {
3+
"require-author": "error",
4+
"require-description": "error",
5+
"require-devDependencies": "error",
6+
"require-homepage": "error",
7+
"require-keywords": "error",
8+
"require-license": "warning",
9+
"require-name": "error",
10+
"require-repository": "error",
11+
"require-scripts": "error",
12+
"require-version": "error",
13+
"description-type": "error",
14+
"devDependencies-type": "error",
15+
"homepage-type": "error",
16+
"keywords-type": "error",
17+
"name-type": "error",
18+
"repository-type": "error",
19+
"version-type": "error",
20+
"valid-values-author": ["error", [
21+
"Thomas Lindner"
22+
]],
23+
"name-format": "error",
24+
"version-format": "error"
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "npm-package-json-lint",
3+
"version": "0.1.0",
4+
"description": "CLI app for linting package.json files.",
5+
"keywords": [
6+
"lint"
7+
],
8+
"homepage": "https://github.com/tclindner/npm-package-json-lint",
9+
"author": "Thomas Lindner",
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/tclindner/npm-package-json-lint"
13+
},
14+
"devDependencies": {
15+
"mocha": "^2.4.5"
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"rules": {
3+
"require-author": "error",
4+
"require-description": "error",
5+
"require-devDependencies": "error",
6+
"require-homepage": "error",
7+
"require-keywords": "error",
8+
"require-name": "error",
9+
"require-repository": "error",
10+
"require-version": "error",
11+
"description-type": "error",
12+
"devDependencies-type": "error",
13+
"homepage-type": "error",
14+
"keywords-type": "error",
15+
"name-type": "error",
16+
"repository-type": "error",
17+
"version-type": "error",
18+
"valid-values-author": ["error", [
19+
"Thomas Lindner"
20+
]],
21+
"name-format": "error",
22+
"version-format": "error"
23+
}
24+
}

0 commit comments

Comments
 (0)