Skip to content

Commit 79f8fb1

Browse files
authored
2.0.0 (#22)
* Add node 7 to travis build list * Alter coverage check to exclude require method * Remove grunt watch, bump deps, and move to 2.0.0 * Add extends support and add new severity cli option * Remove default config * Update docs and changelog * Remove reference to old default config
1 parent 1625510 commit 79f8fb1

File tree

12 files changed

+386
-235
lines changed

12 files changed

+386
-235
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ node_js:
1313
- "4"
1414
- "5"
1515
- "6"
16+
- "7"
1617

1718
cache:
1819
directories:

CHANGELOG.md

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

1212
### Removed
1313

14+
## [2.0.0] - 2017-02-18
15+
### Added
16+
- Support configuration extension!! Now you can add "extends" to your rc file to extend a based configuration. This is great for sharing a standard ruleset between many projects. Please see the [wiki](https://github.com/tclindner/npm-package-json-lint/wiki) for more information.
17+
- An optional cli flag for controlling rule severity. Please see the [README.md](README.md) for examples.
18+
19+
### Removed
20+
- Default configuration is no longer provided. Please see the new default config module, [npm-package-json-lint-config-default](https://github.com/tclindner/npm-package-json-lint-config-default)
21+
1422
## [1.4.0] - 2016-10-15
1523
### Added
1624
- New rule: [valid-values-license](https://github.com/tclindner/npm-package-json-lint/wiki/valid-values-license)

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ First thing first, let's make sure you have the necessary pre-requisites.
5353
### Examples
5454

5555
Run a specific rule, require-author, on a file relative to the current working directory.
56-
`pjl-cli -f "../relative-path/package.json" -r "require-author: 'error'"`
56+
`pjl-cli -f "../relative-path/package.json" -r "require-author"`
5757

5858
Run a specific rule, require-author, ignoring warnings on a file relative to the current working directory.
59-
`pjl-cli -f "../relative-path/package.json" -r "require-author: 'error'" --ignore-warnings`
59+
`pjl-cli -f "../relative-path/package.json" -r "require-author" --ignore-warnings`
60+
61+
Run a specific rule, require-author, set severity to warning on a file relative to the current working directory.
62+
`pjl-cli -f "../relative-path/package.json" -r "require-author" -s "warning"`
6063

6164
Run using the config in `.npmpackagejsonlintrc` on a file relative to the current working directory.
6265
`pjl-cli -f "../relative-path/package.json" -c "./.npmpackagejsonlintrc"`
@@ -66,7 +69,7 @@ Run using the default config on a file relative to the current working directory
6669

6770
## Lint Rules
6871

69-
npm-package-json-lint has a configurable set of rules. Please see the [wiki](https://github.com/tclindner/npm-package-json-lint/wiki) for a full list of available rules. By [default](src/defaultConfig.js) only type checks and name/version rules are enforced. This is the bare minimum configuration.
72+
npm-package-json-lint has a configurable set of rules. Please see the [wiki](https://github.com/tclindner/npm-package-json-lint/wiki) for a full list of available rules. By default no rules are enabled. If you would like to use npm-package-json-lint's default ruleset, please see [npm-package-json-lint-config-default](https://github.com/tclindner/npm-package-json-lint-config-default).
7073

7174
Each rule contains the following properties:
7275

@@ -75,17 +78,22 @@ Each rule contains the following properties:
7578
3. Message - example: author is required
7679
4. Rule Type - example: required
7780

78-
As mentioned in the "Commands and configuration" section there are two ways to specify rule sets. The first is using `--rule` to specify a given rule. This will run npm-package-json-lint with just this rule. The second is using `--rules-file` to specify a JSON file, named [`.npmpackagejsonlintrc`](https://github.com/tclindner/npm-package-json-lint/wiki/npm-package-json-lint-rc), to run a set of rules. If neither of the options above are specified then npm-package-json-lint looks for a global [`.npmpackagejsonlintrc`](https://github.com/tclindner/npm-package-json-lint/wiki/npm-package-json-lint-rc) file in the root of your user directory. Finally, if a global [`.npmpackagejsonlintrc`](https://github.com/tclindner/npm-package-json-lint/wiki/npm-package-json-lint-rc) file doesn't exist then the [defaults](src/defaultConfig.js) are used.
81+
As mentioned in the "Commands and configuration" section there are two ways to specify rule sets. The first is using `--rule` to specify a given rule. This will run npm-package-json-lint with just this rule. The second is using `--rules-file` to specify a JSON file, named [`.npmpackagejsonlintrc`](https://github.com/tclindner/npm-package-json-lint/wiki/npm-package-json-lint-rc), to run a set of rules. If neither of the options above are specified then npm-package-json-lint looks for a global [`.npmpackagejsonlintrc`](https://github.com/tclindner/npm-package-json-lint/wiki/npm-package-json-lint-rc) file in the root of your user directory.
7982

8083
### Configuring rules
8184

82-
npm-package-json-lint rules can either be run as an `error` or a `warning`.
85+
npm-package-json-lint rules can either be run as an `error`, `warning`, or `off`.
8386

8487
* "warning" - run the rule as a warning
8588
* "error" - run the rule as an error
89+
* "off" - disables the rule
8690

8791
Ex: `"require-author": "error"`
8892

93+
## Migrating from v10.x.x to 2.x.x
94+
95+
Please see the [migration guide](https://github.com/tclindner/npm-package-json-lint/wiki/migrating-from-v1-to-v2).
96+
8997
## Migrating from v0.x.x to 1.x.x
9098

9199
Please see the [migration guide](https://github.com/tclindner/npm-package-json-lint/wiki/migrating-from-v0-to-v1).
@@ -101,6 +109,7 @@ Please see [CHANGELOG.md](CHANGELOG.md).
101109
## Related
102110

103111
* [grunt-npm-package-json-lint](https://github.com/tclindner/grunt-npm-package-json-lint): Grunt Wrapper for npm-package-json-lint
112+
* [npm-package-json-lint-config-default](https://github.com/tclindner/npm-package-json-lint-config-default): Shared default configuration module for npm-package-json-lint
104113

105114
## License
106115

grunt/coverage-check.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ module.exports = function(grunt) {
66
options: {
77
thresholds: {
88
statements: 100,
9-
branches: 98,
9+
branches: 99,
1010
lines: 100,
11-
functions: 100
11+
functions: 99
1212
},
1313
dir: 'coverage',
1414
root: 'tests'

grunt/watch.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

package.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "1.4.0",
3+
"version": "2.0.0",
44
"description": "CLI app for linting package.json files.",
55
"keywords": [
66
"lint",
@@ -24,33 +24,32 @@
2424
},
2525
"main": "src/NpmPackageJsonLint.js",
2626
"dependencies": {
27-
"chalk": "^1.1.1",
27+
"chalk": "^1.1.3",
2828
"commander": "^2.9.0",
2929
"in-array": "^0.1.2",
3030
"is-plain-obj": "^1.1.0",
3131
"semver": "^5.3.0",
3232
"user-home": "^2.0.0",
33-
"validator": "^6.0.0"
33+
"validator": "^6.2.1"
3434
},
3535
"devDependencies": {
36-
"eslint": "^3.7.1",
37-
"eslint-config-tc": "^1.2.0",
36+
"eslint": "^3.15.0",
37+
"eslint-config-tc": "^1.4.0",
3838
"eslint-formatter-pretty": "^1.0.0",
3939
"grunt": "^1.0.1",
4040
"grunt-bump": "^0.8.0",
4141
"grunt-contrib-clean": "^1.0.0",
4242
"grunt-contrib-copy": "^1.0.0",
43-
"grunt-contrib-watch": "^1.0.0",
4443
"grunt-env": "^0.4.4",
4544
"grunt-eslint": "^19.0.0",
46-
"grunt-istanbul": "^0.7.1",
45+
"grunt-istanbul": "^0.7.2",
4746
"grunt-istanbul-coverage": "^0.1.4",
4847
"grunt-jscs": "^3.0.1",
4948
"grunt-jsonlint": "^1.1.0",
5049
"grunt-mocha-test": "^0.13.2",
51-
"mocha": "^3.1.0",
52-
"should": "^11.1.0",
53-
"sinon": "^1.17.5",
50+
"mocha": "^3.2.0",
51+
"should": "^11.2.0",
52+
"sinon": "^1.17.7",
5453
"time-grunt": "^1.4.0"
5554
},
5655
"engines": {

src/Config.js

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@ class Config {
2121
'no-restricted-pre-release-devDependencies'
2222
];
2323

24-
if (this._isConfigPassed(passedConfigParam)) {
25-
const passedConfig = this._getPassedConfig(passedConfigParam);
26-
27-
this.config = Object.assign({}, passedConfig);
28-
} else {
29-
this.defaultConfig = require('./defaultConfig');
30-
this.config = Object.assign({}, this.defaultConfig);
31-
}
24+
this.passedConfigParam = passedConfigParam;
3225
}
3326

3427
/**
3528
* Gets the config
3629
* @return {Object} Config object
3730
*/
3831
get() {
39-
return this.config;
32+
if (this._isConfigPassed(this.passedConfigParam)) {
33+
const passedConfig = this._getPassedConfig(this.passedConfigParam);
34+
let extendsConfig = {};
35+
36+
if (passedConfig.hasOwnProperty('extends')) {
37+
extendsConfig = this._getExtendsConfig(passedConfig.extends);
38+
}
39+
40+
return Object.assign({}, extendsConfig, passedConfig.rules);
41+
} else {
42+
throw new Error('No configuration passed');
43+
}
4044
}
4145

4246
/**
@@ -75,24 +79,62 @@ class Config {
7579
}
7680

7781
/**
78-
* Validates config file
79-
* @param {Object} rcFileObj Object version of .npmpackagejsonlintrc file
80-
* @return {boolean} True if validate config is successful
82+
* Gets configuration from a extends config module
83+
* @param {String} moduleName Name of the configuration module
84+
* @return {Object} Configuration object
85+
*/
86+
_getExtendsConfig(moduleName) {
87+
const configObj = this._getExtendsConfigModule(moduleName);
88+
89+
this._validateConfig(configObj);
90+
91+
return configObj.rules;
92+
}
93+
94+
/**
95+
* Loads extends config module
96+
* @param {String} moduleName Name of the configuration module
97+
* @return {Object} Configuration object
98+
*/
99+
_getExtendsConfigModule(moduleName) {
100+
/* istanbul ignore next */
101+
return require(moduleName);
102+
}
103+
104+
/**
105+
* Validates config object
106+
* @param {Object} rcFileObj Object version of .npmpackagejsonlintrc file
107+
* @return {boolean} True if validate config is successful
81108
*/
82109
_validateConfig(rcFileObj) {
83-
for (const rule in rcFileObj) {
84-
const ruleConfig = rcFileObj[rule];
110+
if (rcFileObj.hasOwnProperty('rules')) {
111+
this._validateRulesConfig(rcFileObj.rules);
112+
} else {
113+
throw new Error('`rules` object missing in config');
114+
}
115+
116+
return true;
117+
}
118+
119+
/**
120+
* Validates rules object
121+
* @param {Object} rulesObj Object version of .npmpackagejsonlintrc file
122+
* @return {boolean} True if validate config is successful
123+
*/
124+
_validateRulesConfig(rulesObj) {
125+
for (const rule in rulesObj) {
126+
const ruleConfig = rulesObj[rule];
85127

86128
if (Array.isArray(ruleConfig) && inArray(this.arrayRules, rule)) {
87129
if (typeof ruleConfig[0] !== 'string' || this._isRuleValid(ruleConfig[0])) {
88-
throw new Error(`${rule} - first key must be set to "error" or "warning". Currently set to ${ruleConfig[0]}`);
130+
throw new Error(`${rule} - first key must be set to "error", "warning", or "off". Currently set to ${ruleConfig[0]}`);
89131
}
90132

91133
if (!Array.isArray(ruleConfig[1])) {
92134
throw new Error(`${rule} - second key must be set an array. Currently set to ${ruleConfig[1]}`);
93135
}
94136
} else if (typeof ruleConfig !== 'string' || this._isRuleValid(ruleConfig)) {
95-
throw new Error(`${rule} - must be set to "error" or "warning". Currently set to ${ruleConfig}`);
137+
throw new Error(`${rule} - must be set to "error", "warning", or "off". Currently set to ${ruleConfig}`);
96138
}
97139
}
98140

@@ -105,7 +147,7 @@ class Config {
105147
* @return {Boolean} True if the rule is valid. False if the rule is invalid.
106148
*/
107149
_isRuleValid(key) {
108-
return typeof key === 'string' && key !== 'error' && key !== 'warning';
150+
return typeof key === 'string' && key !== 'error' && key !== 'warning' && key !== 'off';
109151
}
110152

111153
}

src/NpmPackageJsonLint.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,34 @@ class NpmPackageJsonLint {
2222
this.warnings = [];
2323

2424
this.rules = new Rules();
25-
this._loadRules();
26-
27-
this.config = this._getConfig(config);
25+
this.rules.load();
26+
this.config = config;
2827
}
2928

3029
/**
3130
* Main execution method for package json lint.
3231
* @return {Object} Results object
3332
*/
3433
lint() {
35-
for (const rule in this.config) {
34+
const configObj = this._getConfig(this.config);
35+
36+
for (const rule in configObj) {
3637
const ruleModule = this.rules.get(rule);
3738

3839
if (inArray(this.arrayRuleTypes, ruleModule.ruleType)) {
39-
const errorWarningSetting = this.config[rule][0];
40-
const ruleConfigArray = this.config[rule][1];
41-
const lintResult = ruleModule.lint(this.packageJsonData, errorWarningSetting, ruleConfigArray);
40+
const errorWarningOffSetting = configObj[rule][0];
41+
const ruleConfigArray = configObj[rule][1];
42+
43+
if (errorWarningOffSetting !== 'off') {
44+
this._processResult(ruleModule.lint(this.packageJsonData, errorWarningOffSetting, ruleConfigArray), errorWarningOffSetting);
45+
}
4246

43-
this._processResult(lintResult, errorWarningSetting);
4447
} else {
45-
const errorWarningSetting = this.config[rule];
46-
const lintResult = ruleModule.lint(this.packageJsonData, errorWarningSetting);
48+
const errorWarningOffSetting = configObj[rule];
4749

48-
this._processResult(lintResult, errorWarningSetting);
50+
if (errorWarningOffSetting !== 'off') {
51+
this._processResult(ruleModule.lint(this.packageJsonData, errorWarningOffSetting), errorWarningOffSetting);
52+
}
4953
}
5054
}
5155

@@ -84,14 +88,6 @@ class NpmPackageJsonLint {
8488
return result;
8589
}
8690

87-
/**
88-
* Private method for loading rules
89-
* @return {undefined} No return
90-
*/
91-
_loadRules() {
92-
this.rules.load();
93-
}
94-
9591
/**
9692
* Private method for loading config
9793
* @param {Object|String} config Object or string with desired configuration

src/cli.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Reporter = require('./Reporter');
1212
const userHome = require('user-home');
1313

1414
const DEFAULT_FILE_NAME = './package.json';
15+
const DEFAULT_RULE_SEVERITY = 'error';
1516
const RC_FILE_NAME = '.npmpackagejsonlintrc';
1617

1718
/**
@@ -32,6 +33,7 @@ cliApp.version(pkg.version);
3233
cliApp.usage(pkg.name);
3334
cliApp.option('-f, --file <filePath>', `File path including name. Defaults to ${DEFAULT_FILE_NAME}`, DEFAULT_FILE_NAME);
3435
cliApp.option('-r, --rule <rule name>', 'Valid rule name to check. Defaults to nothing');
36+
cliApp.option('-s, --rule-severity <rule severity>', `"error" or "warning". Defaults to ${DEFAULT_RULE_SEVERITY}`, DEFAULT_RULE_SEVERITY);
3537
cliApp.option('-c, --rules-file <filePath>', 'File path of .npmpackagejsonlintrc');
3638
cliApp.option('-w, --ignore-warnings', 'Ignore warnings');
3739
cliApp.parse(process.argv);
@@ -52,7 +54,10 @@ let rulesLoaded = false;
5254
let rulesConfig = {};
5355

5456
if (typeof cliApp.rule !== 'undefined') {
55-
rulesConfig[cliApp.rule] = true;
57+
const rules = {};
58+
59+
rules[cliApp.rule] = cliApp.ruleSeverity;
60+
rulesConfig.rules = rules;
5661
rulesLoaded = true;
5762
}
5863

0 commit comments

Comments
 (0)