Skip to content

Commit 9f781ce

Browse files
committed
Require Node.js 8
1 parent 04d51bf commit 9f781ce

8 files changed

+125
-107
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
bench
21
node_modules
32
yarn.lock
3+
bench
44
*.tmp
55
tmp

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ node_js:
77
- '12'
88
- '10'
99
- '8'
10-
- '6'

bench.js

+25-13
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ const BENCH_DIR = 'bench';
1111

1212
const runners = [{
1313
name: 'globby async (working directory)',
14-
run: (patterns, cb) => {
15-
globby(patterns).then(cb.bind(null, null), cb);
14+
run: async (patterns, callback) => {
15+
await globby(patterns);
16+
callback();
1617
}
1718
}, {
1819
name: 'globby async (upstream/master)',
19-
run: (patterns, cb) => {
20-
globbyMaster(patterns).then(cb.bind(null, null), cb);
20+
run: async (patterns, callback) => {
21+
await globbyMaster(patterns);
22+
callback();
2123
}
2224
}, {
2325
name: 'globby sync (working directory)',
@@ -36,8 +38,9 @@ const runners = [{
3638
}
3739
}, {
3840
name: 'fast-glob async',
39-
run: (patterns, cb) => {
40-
fastGlob(patterns).then(cb.bind(null, null), cb);
41+
run: async (patterns, callback) => {
42+
await fastGlob(patterns);
43+
callback();
4144
}
4245
}, {
4346
name: 'fast-glob sync',
@@ -47,13 +50,22 @@ const runners = [{
4750
}];
4851
const benchs = [{
4952
name: 'negative globs (some files inside dir)',
50-
patterns: ['a/*', '!a/c*']
53+
patterns: [
54+
'a/*',
55+
'!a/c*'
56+
]
5157
}, {
5258
name: 'negative globs (whole dir)',
53-
patterns: ['a/*', '!a/**']
59+
patterns: [
60+
'a/*',
61+
'!a/**'
62+
]
5463
}, {
5564
name: 'multiple positive globs',
56-
patterns: ['a/*', 'b/*']
65+
patterns: [
66+
'a/*',
67+
'b/*'
68+
]
5769
}];
5870

5971
before(() => {
@@ -62,11 +74,11 @@ before(() => {
6274
fs.mkdirSync(BENCH_DIR);
6375
process.chdir(BENCH_DIR);
6476
['a', 'b']
65-
.map(dir => `${dir}/`)
66-
.forEach(dir => {
67-
fs.mkdirSync(dir);
77+
.map(directory => `${directory}/`)
78+
.forEach(directory => {
79+
fs.mkdirSync(directory);
6880
for (let i = 0; i < 500; i++) {
69-
fs.writeFileSync(dir + (i < 100 ? 'c' : 'd') + i, '');
81+
fs.writeFileSync(directory + (i < 100 ? 'c' : 'd') + i, '');
7082
}
7183
});
7284
});

gitignore.js

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
2+
const {promisify} = require('util');
23
const fs = require('fs');
34
const path = require('path');
45
const fastGlob = require('fast-glob');
56
const gitIgnore = require('ignore');
6-
const pify = require('pify');
77
const slash = require('slash');
88

99
const DEFAULT_IGNORE = [
@@ -14,7 +14,7 @@ const DEFAULT_IGNORE = [
1414
'**/.git'
1515
];
1616

17-
const readFileP = pify(fs.readFile);
17+
const readFileP = promisify(fs.readFile);
1818

1919
const mapGitIgnorePatternTo = base => ignore => {
2020
if (ignore.startsWith('!')) {
@@ -30,7 +30,7 @@ const parseGitIgnore = (content, options) => {
3030
return content
3131
.split(/\r?\n/)
3232
.filter(Boolean)
33-
.filter(line => line.charAt(0) !== '#')
33+
.filter(line => !line.startsWith('#'))
3434
.map(mapGitIgnorePatternTo(base));
3535
};
3636

@@ -60,43 +60,47 @@ const getIsIgnoredPredecate = (ignores, cwd) => {
6060
return p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, p))));
6161
};
6262

63-
const getFile = (file, cwd) => {
63+
const getFile = async (file, cwd) => {
6464
const filePath = path.join(cwd, file);
65-
return readFileP(filePath, 'utf8')
66-
.then(content => ({
67-
content,
68-
cwd,
69-
filePath
70-
}));
65+
const content = await readFileP(filePath, 'utf8');
66+
67+
return {
68+
cwd,
69+
filePath,
70+
content
71+
};
7172
};
7273

7374
const getFileSync = (file, cwd) => {
7475
const filePath = path.join(cwd, file);
7576
const content = fs.readFileSync(filePath, 'utf8');
7677

7778
return {
78-
content,
7979
cwd,
80-
filePath
80+
filePath,
81+
content
8182
};
8283
};
8384

84-
const normalizeOptions = (options = {}) => {
85-
const ignore = options.ignore || [];
86-
const cwd = options.cwd || process.cwd();
85+
const normalizeOptions = ({
86+
ignore = [],
87+
cwd = process.cwd()
88+
} = {}) => {
8789
return {ignore, cwd};
8890
};
8991

90-
module.exports = options => {
92+
module.exports = async options => {
9193
options = normalizeOptions(options);
9294

93-
return fastGlob('**/.gitignore', {
95+
const paths = await fastGlob('**/.gitignore', {
9496
ignore: DEFAULT_IGNORE.concat(options.ignore),
9597
cwd: options.cwd
96-
})
97-
.then(paths => Promise.all(paths.map(file => getFile(file, options.cwd))))
98-
.then(files => reduceIgnore(files))
99-
.then(ignores => getIsIgnoredPredecate(ignores, options.cwd));
98+
});
99+
100+
const files = await Promise.all(paths.map(file => getFile(file, options.cwd)));
101+
const ignores = reduceIgnore(files);
102+
103+
return getIsIgnoredPredecate(ignores, options.cwd);
100104
};
101105

102106
module.exports.sync = options => {
@@ -106,6 +110,7 @@ module.exports.sync = options => {
106110
ignore: DEFAULT_IGNORE.concat(options.ignore),
107111
cwd: options.cwd
108112
});
113+
109114
const files = paths.map(file => getFileSync(file, options.cwd));
110115
const ignores = reduceIgnore(files);
111116

index.d.ts

-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ declare const globby: {
138138
): boolean;
139139

140140
readonly gitignore: Gitignore;
141-
142-
// TODO: Remove this for the next major release
143-
default: typeof globby;
144141
};
145142

146143
export = globby;

index.js

+38-41
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const DEFAULT_FILTER = () => false;
1111
const isNegative = pattern => pattern[0] === '!';
1212

1313
const assertPatternsInput = patterns => {
14-
if (!patterns.every(x => typeof x === 'string')) {
14+
if (!patterns.every(pattern => typeof pattern === 'string')) {
1515
throw new TypeError('Patterns must be a string or an array of strings');
1616
}
1717
};
@@ -31,27 +31,29 @@ const generateGlobTasks = (patterns, taskOptions) => {
3131

3232
const globTasks = [];
3333

34-
taskOptions = Object.assign({
34+
taskOptions = {
3535
ignore: [],
36-
expandDirectories: true
37-
}, taskOptions);
36+
expandDirectories: true,
37+
...taskOptions
38+
};
3839

39-
patterns.forEach((pattern, i) => {
40+
for (const [index, pattern] of patterns.entries()) {
4041
if (isNegative(pattern)) {
41-
return;
42+
continue;
4243
}
4344

4445
const ignore = patterns
45-
.slice(i)
46+
.slice(index)
4647
.filter(isNegative)
4748
.map(pattern => pattern.slice(1));
4849

49-
const options = Object.assign({}, taskOptions, {
50+
const options = {
51+
...taskOptions,
5052
ignore: taskOptions.ignore.concat(ignore)
51-
});
53+
};
5254

5355
globTasks.push({pattern, options});
54-
});
56+
}
5557

5658
return globTasks;
5759
};
@@ -63,9 +65,15 @@ const globDirs = (task, fn) => {
6365
}
6466

6567
if (Array.isArray(task.options.expandDirectories)) {
66-
options = Object.assign(options, {files: task.options.expandDirectories});
68+
options = {
69+
...options,
70+
files: task.options.expandDirectories
71+
};
6772
} else if (typeof task.options.expandDirectories === 'object') {
68-
options = Object.assign(options, task.options.expandDirectories);
73+
options = {
74+
...options,
75+
...task.options.expandDirectories
76+
};
6977
}
7078

7179
return fn(task.pattern, options);
@@ -85,40 +93,29 @@ const globToTask = task => glob => {
8593
};
8694
};
8795

88-
const globby = (patterns, options) => {
89-
let globTasks;
96+
module.exports = async (patterns, options) => {
97+
const globTasks = generateGlobTasks(patterns, options);
9098

91-
try {
92-
globTasks = generateGlobTasks(patterns, options);
93-
} catch (error) {
94-
return Promise.reject(error);
95-
}
99+
const getFilter = async () => {
100+
return options && options.gitignore ?
101+
gitignore({cwd: options.cwd, ignore: options.ignore}) :
102+
DEFAULT_FILTER;
103+
};
96104

97-
const getTasks = Promise.all(globTasks.map(task => Promise.resolve(getPattern(task, dirGlob))
98-
.then(globs => Promise.all(globs.map(globToTask(task))))
99-
))
100-
.then(tasks => arrayUnion(...tasks));
105+
const getTasks = async () => {
106+
const tasks = await Promise.all(globTasks.map(async task => {
107+
const globs = await getPattern(task, dirGlob);
108+
return Promise.all(globs.map(globToTask(task)));
109+
}));
101110

102-
const getFilter = () => {
103-
return Promise.resolve(
104-
options && options.gitignore ?
105-
gitignore({cwd: options.cwd, ignore: options.ignore}) :
106-
DEFAULT_FILTER
107-
);
111+
return arrayUnion(...tasks);
108112
};
109113

110-
return getFilter()
111-
.then(filter => {
112-
return getTasks
113-
.then(tasks => Promise.all(tasks.map(task => fastGlob(task.pattern, task.options))))
114-
.then(paths => arrayUnion(...paths))
115-
.then(paths => paths.filter(p => !filter(getPathString(p))));
116-
});
117-
};
114+
const [filter, tasks] = await Promise.all([getFilter(), getTasks()]);
115+
const paths = await Promise.all(tasks.map(task => fastGlob(task.pattern, task.options)));
118116

119-
module.exports = globby;
120-
// TODO: Remove this for the next major release
121-
module.exports.default = globby;
117+
return arrayUnion(...paths).filter(path_ => !filter(getPathString(path_)));
118+
};
122119

123120
module.exports.sync = (patterns, options) => {
124121
const globTasks = generateGlobTasks(patterns, options);
@@ -138,7 +135,7 @@ module.exports.sync = (patterns, options) => {
138135
return tasks.reduce(
139136
(matches, task) => arrayUnion(matches, fastGlob.sync(task.pattern, task.options)),
140137
[]
141-
).filter(p => !filter(p));
138+
).filter(path_ => !filter(path_));
142139
};
143140

144141
module.exports.generateGlobTasks = generateGlobTasks;

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=6"
13+
"node": ">=8"
1414
},
1515
"scripts": {
1616
"bench": "npm update glob-stream fast-glob && matcha bench.js",
@@ -25,7 +25,6 @@
2525
"all",
2626
"array",
2727
"directories",
28-
"dirs",
2928
"expand",
3029
"files",
3130
"filesystem",
@@ -57,16 +56,15 @@
5756
],
5857
"dependencies": {
5958
"@types/glob": "^7.1.1",
60-
"array-union": "^1.0.2",
59+
"array-union": "^2.1.0",
6160
"dir-glob": "^2.2.2",
6261
"fast-glob": "^2.2.6",
6362
"glob": "^7.1.3",
6463
"ignore": "^5.1.1",
65-
"pify": "^4.0.1",
66-
"slash": "^2.0.0"
64+
"slash": "^3.0.0"
6765
},
6866
"devDependencies": {
69-
"ava": "^1.4.1",
67+
"ava": "^2.1.0",
7068
"glob-stream": "^6.1.0",
7169
"globby": "sindresorhus/globby#master",
7270
"matcha": "^0.7.0",

0 commit comments

Comments
 (0)