Skip to content

Commit

Permalink
Add ignoreFrom option for reusable ignores
Browse files Browse the repository at this point in the history
Closes GH-41.
  • Loading branch information
wooorm committed Mar 30, 2020
1 parent 458be8e commit e062590
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 9 deletions.
11 changes: 9 additions & 2 deletions doc/ignore.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ an error is thrown.
These files can be silently ignored by turning on
[`silentlyIgnore`][silently-ignore].

Files are ignored based on the path of the found ignore file and the patterns
inside it.
Normally, files are ignored based on the path of the found ignore file and the
patterns inside it.
Patterns passed with [`ignorePatterns`][ignore-patterns] are resolved based on
the current working directory.

Patterns in [`ignorePath`][ignore-path] and [`ignorePatterns`][ignore-patterns]
can be resolved from somewhere else by passing [`ignoreFrom`][ignore-from].

If paths or globs to directories are given to the engine, they will be searched
for matching files, but `node_modules` and hidden directories (those starting
Expand Down Expand Up @@ -72,6 +77,8 @@ test/{input,tree}

[ignore-patterns]: options.md#optionsignorepatterns

[ignore-from]: options.md#optionsignorefrom

[silently-ignore]: options.md#optionssilentlyignore

[gitignore]: https://git-scm.com/docs/gitignore
Expand Down
73 changes: 73 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* [`options.detectIgnore`](#optionsdetectignore)
* [`options.ignorePath`](#optionsignorepath)
* [`options.ignorePatterns`](#optionsignorepatterns)
* [`options.ignoreFrom`](#optionsignorefrom)
* [`options.silentlyIgnore`](#optionssilentlyignore)
* [`options.plugins`](#optionsplugins)
* [`options.pluginPrefix`](#optionspluginprefix)
Expand Down Expand Up @@ -839,6 +840,18 @@ Name of [ignore file][ignore] to load.
If given and [`detectIgnore`][detect-ignore] is not `false`, `$ignoreName` files
are loaded.

The patterns in found ignore file are resolved based on the file’s directory.
If we had an ignore file `folder/.remarkignore`, with the value: `index.txt`,
and our file system looked as follows:

```txt
folder/.remarkignore
folder/index.txt
index.txt
```

Then `folder/index.txt` would be ignored but `index.txt` would not be.

* Type: `string`, optional

###### Example
Expand Down Expand Up @@ -904,6 +917,18 @@ function done(error) {
File path to [ignore file][ignore] to load, regardless of
[`detectIgnore`][detect-ignore] or [`ignoreName`][ignore-name].

The patterns in the ignore file are resolved based on the file’s directory.
If we had an ignore file `folder/ignore`, with the value: `index.txt`, and our
file system looked as follows:

```txt
folder/ignore
folder/index.txt
index.txt
```

Then `folder/index.txt` would be ignored but `index.txt` would not be.

* Type: `string`, optional

###### Example
Expand Down Expand Up @@ -960,6 +985,50 @@ function done(error) {
}
```

## `options.ignoreFrom`

File path to a directory where to resolve [`ignorePath`][ignore-path] and
[`ignorePatterns`][ignore-patterns] from.

If we had an ignore file `config/ignore`, with the value: `index.txt`, and our
file system looked as follows:

```txt
config/ignore
folder/index.txt
index.txt
```

Normally, both `index.txt` files would not be ignored, but when given
`ignoreFrom: '.'`, both would be.

* Type: `string`, optional

###### Example

The following example processes files in the current working directory with an
`md` extension and takes a reusable configuration file from a dependency.

```js
var engine = require('unified-engine')
var remark = require('remark')

engine(
{
processor: remark(),
files: ['.'],
extensions: ['md'],
ignorePath: 'node_modules/my-config/my-ignore',
ignoreFrom: '.'
},
done
)

function done(error) {
if (error) throw error
}
```

## `options.silentlyIgnore`

Skip given [`files`][files] which are ignored by [ignore files][ignore], instead
Expand Down Expand Up @@ -1407,6 +1476,10 @@ function done(error, code) {

[ignore-name]: #optionsignorename

[ignore-path]: #optionsignorepath

[ignore-patterns]: #optionsignorepatterns

[quiet]: #optionsquiet

[silent]: #optionssilent
Expand Down
4 changes: 3 additions & 1 deletion lib/file-set-pipeline/file-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ function fileSystem(context, settings, next) {
extensions: settings.extensions,
silentlyIgnore: settings.silentlyIgnore,
ignorePatterns: settings.ignorePatterns,
ignoreFrom: settings.ignoreFrom,
ignore: new Ignore({
cwd: settings.cwd,
detectIgnore: settings.detectIgnore,
ignoreName: settings.ignoreName,
ignorePath: settings.ignorePath
ignorePath: settings.ignorePath,
ignoreFrom: settings.ignoreFrom
})
},
onfound
Expand Down
18 changes: 15 additions & 3 deletions lib/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var vfile = require('to-vfile')

var readdir = fs.readdir
var stat = fs.stat
var sep = path.sep
var join = path.join
var relative = path.relative
var resolve = path.resolve
Expand Down Expand Up @@ -224,8 +225,12 @@ function search(input, options, next) {
function statAndIgnore(file, options, callback) {
var ignore = options.ignore
var extraIgnore = options.extraIgnore
var fp = resolve(options.cwd, filePath(file))
var normal = relative(options.cwd, fp)
var cwd = options.cwd
var fp = resolve(cwd, filePath(file))
var normal = relative(
resolve(cwd, options.ignoreFrom || '.'),
resolve(cwd, fp)
)
var expected = 1
var actual = 0
var stats
Expand Down Expand Up @@ -257,7 +262,14 @@ function statAndIgnore(file, options, callback) {
} else if (actual === expected) {
callback(null, {
stats: stats,
ignored: ignored || (normal ? extraIgnore.ignores(normal) : false)
ignored:
ignored ||
(normal === '' ||
normal === '..' ||
normal.charAt(0) === sep ||
normal.slice(0, 3) === '..' + sep
? false
: extraIgnore.ignores(normal))
})
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var resolve = path.resolve

function Ignore(options) {
this.cwd = options.cwd
this.ignoreFrom = options.ignoreFrom

this.findUp = new FindUp({
filePath: options.ignorePath,
Expand All @@ -36,7 +37,10 @@ function check(filePath, callback) {
if (error) {
callback(error)
} else if (ignore) {
normal = relative(ignore.filePath, resolve(self.cwd, filePath))
normal = relative(
resolve(self.cwd, self.ignoreFrom || ignore.filePath),
resolve(self.cwd, filePath)
)

if (
normal === '' ||
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function run(options, callback) {
settings.ignoreName = options.ignoreName || null
settings.ignorePath = options.ignorePath || null
settings.ignorePatterns = options.ignorePatterns || []
settings.ignoreFrom = options.ignoreFrom || null
settings.silentlyIgnore = Boolean(options.silentlyIgnore)

if (detectIgnore && !hasIgnore) {
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ done.
— Filepath to an ignore file to load
* [`ignorePatterns`][ignore-patterns] (`Array.<string>`, optional)
— Patterns to ignore in addition to ignore files, if any
* [`ignoreFrom`][ignore-from] (`string`, optional)
— Path to directory to resolve ignore patterns from
* [`silentlyIgnore`][silently-ignore] (`boolean`, default: `false`)
— Skip given files if they are ignored
* [`plugins`][options-plugins] (`Array|Object`, optional)
Expand Down Expand Up @@ -286,6 +288,8 @@ abide by its terms.

[ignore-patterns]: doc/options.md#optionsignorepatterns

[ignore-from]: doc/options.md#optionsignorefrom

[silently-ignore]: doc/options.md#optionssilentlyignore

[plugin-prefix]: doc/options.md#optionspluginprefix
Expand Down
37 changes: 35 additions & 2 deletions test/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var join = path.join
var fixtures = join(__dirname, 'fixtures')

test('ignore', function (t) {
t.plan(8)
t.plan(9)

t.test('should fail fatally when given ignores are not found', function (t) {
var cwd = join(fixtures, 'simple-structure')
Expand Down Expand Up @@ -223,7 +223,6 @@ test('ignore', function (t) {
files: ['.'],
ignorePath: join('deep', 'ignore'),
ignorePatterns: ['files/two.txt'],
ignoreFrom: '.',
extensions: ['txt']
},
onrun
Expand All @@ -244,4 +243,38 @@ test('ignore', function (t) {
}
}
)

t.test('`ignoreFrom`', function (st) {
var stderr = spy()

st.plan(1)

engine(
{
processor: noop,
cwd: join(fixtures, 'sibling-ignore'),
streamError: stderr.stream,
files: ['.'],
ignorePath: join('deep', 'ignore'),
ignorePatterns: ['files/two.txt'],
ignoreFrom: '.',
extensions: ['txt']
},
onrun
)

function onrun(error, code) {
var expected = [
'deep/files/one.txt: no issues found',
'deep/files/two.txt: no issues found',
''
].join('\n')

st.deepEqual(
[error, code, stderr()],
[null, 0, expected],
'should report'
)
}
})
})

0 comments on commit e062590

Please sign in to comment.