Skip to content

Commit 498d329

Browse files
authored
Config alternatives (#45)
* #43 Add test fixtures for new config loading options * Bump version and dependencies * Pass package.json file contents to Config class * #43 Update config class with new options 1. a `npmPackageJsonLintConfig` property in `package.json` 2. a `.npmpackagejsonlintrc` file in the current working directory 3. a `npmpackagejsonlint.config.js` file that exports a config object in the current working directory. 4. a global `.npmpackagejsonlintrc` file in the root of your user directory 5. a global `npmpackagejsonlint.config.js` file that exports a config object in the root of your user directory * #43 Update changelog with new config options * #43 Update README with documentation on configuration * Fix case typo in file page * Adjusting coverage for require methods
1 parent 35b33fb commit 498d329

File tree

10 files changed

+428
-46
lines changed

10 files changed

+428
-46
lines changed

CHANGELOG.md

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

1212
### Removed
1313

14+
## [2.7.0] - 2017-08-08
15+
### Added
16+
- The ability to pass config using:
17+
1. a `npmPackageJsonLintConfig` property in `package.json`
18+
2. a `.npmpackagejsonlintrc` file in the current working directory
19+
3. a `npmpackagejsonlint.config.js` file that exports a config object in the current working directory.
20+
4. a global `.npmpackagejsonlintrc` file in the root of your user directory
21+
5. a global `npmpackagejsonlint.config.js` file that exports a config object in the root of your user directory
22+
1423
## [2.6.0] - 2017-07-30
1524
### Changed
1625
- Bumped dependencies

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ Run a specific rule, require-author, set severity to warning on a file relative
6666
Run using the config in `.npmpackagejsonlintrc` on a file relative to the current working directory.
6767
`pjl-cli -f "../relative-path/package.json" -c "./.npmpackagejsonlintrc"`
6868

69-
Run using the default config on a file relative to the current working directory
69+
Run on file relative to the current working directory. npm-package-json-lint attempts to find config. See lookup order below.
7070
`pjl-cli -f "../relative-path/package.json"`
7171

72+
Run on file in the current working directory. npm-package-json-lint attempts to find config. See lookup order below.
73+
`pjl-cli`
74+
7275
## Lint Rules
7376

7477
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).
@@ -80,7 +83,13 @@ Each rule contains the following properties:
8083
3. Message - example: author is required
8184
4. Rule Type - example: required
8285

83-
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.
86+
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 providing a configuration object. As of v2.7.0, there are multiple ways to provide a [configuration object](https://github.com/tclindner/npm-package-json-lint/wiki/configuration).
87+
88+
1. Adding a `--rules-file` to the command to specify a JSON file. This file is typically named [`.npmpackagejsonlintrc`](https://github.com/tclindner/npm-package-json-lint/wiki/npm-package-json-lint-rc); however, you may optionally add a .json extension if you prefer.
89+
2. Add a `npmPackageJsonLintConfig` property in `package.json` file
90+
3. Add a `npmpackagejsonlint.config.js` file that exports a config object in the current working directory.
91+
4. Add a global `.npmpackagejsonlintrc.json` file in the root of your user directory
92+
5. Add a global `npmpackagejsonlint.config.js` file that exports a config object in the root of your user directory
8493

8594
### Configuring rules
8695

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "2.6.0",
3+
"version": "2.7.0",
44
"description": "CLI app for linting package.json files.",
55
"keywords": [
66
"lint",
@@ -31,10 +31,10 @@
3131
"eslint": "eslint . --format=node_modules/eslint-formatter-pretty",
3232
"lint": "npm run eslint",
3333
"test": "mocha tests/unit --recursive",
34-
"coverage": "nyc --extension .js --check-coverage --lines 99 --branches 97 --functions 98 npm test"
34+
"coverage": "nyc --extension .js --check-coverage --lines 99 --branches 97 --functions 96 npm test"
3535
},
3636
"dependencies": {
37-
"chalk": "^2.0.1",
37+
"chalk": "^2.1.0",
3838
"commander": "^2.11.0",
3939
"in-array": "^0.1.2",
4040
"is-plain-obj": "^1.1.0",
@@ -45,14 +45,14 @@
4545
"validator": "^8.0.0"
4646
},
4747
"devDependencies": {
48-
"chai": "^4.1.0",
49-
"eslint": "^4.3.0",
50-
"eslint-config-tc": "^2.0.0",
48+
"chai": "^4.1.1",
49+
"eslint": "^4.4.1",
50+
"eslint-config-tc": "^2.1.0",
5151
"eslint-formatter-pretty": "^1.1.0",
5252
"figures": "^2.0.0",
53-
"mocha": "^3.4.2",
53+
"mocha": "^3.5.0",
5454
"nyc": "^11.1.0",
55-
"sinon": "^2.4.1"
55+
"sinon": "^3.1.0"
5656
},
5757
"engines": {
5858
"node": ">=4.2.0",

src/Config.js

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
/* eslint class-methods-use-this: 'off' */
44

5+
const fs = require('fs');
56
const inArray = require('in-array');
67
const Parser = require('./Parser');
78
const path = require('path');
9+
const userHome = require('user-home');
810

911
class Config {
1012

1113
/**
1214
* Constructor
1315
* @param {Object|String} passedConfigParam Object or string with desired configuration
16+
* @param {Object} packageJsonData User's package.json data
1417
*/
15-
constructor(passedConfigParam) {
18+
constructor(passedConfigParam, packageJsonData) {
1619
this.arrayRules = [
1720
'valid-values-author',
1821
'valid-values-private',
@@ -23,29 +26,54 @@ class Config {
2326
];
2427

2528
this.passedConfigParam = passedConfigParam;
29+
this.packageJsonData = packageJsonData;
30+
this.rcFileName = '.npmpackagejsonlintrc';
31+
this.configFileName = 'npmpackagejsonlint.config.js';
2632
}
2733

2834
/**
2935
* Gets the config
3036
* @return {Object} Config object
3137
*/
3238
get() {
33-
if (this._isConfigPassed(this.passedConfigParam)) {
34-
const passedConfig = this._getPassedConfig(this.passedConfigParam);
35-
let extendsConfig = {};
39+
const userConfig = this._getUserConfig();
3640

37-
if (passedConfig.hasOwnProperty('extends')) {
38-
extendsConfig = this._getExtendsConfig(passedConfig.extends);
39-
}
41+
this._validateConfig(userConfig);
4042

41-
if (!passedConfig.hasOwnProperty('rules')) {
42-
return Object.assign({}, extendsConfig);
43-
}
43+
let extendsConfig = {};
44+
45+
if (userConfig.hasOwnProperty('extends')) {
46+
extendsConfig = this._getExtendsConfig(userConfig.extends);
47+
}
48+
49+
if (!userConfig.hasOwnProperty('rules')) {
50+
return Object.assign({}, extendsConfig);
51+
}
52+
53+
return Object.assign({}, extendsConfig, userConfig.rules);
54+
}
4455

45-
return Object.assign({}, extendsConfig, passedConfig.rules);
56+
/**
57+
* Get users config with multiple fallbacks
58+
*
59+
* @returns {Object} Users config
60+
*/
61+
_getUserConfig() {
62+
if (this._isConfigPassed(this.passedConfigParam)) {
63+
return this._getPassedConfig(this.passedConfigParam);
64+
} else if (this.packageJsonData.hasOwnProperty('npmPackageJsonLintConfig')) {
65+
return this.packageJsonData.npmPackageJsonLintConfig;
66+
} else if (this._isConfigFileExist(this._getRelativeConfigFilePth(this.rcFileName))) {
67+
return this._loadRcFile(this._getRelativeConfigFilePth(this.rcFileName));
68+
} else if (this._isConfigFileExist(this._getRelativeConfigFilePth(this.configFileName))) {
69+
return this._loadConfigFile(this._getRelativeConfigFilePth(this.configFileName));
70+
} else if (this._isConfigFileExist(this._getUsrHmConfigFilePath(this.rcFileName))) {
71+
return this._loadRcFile(this._getUsrHmConfigFilePath(this.rcFileName));
72+
} else if (this._isConfigFileExist(this._getUsrHmConfigFilePath(this.configFileName))) {
73+
return this._loadConfigFile(this._getUsrHmConfigFilePath(this.configFileName));
4674
}
4775

48-
throw new Error('No configuration passed');
76+
throw new Error('No configuration found');
4977
}
5078

5179
/**
@@ -73,11 +101,7 @@ class Config {
73101
configFile = path.join(process.cwd(), passedConfig);
74102
}
75103

76-
const rcFileObj = parser.parse(configFile);
77-
78-
this._validateConfig(rcFileObj);
79-
80-
return rcFileObj;
104+
return parser.parse(configFile);
81105
}
82106

83107
return passedConfig;
@@ -104,6 +128,57 @@ class Config {
104128
return configObj.rules;
105129
}
106130

131+
/**
132+
* Gets relative config file path
133+
*
134+
* @param {String} fileName Name of the file
135+
* @return {String} File path of the config file
136+
*/
137+
_getRelativeConfigFilePth(fileName) {
138+
return path.join(process.cwd(), fileName);
139+
}
140+
141+
/**
142+
* Gets userhome directory config file path
143+
*
144+
* @param {String} fileName Name of the file
145+
* @return {String} File path of the config file
146+
*/
147+
_getUsrHmConfigFilePath(fileName) {
148+
return path.join(userHome, fileName);
149+
}
150+
151+
/**
152+
* Checks if a .npmpackagejsonlintrc.json file exists
153+
*
154+
* @param {String} filePath Path of the file
155+
* @return {Boolean} true if it exists, false if not
156+
*/
157+
_isConfigFileExist(filePath) {
158+
return fs.existsSync(filePath);
159+
}
160+
161+
/**
162+
* Gets configuration from a extends config module
163+
* @param {String} filePath File path of config file
164+
* @return {Object} Configuration object
165+
*/
166+
_loadRcFile(filePath) {
167+
const parser = new Parser();
168+
169+
return parser.parse(filePath);
170+
}
171+
172+
/**
173+
* Checks if a .npmpackagejsonlintrc.json file exists
174+
*
175+
* @param {String} filePath File path of config file
176+
* @return {Boolean} true if it exists, false if not
177+
*/
178+
_loadConfigFile(filePath) {
179+
return require(filePath);
180+
}
181+
107182
/**
108183
* Loads extends config module
109184
* @param {String} moduleName Name of the configuration module

src/NpmPackageJsonLint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class NpmPackageJsonLint {
9292
* @return {Object} Configuration object for the run
9393
*/
9494
_getConfig(config) {
95-
const configObj = new Config(config);
95+
const configObj = new Config(config, this.packageJsonData);
9696

9797
return configObj.get();
9898
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
module.exports = {
4+
rules: {
5+
'require-author': 'error',
6+
'require-description': 'error',
7+
'require-devDependencies': 'error',
8+
'require-homepage': 'error',
9+
'require-keywords': 'error',
10+
'require-license': 'warning',
11+
'require-name': 'error',
12+
'require-repository': 'error',
13+
'require-scripts': 'error',
14+
'require-version': 'error',
15+
'description-type': 'error',
16+
'devDependencies-type': 'error',
17+
'homepage-type': 'error',
18+
'keywords-type': 'error',
19+
'name-type': 'error',
20+
'repository-type': 'error',
21+
'version-type': 'error',
22+
'valid-values-author': ['error', [
23+
'Thomas Lindner'
24+
]],
25+
'name-format': 'error',
26+
'version-format': 'error'
27+
}
28+
};
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
"npmPackageJsonLintConfig": {
18+
"rules": {
19+
"require-author": "error",
20+
"require-description": "error",
21+
"require-devDependencies": "error",
22+
"require-homepage": "error",
23+
"require-keywords": "error",
24+
"require-license": "warning",
25+
"require-name": "error",
26+
"require-repository": "error",
27+
"require-scripts": "error",
28+
"require-version": "error",
29+
"description-type": "error",
30+
"devDependencies-type": "error",
31+
"homepage-type": "error",
32+
"keywords-type": "error",
33+
"name-type": "error",
34+
"repository-type": "error",
35+
"version-type": "error",
36+
"valid-values-author": ["error", [
37+
"Thomas Lindner"
38+
]],
39+
"name-format": "error",
40+
"version-format": "error"
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)