Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

globby

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

globby - npm Package Compare versions

Comparing version 9.2.0 to 10.0.0

stream-utils.js

62

gitignore.js
'use strict';
const {promisify} = require('util');
const fs = require('fs');

@@ -6,3 +7,2 @@ const path = require('path');

const gitIgnore = require('ignore');
const pify = require('pify');
const slash = require('slash');

@@ -12,3 +12,2 @@

'**/node_modules/**',
'**/bower_components/**',
'**/flow-typed/**',

@@ -19,3 +18,3 @@ '**/coverage/**',

const readFileP = pify(fs.readFile);
const readFileP = promisify(fs.readFile);

@@ -36,3 +35,3 @@ const mapGitIgnorePatternTo = base => ignore => {

.filter(Boolean)
.filter(line => line.charAt(0) !== '#')
.filter(line => !line.startsWith('#'))
.map(mapGitIgnorePatternTo(base));

@@ -51,14 +50,27 @@ };

const ensureAbsolutePathForCwd = (cwd, p) => {
if (path.isAbsolute(p)) {
if (p.startsWith(cwd)) {
return p;
}
throw new Error(`Path ${p} is not in cwd ${cwd}`);
}
return path.join(cwd, p);
};
const getIsIgnoredPredecate = (ignores, cwd) => {
return p => ignores.ignores(slash(path.relative(cwd, p)));
return p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, p))));
};
const getFile = (file, cwd) => {
const getFile = async (file, cwd) => {
const filePath = path.join(cwd, file);
return readFileP(filePath, 'utf8')
.then(content => ({
content,
cwd,
filePath
}));
const content = await readFileP(filePath, 'utf8');
return {
cwd,
filePath,
content
};
};

@@ -71,24 +83,27 @@

return {
content,
cwd,
filePath
filePath,
content
};
};
const normalizeOptions = (options = {}) => {
const ignore = options.ignore || [];
const cwd = options.cwd || process.cwd();
const normalizeOptions = ({
ignore = [],
cwd = process.cwd()
} = {}) => {
return {ignore, cwd};
};
module.exports = options => {
module.exports = async options => {
options = normalizeOptions(options);
return fastGlob('**/.gitignore', {
const paths = await fastGlob('**/.gitignore', {
ignore: DEFAULT_IGNORE.concat(options.ignore),
cwd: options.cwd
})
.then(paths => Promise.all(paths.map(file => getFile(file, options.cwd))))
.then(files => reduceIgnore(files))
.then(ignores => getIsIgnoredPredecate(ignores, options.cwd));
});
const files = await Promise.all(paths.map(file => getFile(file, options.cwd)));
const ignores = reduceIgnore(files);
return getIsIgnoredPredecate(ignores, options.cwd);
};

@@ -103,2 +118,3 @@

});
const files = paths.map(file => getFileSync(file, options.cwd));

@@ -105,0 +121,0 @@ const ignores = reduceIgnore(files);

@@ -8,3 +8,3 @@ import {IOptions as NodeGlobOptions} from 'glob';

| ReadonlyArray<string>
| {files: ReadonlyArray<string>; extensions: ReadonlyArray<string>};
| {files: readonly string[]; extensions: readonly string[]};

@@ -52,3 +52,3 @@ interface GlobbyOptions extends FastGlobOptions {

readonly cwd?: string;
readonly ignore?: ReadonlyArray<string>;
readonly ignore?: readonly string[];
}

@@ -63,3 +63,3 @@

@returns A `Promise` for a filter function indicating whether a given path is ignored via a `.gitignore` file.
@returns A filter function indicating whether a given path is ignored via a `.gitignore` file.

@@ -88,3 +88,3 @@ @example

@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
@returns A `Promise<Array>` of matching paths.
@returns The matching paths.

@@ -104,3 +104,3 @@ @example

(
patterns: string | ReadonlyArray<string>,
patterns: string | readonly string[],
options?: globby.GlobbyOptions

@@ -112,6 +112,6 @@ ): Promise<string[]>;

@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
@returns An `Array` of matching paths.
@returns The matching paths.
*/
sync(
patterns: string | ReadonlyArray<string>,
patterns: string | readonly string[],
options?: globby.GlobbyOptions

@@ -121,2 +121,23 @@ ): string[];

/**
@param patterns - See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
@returns The stream of matching paths.
@example
```
import globby = require('globby');
(async () => {
for await (const path of globby.stream('*.tmp')) {
console.log(path);
}
})();
```
*/
stream(
patterns: string | readonly string[],
options?: globby.GlobbyOptions
): NodeJS.ReadableStream;
/**
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.

@@ -126,6 +147,6 @@

@param options - See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones in this package.
@returns An `Array<Object>` in the format `{ pattern: string, options: Object }`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
@returns Object in the format `{ pattern: string, options: Object }`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
*/
generateGlobTasks(
patterns: string | ReadonlyArray<string>,
patterns: string | readonly string[],
options?: globby.GlobbyOptions

@@ -141,6 +162,6 @@ ): globby.GlobTask[];

@param options - See the [`node-glob` options](https://github.com/isaacs/node-glob#globhasmagicpattern-options).
@returns A boolean of whether there are any special glob characters in the `patterns`.
@returns Whether there are any special glob characters in the `patterns`.
*/
hasMagic(
patterns: string | ReadonlyArray<string>,
patterns: string | readonly string[],
options?: NodeGlobOptions

@@ -150,7 +171,4 @@ ): boolean;

readonly gitignore: Gitignore;
// TODO: Remove this for the next major release
default: typeof globby;
};
export = globby;
'use strict';
const fs = require('fs');
const arrayUnion = require('array-union');
const merge2 = require('merge2');
const glob = require('glob');

@@ -8,2 +9,3 @@ const fastGlob = require('fast-glob');

const gitignore = require('./gitignore');
const {FilterStream, UniqueStream} = require('./stream-utils');

@@ -15,3 +17,3 @@ const DEFAULT_FILTER = () => false;

const assertPatternsInput = patterns => {
if (!patterns.every(x => typeof x === 'string')) {
if (!patterns.every(pattern => typeof pattern === 'string')) {
throw new TypeError('Patterns must be a string or an array of strings');

@@ -27,2 +29,4 @@ }

const getPathString = p => p.stats instanceof fs.Stats ? p.path : p;
const generateGlobTasks = (patterns, taskOptions) => {

@@ -35,23 +39,25 @@ patterns = arrayUnion([].concat(patterns));

taskOptions = Object.assign({
taskOptions = {
ignore: [],
expandDirectories: true
}, taskOptions);
expandDirectories: true,
...taskOptions
};
patterns.forEach((pattern, i) => {
for (const [index, pattern] of patterns.entries()) {
if (isNegative(pattern)) {
return;
continue;
}
const ignore = patterns
.slice(i)
.slice(index)
.filter(isNegative)
.map(pattern => pattern.slice(1));
const options = Object.assign({}, taskOptions, {
const options = {
...taskOptions,
ignore: taskOptions.ignore.concat(ignore)
});
};
globTasks.push({pattern, options});
});
}

@@ -68,5 +74,11 @@ return globTasks;

if (Array.isArray(task.options.expandDirectories)) {
options = Object.assign(options, {files: task.options.expandDirectories});
options = {
...options,
files: task.options.expandDirectories
};
} else if (typeof task.options.expandDirectories === 'object') {
options = Object.assign(options, task.options.expandDirectories);
options = {
...options,
...task.options.expandDirectories
};
}

@@ -79,2 +91,8 @@

const getFilterSync = options => {
return options && options.gitignore ?
gitignore.sync({cwd: options.cwd, ignore: options.ignore}) :
DEFAULT_FILTER;
};
const globToTask = task => glob => {

@@ -92,46 +110,29 @@ const {options} = task;

const globby = (patterns, options) => {
let globTasks;
module.exports = async (patterns, options) => {
const globTasks = generateGlobTasks(patterns, options);
try {
globTasks = generateGlobTasks(patterns, options);
} catch (error) {
return Promise.reject(error);
}
const getFilter = async () => {
return options && options.gitignore ?
gitignore({cwd: options.cwd, ignore: options.ignore}) :
DEFAULT_FILTER;
};
const getTasks = Promise.all(globTasks.map(task => Promise.resolve(getPattern(task, dirGlob))
.then(globs => Promise.all(globs.map(globToTask(task))))
))
.then(tasks => arrayUnion(...tasks));
const getTasks = async () => {
const tasks = await Promise.all(globTasks.map(async task => {
const globs = await getPattern(task, dirGlob);
return Promise.all(globs.map(globToTask(task)));
}));
const getFilter = () => {
return Promise.resolve(
options && options.gitignore ?
gitignore({cwd: options.cwd, ignore: options.ignore}) :
DEFAULT_FILTER
);
return arrayUnion(...tasks);
};
return getFilter()
.then(filter => {
return getTasks
.then(tasks => Promise.all(tasks.map(task => fastGlob(task.pattern, task.options))))
.then(paths => arrayUnion(...paths))
.then(paths => paths.filter(p => !filter(p)));
});
const [filter, tasks] = await Promise.all([getFilter(), getTasks()]);
const paths = await Promise.all(tasks.map(task => fastGlob(task.pattern, task.options)));
return arrayUnion(...paths).filter(path_ => !filter(getPathString(path_)));
};
module.exports = globby;
// TODO: Remove this for the next major release
module.exports.default = globby;
module.exports.sync = (patterns, options) => {
const globTasks = generateGlobTasks(patterns, options);
const getFilter = () => {
return options && options.gitignore ?
gitignore.sync({cwd: options.cwd, ignore: options.ignore}) :
DEFAULT_FILTER;
};
const tasks = globTasks.reduce((tasks, task) => {

@@ -142,9 +143,27 @@ const newTask = getPattern(task, dirGlob.sync).map(globToTask(task));

const filter = getFilter();
const filter = getFilterSync(options);
return tasks.reduce(
(matches, task) => arrayUnion(matches, fastGlob.sync(task.pattern, task.options)),
[]
).filter(p => !filter(p));
).filter(path_ => !filter(path_));
};
module.exports.stream = (patterns, options) => {
const globTasks = generateGlobTasks(patterns, options);
const tasks = globTasks.reduce((tasks, task) => {
const newTask = getPattern(task, dirGlob.sync).map(globToTask(task));
return tasks.concat(newTask);
}, []);
const filter = getFilterSync(options);
const filterStream = new FilterStream(p => !filter(p));
const uniqueStream = new UniqueStream();
return merge2(tasks.map(task => fastGlob.stream(task.pattern, task.options)))
.pipe(filterStream)
.pipe(uniqueStream);
};
module.exports.generateGlobTasks = generateGlobTasks;

@@ -151,0 +170,0 @@

{
"name": "globby",
"version": "9.2.0",
"version": "10.0.0",
"description": "Extends `glob` with support for multiple patterns and exposes a Promise API",

@@ -13,3 +13,3 @@ "license": "MIT",

"engines": {
"node": ">=6"
"node": ">=8"
},

@@ -23,3 +23,4 @@ "scripts": {

"gitignore.js",
"index.d.ts"
"index.d.ts",
"stream-utils.js"
],

@@ -30,3 +31,2 @@ "keywords": [

"directories",
"dirs",
"expand",

@@ -63,12 +63,13 @@ "files",

"@types/glob": "^7.1.1",
"array-union": "^1.0.2",
"dir-glob": "^2.2.2",
"fast-glob": "^2.2.6",
"array-union": "^2.1.0",
"dir-glob": "^3.0.1",
"fast-glob": "^3.0.3",
"glob": "^7.1.3",
"ignore": "^4.0.3",
"pify": "^4.0.1",
"slash": "^2.0.0"
"ignore": "^5.1.1",
"merge2": "^1.2.3",
"slash": "^3.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"ava": "^2.1.0",
"get-stream": "^5.1.0",
"glob-stream": "^6.1.0",

@@ -78,3 +79,3 @@ "globby": "sindresorhus/globby#master",

"rimraf": "^2.6.3",
"tsd": "^0.7.1",
"tsd": "^0.7.3",
"xo": "^0.24.0"

@@ -81,0 +82,0 @@ },

@@ -46,9 +46,9 @@ # globby [![Build Status](https://travis-ci.org/sindresorhus/globby.svg?branch=master)](https://travis-ci.org/sindresorhus/globby)

### globby(patterns, [options])
### globby(patterns, options?)
Returns a `Promise<Array>` of matching paths.
Returns a `Promise<string[]>` of matching paths.
#### patterns
Type: `string` `Array`
Type: `string | string[]`

@@ -59,3 +59,3 @@ See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).

Type: `Object`
Type: `object`

@@ -66,8 +66,10 @@ See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-1) in addition to the ones below.

Type: `boolean` `Array` `Object`<br>
Type: `boolean | string[] | object`<br>
Default: `true`
If set to `true`, `globby` will automatically glob directories for you. If you define an `Array` it will only glob files that matches the patterns inside the `Array`. You can also define an `Object` with `files` and `extensions` like below:
If set to `true`, `globby` will automatically glob directories for you. If you define an `Array` it will only glob files that matches the patterns inside the `Array`. You can also define an `object` with `files` and `extensions` like below:
```js
const globby = require('globby');
(async () => {

@@ -95,13 +97,29 @@ const paths = await globby('images', {

### globby.sync(patterns, [options])
### globby.sync(patterns, options?)
Returns an `Array` of matching paths.
Returns `string[]` of matching paths.
### globby.generateGlobTasks(patterns, [options])
### globby.stream(patterns, options?)
Returns an `Array<Object>` in the format `{pattern: string, options: Object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
Returns a [`stream.Readable`](https://nodejs.org/api/stream.html#stream_readable_streams) of matching paths.
Since Node.js 10, [readable streams are iterable](https://nodejs.org/api/stream.html#stream_readable_symbol_asynciterator), so you can loop over glob matches in a [`for await...of` loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) like this:
```js
const globby = require('globby');
(async () => {
for await (const path of globby.stream('*.tmp')) {
console.log(path);
}
})();
```
### globby.generateGlobTasks(patterns, options?)
Returns an `object[]` in the format `{pattern: string, options: Object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-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])
### globby.hasMagic(patterns, options?)

@@ -114,3 +132,3 @@ Returns a `boolean` of whether there are any special glob characters in the `patterns`.

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

@@ -131,3 +149,3 @@ Returns a `Promise<(path: string) => boolean>` indicating whether a given path is ignored via a `.gitignore` file.

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

@@ -160,4 +178,12 @@ Returns a `(path: string) => boolean` indicating whether a given path is ignored via a `.gitignore` file.

## License
---
MIT © [Sindre Sorhus](https://sindresorhus.com)
<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-globby?utm_source=npm-globby&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc