Skip to content

Commit c3c10c0

Browse files
evilebottnawitclindner
authored andcommitted
feat: ignorePath option (#100)
1 parent 2d27127 commit c3c10c0

File tree

14 files changed

+292
-4
lines changed

14 files changed

+292
-4
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ First thing first, let's make sure you have the necessary pre-requisites.
5454
| npmPkgJsonLint --configFile <file path> | -c | File path to local config file or module name. |
5555
| npmPkgJsonLint --quiet | -q | Report errors only |
5656
| npmPkgJsonLint --noConfigFiles | -ncf | Skips loading project config files (i.e. .npmpackagejsonlintrc.json and npmpackagejsonlint.config.js) |
57+
| npmPkgJsonLint --ignorePath | -i | Path to a file containing patterns that describe files to ignore. |
5758

5859
### Examples
5960

@@ -99,6 +100,12 @@ $ npmPkgJsonLint --quiet ./packages
99100

100101
> Looks for all `package.json` files in the `packages` directory. The CLI engine automatically looks for relevant config files for each package.json file that is found. Removes any warnings from the output using the long form for quieting output.
101102
103+
```bash
104+
$ npmPkgJsonLint . --ignorePath .gitignore
105+
```
106+
107+
> Looks for all `package.json` files in the project and exclude ignored paths. The CLI engine automatically looks for relevant config files for each package.json file that is found.
108+
102109
## Node.js API
103110

104111
npm-package-json-lint exports two main objects: `CLIEngine` and `NpmPackageJsonLint`.
@@ -180,7 +187,8 @@ CLIEngine configuration object
180187
* `configFile` {string} Name of module/file to use.
181188
* `cwd` {string} The current working diretory for all file operations.
182189
* `useConfigFiles` {boolean} False disables use of .npmpackagejsonlintrc.json files and npmpackagejsonlint.config.js files.
183-
* `rules` {object} An object of rules to use.
190+
* `ignorePath` {string} Path to a file containing patterns that describe files to ignore. The path can be absolute or relative to process.cwd(). By default, npm-package-json-lint looks for .npmpackagejsonlintignore in process.cwd().
191+
* `rules` {object} An object of rules to use.
184192

185193
##### Example
186194

@@ -193,6 +201,7 @@ const cliEngineOptions = {
193201
configFile: '',
194202
cwd: process.cwd(),
195203
useConfigFiles: true,
204+
ignorePath: '',
196205
rules: {}
197206
};
198207

@@ -220,6 +229,7 @@ const cliEngineOptions = {
220229
configFile: '',
221230
cwd: process.cwd(),
222231
useConfigFiles: true,
232+
ignorePath: '',
223233
rules: {}
224234
};
225235
const patterns = ['.'];
@@ -267,6 +277,7 @@ const cliEngineOptions = {
267277
configFile: '',
268278
cwd: process.cwd(),
269279
useConfigFiles: true,
280+
ignorePath: '',
270281
rules: {}
271282
};
272283

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"ajv": "^6.7.0",
3838
"chalk": "^2.4.2",
3939
"glob": "^7.1.3",
40+
"ignore": "^5.0.5",
4041
"is-path-inside": "^2.0.0",
4142
"is-plain-obj": "^1.1.0",
4243
"is-resolvable": "^1.1.0",

src/CLIEngine.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ const path = require('path');
77
const NpmPackageJsonLint = require('./NpmPackageJsonLint');
88
const Config = require('./Config');
99
const glob = require('glob');
10+
const ignore = require('ignore');
1011
const ConfigValidator = require('./config/ConfigValidator');
1112
const Parser = require('./Parser');
1213
const pkg = require('../package.json');
1314

15+
const DEFAULT_IGNORE_FILENAME = '.npmpackagejsonlintignore';
16+
const FILE_NOT_FOUND_ERROR_CODE = 'ENOENT';
17+
1418
const noIssues = 0;
1519

1620
/**
@@ -154,6 +158,31 @@ const isIssueAnError = function(issue) {
154158
return issue.severity === 'error';
155159
};
156160

161+
/**
162+
* Generates ignorer based on ignore file content.
163+
*
164+
* @param {String} cwd Current work directory.
165+
* @param {CLIEngineOptions} options CLIEngineOptions object.
166+
* @returns {Object} Ignorer
167+
*/
168+
const getIgnorer = function(cwd, options) {
169+
const ignoreFilePath = options.ignorePath || DEFAULT_IGNORE_FILENAME;
170+
const absoluteIgnoreFilePath = path.isAbsolute(ignoreFilePath)
171+
? ignoreFilePath
172+
: path.resolve(cwd, ignoreFilePath);
173+
let ignoreText = '';
174+
175+
try {
176+
ignoreText = fs.readFileSync(absoluteIgnoreFilePath, 'utf8');
177+
} catch (readError) {
178+
if (readError.code !== FILE_NOT_FOUND_ERROR_CODE) {
179+
throw readError;
180+
}
181+
}
182+
183+
return ignore().add(ignoreText);
184+
};
185+
157186
/**
158187
* Generates a list of files to lint based on a list of provided patterns
159188
*
@@ -197,12 +226,13 @@ const getFileList = function(patterns, options) {
197226

198227
const files = [];
199228
const addedFiles = new Set();
229+
const ignorer = getIgnorer(cwd, options);
200230

201231
globPatterns.forEach((pattern) => {
202232
const file = path.resolve(cwd, pattern);
203233

204234
if (fs.existsSync(file) && fs.statSync(file).isFile()) {
205-
if (addedFiles.has(file)) {
235+
if (addedFiles.has(file) || ignorer.ignores(path.relative(cwd, file))) {
206236
return;
207237
}
208238

@@ -225,7 +255,7 @@ const getFileList = function(patterns, options) {
225255
globFiles.forEach((globFile) => {
226256
const filePath = path.resolve(cwd, globFile);
227257

228-
if (addedFiles.has(filePath)) {
258+
if (addedFiles.has(filePath) || ignorer.ignores(path.relative(cwd, filePath))) {
229259
return;
230260
}
231261

src/cli.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const cli = meow(`
2424
--quiet, -q Report errors only
2525
--noConfigFiles, -ncf Disables use of .npmpackagejsonlintrc.json files, npmpackagejsonlint.config.js files, and npmPackageJsonLintConfig object in package.json file.
2626
--configFile, -c File path of .npmpackagejsonlintrc.json
27+
--ignorePath, -i Path to a file containing patterns that describe files to ignore. The path can be absolute or relative to process.cwd(). By default, npm-package-json-lint looks for .npmpackagejsonlintignore in process.cwd().
2728
2829
Examples
2930
$ npmPkgJsonLint --version
@@ -34,6 +35,8 @@ const cli = meow(`
3435
$ npmPkgJsonLint --configFile ./config/npmpackagejsonlint.config.json .
3536
$ npmPkgJsonLint -q .
3637
$ npmPkgJsonLint --quiet ./packages
38+
$ npmPkgJsonLint . --ignorePath .gitignore
39+
$ npmPkgJsonLint . -i .gitignore
3740
`, {
3841
flags: {
3942
quiet: {
@@ -50,6 +53,11 @@ const cli = meow(`
5053
'type': 'string',
5154
'alias': 'c',
5255
'default': ''
56+
},
57+
ignorePath: {
58+
'type': 'string',
59+
'alias': 'i',
60+
'default': ''
5361
}
5462
}
5563
});
@@ -73,6 +81,7 @@ const cliEngineOptions = {
7381
configFile: cli.flags.configFile,
7482
cwd: process.cwd(),
7583
useConfigFiles: !cli.flags.noConfigFiles,
84+
ignorePath: cli.flags.ignorePath,
7685
rules: {}
7786
};
7887

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignoredDirectory
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
"prefer-property-order": ["error", [
24+
"name",
25+
"version",
26+
"description",
27+
"keywords",
28+
"homepage",
29+
"bugs",
30+
"author",
31+
"repository",
32+
"bin",
33+
"files",
34+
"main",
35+
"scripts",
36+
"dependencies",
37+
"devDependencies",
38+
"engines",
39+
"license"
40+
]]
41+
}
42+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/tclindner/npm-package-json-lint"
12+
},
13+
"devDependencies": {
14+
"mocha": "^2.4.5"
15+
}
16+
}
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignoredDirectory
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
"prefer-property-order": ["error", [
24+
"name",
25+
"version",
26+
"description",
27+
"keywords",
28+
"homepage",
29+
"bugs",
30+
"author",
31+
"repository",
32+
"bin",
33+
"files",
34+
"main",
35+
"scripts",
36+
"dependencies",
37+
"devDependencies",
38+
"engines",
39+
"license"
40+
]]
41+
}
42+
}

0 commit comments

Comments
 (0)