Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into perf-shallow-giti…
Browse files Browse the repository at this point in the history
…gnore
  • Loading branch information
marionebl committed Jan 28, 2018
2 parents fcc95cf + c8574ae commit cef234d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 100 deletions.
8 changes: 3 additions & 5 deletions gitignore.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
'use strict';
const fs = require('fs');
const path = require('path');
const findUp = require('find-up');
const glob = require('glob');
const gitIgnore = require('ignore');
const multimatch = require('multimatch');
const pify = require('pify');
const slash = require('slash');

const globP = pify(glob);
const readFileP = pify(fs.readFile);

const mapGitIgnorePatternTo = base => ignore => {
Expand Down Expand Up @@ -65,6 +62,7 @@ const getFileSync = (file, cwd) => {
};

const normalizeOpts = opts => {
opts = opts || {};
const ignore = opts.ignore || [];
const cwd = opts.cwd || process.cwd();
return {ignore, cwd};
Expand All @@ -76,7 +74,7 @@ module.exports = o => {
const opts = normalizeOpts(o);
const rootIgnore = path.join(opts.cwd, '.gitignore');

if (opts.ignore.length > 0 && multimatch(rootIgnore, opts.ignore)) {
if (opts.ignore.length > 0 && multimatch(rootIgnore, opts.ignore).length > 0) {
return Promise.resolve(PASS_THROUGH);
}

Expand All @@ -93,7 +91,7 @@ module.exports.sync = o => {
const opts = normalizeOpts(o);
const rootIgnore = path.join(opts.cwd, '.gitignore');

if (opts.ignore.length > 0 && multimatch(rootIgnore, opts.ignore)) {
if (opts.ignore.length > 0 && multimatch(rootIgnore, opts.ignore).length > 0) {
return PASS_THROUGH;
}

Expand Down
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ module.exports.sync = (patterns, opts) => {
DEFAULT_FILTER;
};

const tasks = globTasks.reduce(
(tasks, task) => arrayUnion(getPattern(task, dirGlob.sync).map(glob => ({
const tasks = globTasks.reduce((tasks, task) => {
const newTask = getPattern(task, dirGlob.sync).map(glob => ({
pattern: glob,
opts: task.opts
}))),
[]
);
}));
return tasks.concat(newTask);
}, []);

const filter = getFilter();

Expand All @@ -130,3 +130,5 @@ module.exports.generateGlobTasks = generateGlobTasks;
module.exports.hasMagic = (patterns, opts) => []
.concat(patterns)
.some(pattern => glob.hasMagic(pattern, opts));

module.exports.gitignore = gitignore;
147 changes: 73 additions & 74 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,76 +1,75 @@
{
"name": "globby",
"version": "7.0.0",
"description": "Extends `glob` with support for multiple patterns and exposes a Promise API",
"license": "MIT",
"repository": "sindresorhus/globby",
"author": {
"email": "sindresorhus@gmail.com",
"name": "Sindre Sorhus",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"bench": "npm update glob-stream fast-glob && matcha bench.js",
"test": "xo && ava"
},
"files": [
"index.js",
"gitignore.js"
],
"keywords": [
"all",
"array",
"directories",
"dirs",
"expand",
"files",
"filesystem",
"filter",
"find",
"fnmatch",
"folders",
"fs",
"glob",
"globbing",
"globs",
"gulpfriendly",
"match",
"matcher",
"minimatch",
"multi",
"multiple",
"paths",
"pattern",
"patterns",
"traverse",
"util",
"utility",
"wildcard",
"wildcards",
"promise",
"gitignore",
"git"
],
"dependencies": {
"array-union": "^1.0.1",
"dir-glob": "^2.0.0",
"find-up": "^2.1.0",
"glob": "^7.1.2",
"ignore": "^3.3.5",
"multimatch": "^2.1.0",
"pify": "^3.0.0",
"slash": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"fast-glob": "^1.0.1",
"glob-stream": "^6.1.0",
"globby": "sindresorhus/globby#master",
"matcha": "^0.7.0",
"rimraf": "^2.2.8",
"xo": "^0.18.0"
}
"name": "globby",
"version": "7.1.1",
"description": "Extends `glob` with support for multiple patterns and exposes a Promise API",
"license": "MIT",
"repository": "sindresorhus/globby",
"author": {
"email": "sindresorhus@gmail.com",
"name": "Sindre Sorhus",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"bench": "npm update glob-stream fast-glob && matcha bench.js",
"test": "xo && ava"
},
"files": [
"index.js",
"gitignore.js"
],
"keywords": [
"all",
"array",
"directories",
"dirs",
"expand",
"files",
"filesystem",
"filter",
"find",
"fnmatch",
"folders",
"fs",
"glob",
"globbing",
"globs",
"gulpfriendly",
"match",
"matcher",
"minimatch",
"multi",
"multiple",
"paths",
"pattern",
"patterns",
"traverse",
"util",
"utility",
"wildcard",
"wildcards",
"promise",
"gitignore",
"git"
],
"dependencies": {
"array-union": "^1.0.1",
"dir-glob": "^2.0.0",
"glob": "^7.1.2",
"ignore": "^3.3.5",
"multimatch": "^2.1.0",
"pify": "^3.0.0",
"slash": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"fast-glob": "^2.0.1",
"glob-stream": "^6.1.0",
"globby": "sindresorhus/globby#master",
"matcha": "^0.7.0",
"rimraf": "^2.2.8",
"xo": "^0.18.0"
}
}
54 changes: 38 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ const globby = require('globby');

Returns a `Promise<Array>` of matching paths.

### globby.sync(patterns, [options])

Returns an `Array` of matching paths.

### globby.generateGlobTasks(patterns, [options])

Returns an `Array<Object>` in the format `{pattern: string, opts: Object}`, which can be passed as arguments to [`node-glob`](https://github.com/isaacs/node-glob). This is useful for other globbing-related packages.

Note that you should avoid running the same tasks multiple times as they contain a file system cache. Instead, run this method each time to ensure file system changes are taken into consideration.

### globby.hasMagic(patterns, [options])

Returns a `boolean` of whether there are any special glob characters in the `patterns`.

Note that the options affect the results. If `noext: true` is set, then `+(a|b)` will not be considered a magic pattern. If the pattern has a brace expansion, like `a/{b/c,x/y}`, then that is considered magical, unless `nobrace: true` is set.

#### patterns

Type: `string` `Array`
Expand Down Expand Up @@ -107,6 +91,44 @@ Default: `false`

Respect ignore patterns in `.gitignore` files that apply to the globbed files.

### globby.sync(patterns, [options])

Returns an `Array` of matching paths.

### globby.generateGlobTasks(patterns, [options])

Returns an `Array<Object>` in the format `{pattern: string, opts: Object}`, which can be passed as arguments to [`node-glob`](https://github.com/isaacs/node-glob). This is useful for other globbing-related packages.

Note that you should avoid running the same tasks multiple times as they contain a file system cache. Instead, run this method each time to ensure file system changes are taken into consideration.

### globby.hasMagic(patterns, [options])

Returns a `boolean` of whether there are any special glob characters in the `patterns`.

Note that the options affect the results. If `noext: true` is set, then `+(a|b)` will not be considered a magic pattern. If the pattern has a brace expansion, like `a/{b/c,x/y}`, then that is considered magical, unless `nobrace: true` is set.

### globby.gitignore([options])

Returns a `Promise<(path: string) => boolean>` indicating whether a given path is ignored via a `.gitignore` file.

Takes `cwd?: string` and `ignore?: string[]` as options. `.gitignore` files matched by the ignore config are not
used for the resulting filter function.

```js
const {gitignore} = require('globby');

(async () => {
const isIgnored = await gitignore();
console.log(isIgnored('some/file'));
})();
```

### globby.gitignore.sync([options])

Returns a `(path: string) => boolean` indicating whether a given path is ignored via a `.gitignore` file.

Takes the same options as `globby.gitignore`.


## Globbing patterns

Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ test('glob - async', async t => {
t.deepEqual(await m('*.tmp'), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
});

test('glob - async - multiple file paths', t => {
t.deepEqual(m.sync(['a.tmp', 'b.tmp']), ['a.tmp', 'b.tmp']);
});

test('glob with multiple patterns - async', async t => {
t.deepEqual(await m(['a.tmp', '*.tmp', '!{c,d,e}.tmp']), ['a.tmp', 'b.tmp']);
});
Expand All @@ -44,6 +48,10 @@ test('glob - sync', t => {
t.deepEqual(m.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
});

test('glob - sync - multiple file paths', t => {
t.deepEqual(m.sync(['a.tmp', 'b.tmp']), ['a.tmp', 'b.tmp']);
});

test('return [] for all negative patterns - sync', t => {
t.deepEqual(m.sync(['!a.tmp', '!b.tmp']), []);
});
Expand Down Expand Up @@ -101,6 +109,11 @@ test('expandDirectories option', t => {
}), ['tmp/a.tmp']);
});

test('expandDirectories:true and nodir:true option', t => {
t.deepEqual(m.sync('tmp', {nodir: true}), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
t.deepEqual(m.sync('tmp', {nodir: false}), ['tmp', 'tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
});

// Rejected for being an invalid pattern
[
{},
Expand Down

0 comments on commit cef234d

Please sign in to comment.