Skip to content

Commit baa5927

Browse files
committed
various edits and fixes to allow inlcude flag and enhance globbing
1 parent ed8816d commit baa5927

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ code-notes is based on two npm modules, mainly forked from [fixme](https://githu
77
- Flexibility in defining the source scanning directory
88
- The ability to pass exclude patterns that are compatible with [multimatch](https://github.com/sindresorhus/multimatch)
99
- The ability to read exclusion list from a `.gitignore` file
10+
- The ability to include **only** certain path patterns to be scanned
1011

1112
It ends up giving you an output like this:
1213

@@ -38,6 +39,7 @@ notes --help
3839
-s, --source [dir] root directory to be included only for checks (default: current working directory)
3940
-x, --patterns [dir] Path patterns to exclude (default: include all files and directories)
4041
-e, --encoding [type] file encoding to be scanned (default: utf8)
42+
-i, --include [dir] Path patterns to include only (default: include all files and directories). Note that include patterns will overwrite any exclude patterns
4143
-l, --line-length <n> number of max characters a line (default: 1000)
4244
-h, --ignore-hidden <n> ignore hidden files (default: false)
4345
-g, --git-ignore <n> ignore patterns from your .gitignore file. This paramter accepts the path for the .gitIgnore file (default: false | no .gitignore is read
@@ -47,6 +49,7 @@ notes --help
4749

4850
* **source:** The path to scan through for notes, defaults to process.cwd()
4951
* **patterns:** Glob patterns for files directories to ignore. Passes these straight to [multimatch](https://github.com/sindresorhus/multimatch) so check there for more information on proper syntax.
52+
* **include** Glob patterns for files or directories to be inlucded **ONLY** in the scan process. Note that any include files will overwrite any exclude patterns
5053
* **ignoreHidden:** Define if you want to ignore hidden files and directories. Defaults to true as all paths will be scanned.
5154
* **encoding:** The encoding the files scanned will be opened as.
5255
* **lineLength:** The number of max characters a line can be before Fixme gives up and doen not scan it for matches. If a line is too long, the regular expression will take an extremely long time to finish. *You have been warned!*
@@ -112,8 +115,12 @@ Those comments would be annotated as:
112115
notes -g .gitignore -h true
113116
# Exclude any file under the src directory and node_modules and any file with .md extension
114117
notes -x src/ -x -x node_modules/ -x *.md
118+
# Only scan .md files
119+
notes -i "*.md"
115120
```
116121

122+
> **Important**: For some reason that i still cant figure out, some extensions like `.md` `.html` have to be wrapped with `"`. So if your pattern does not seem to work at first, try to wrap it with quotes
123+
117124
### Extending code-notes
118125

119126
code-notes scan for NOTE, OPTIMIZE, TODO, HACK, XXX, FIXME, and BUG comments within your source, and print them to stdout so you can deal with them. However, if you wish to define more annotations to be extracted, this can be easily done by extending the definitions in `lib/messageChecks.js`. An example for an annotation:
@@ -128,10 +135,11 @@ todo: {
128135

129136
#### Ignoring files
130137

131-
Certain file extensions are skipped from being scanned. They are defined in `lib/notes.js`
138+
Certain file extensions and directories are skipped from being scanned. They are defined in `lib/notes.js`
132139

133140
```javascript
134-
const BAD_EXTENSIONS = ["!*.jpg", "!*.jpeg", "!*.mov", "!*.mp3", "!*.gif", "!*.png", "!*.log", "!*.bin", "!*.psd", "!*.swf", "!*.fla", "!*.ico"];
141+
const BAD_EXTENSIONS = ["!*.jpg", "!*.jpeg", "!*.mov", "!*.mp3", "!*.gif", "!*.png", "!*.log", "!*.bin", "!*.psd", "!*.swf", "!*.fla", "!*.ico", "!*.jar", "!*.war", "!*.ear", "!*.zip", "!*.tar.gz", "!*.rar"];
142+
const BAD_DIRECTORIES= ["!.git/**", "!.sass-cache/**", "!coverage/**"]
135143
```
136144

137145
The object should contain the following fields:

bin/notes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ program
1212
.option('-s, --source [dir]', 'root directory to be included only for checks (default: current working directory)')
1313
.option('-x, --patterns [dir]', 'Path patterns to exclude (default: include all files and directories)',
1414
function collect(val, collector) { collector.push("" + val); return collector; }, [])
15+
.option('-i, --include [dir]', 'Path patterns to include only (default: include all files and directories). Note that include patterns will overwrite any exclude patterns',
16+
function collect(val, collector) { collector.push("" + val); return collector; }, [])
1517
.option('-e, --encoding [type]', 'file encoding to be scanned (default: utf8)')
1618
.option('-l, --line-length <n>', 'number of max characters a line (default: 1000)', parseInt)
1719
.option('-h, --ignore-hidden <n>', 'ignore hidden files (default: false)')

lib/filter.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict';
22

33
const multimatch = require('multimatch');
4+
const _ = require('lodash');
45

5-
6-
let ignoreHidden, patterns;
6+
let ignoreHidden, patterns, includePatterns;
77

88
class Filter {
99

10-
constructor(_ignoreHidden, _patterns) {
11-
ignoreHidden = _ignoreHidden;
12-
patterns = _patterns;
10+
constructor(_ignoreHidden, _patterns, _includePatterns) {
11+
ignoreHidden = _ignoreHidden;
12+
patterns = _patterns;
13+
includePatterns = _includePatterns;
1314
}
1415

1516
/**
@@ -26,9 +27,22 @@ class Filter {
2627
* @return {Boolean}
2728
*/
2829
fileFilterer(fileInformation) {
30+
2931
let matchOptions = {};
32+
33+
// Check if we have any include patterns and those will overwrite any exclude patters
34+
if (!!includePatterns.length) patterns = includePatterns;
35+
36+
// We want to extract directory filters and based on those check the directory first
37+
let directoriesPatterns = _.compact(patterns.map(function(pattern){ if (pattern.indexOf('/') > -1) return pattern }));
38+
let filePatterns = _.difference(patterns, directoriesPatterns);
39+
3040
if (!ignoreHidden) matchOptions["dot"] = true;
31-
return multimatch(fileInformation.path, patterns, matchOptions).length;
41+
42+
if (!!directoriesPatterns.length){
43+
if (!multimatch(fileInformation.path, patterns, matchOptions).length) return false;
44+
}
45+
return multimatch(fileInformation.name, filePatterns, matchOptions).length;
3246
}
3347
}
3448
module.exports = Filter;

lib/notes.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ const Filter = require('./filter');
1111
const formatter = require('./formatter');
1212
const messageChecks = require('./messageChecks');
1313

14-
const BAD_EXTENSIONS = ["!*.jpg", "!*.jpeg", "!*.mov", "!*.mp3", "!*.gif", "!*.png", "!*.log", "!*.bin", "!*.psd", "!*.swf", "!*.fla", "!*.ico"];
14+
const BAD_EXTENSIONS = ["!*.jpg", "!*.jpeg", "!*.mov", "!*.mp3", "!*.gif", "!*.png", "!*.log", "!*.bin", "!*.psd", "!*.swf", "!*.fla", "!*.ico", "!*.jar", "!*.war", "!*.ear", "!*.zip", "!*.tar.gz", "!*.rar"];
15+
const BAD_DIRECTORIES= ["!.git/**", "!.sass-cache/**", "!coverage/**"]
1516

16-
let scanPath, fileEncoding, lineLengthLimit, gitIgnore, patterns, ignoreHidden, annotationsFound;
17+
let scanPath, includePatterns, fileEncoding, lineLengthLimit, gitIgnore, patterns, ignoreHidden, annotationsFound;
1718

1819
/**
1920
* Takes a line of a file and the line number, and returns an array of all of
@@ -91,7 +92,7 @@ function logMessages(messagesInfo) {
9192
*/
9293
function scanAndProcessMessages() {
9394

94-
let filter = new Filter(ignoreHidden, patterns);
95+
let filter = new Filter(ignoreHidden, patterns, includePatterns);
9596

9697
let options = {
9798
root : scanPath,
@@ -164,6 +165,7 @@ function parseUserOptionsAndScan(options) {
164165

165166
scanPath = options.source ? options.source : process.cwd();
166167
patterns = options.patterns.length ? _.concat(['**'], options.patterns) : ['**'];
168+
includePatterns = options.include.length ? _.concat(['!**'], options.include) : [];
167169
ignoreHidden = options.ignoreHidden || false;
168170
gitIgnore = options.gitIgnore || false;
169171
lineLengthLimit = options.lineLength || 1000;
@@ -176,9 +178,10 @@ function parseUserOptionsAndScan(options) {
176178
});
177179

178180
patterns = _.concat(patterns, BAD_EXTENSIONS);
181+
patterns = _.concat(patterns, BAD_DIRECTORIES);
179182

180183
if (gitIgnore) {
181-
184+
console.log(patterns)
182185
try {
183186

184187
let data = fs.readFileSync(gitIgnore, 'utf8');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Ahmad Assaf",
33
"name": "code-notes",
44
"description": "Tool to summarise all code annotation like TODO or FIXME",
5-
"version": "1.0.1",
5+
"version": "1.0.2",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/ahmadassaf/code-notes.git"

0 commit comments

Comments
 (0)