Skip to content

Commit 5c32b4a

Browse files
authored
Require Node.js 12.20 and move to ESM (#181)
1 parent 94e192c commit 5c32b4a

File tree

11 files changed

+347
-334
lines changed

11 files changed

+347
-334
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
1616
os:
1717
- ubuntu-latest
1818
- macos-latest
1919
- windows-latest
2020
steps:
2121
- uses: actions/checkout@v2
22-
- uses: actions/setup-node@v1
22+
- uses: actions/setup-node@v2
2323
with:
2424
node-version: ${{ matrix.node-version }}
2525
- run: npm install

bench.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,100 @@
1-
'use strict';
21
/* global after, before, bench, suite */
3-
const fs = require('fs');
4-
const rimraf = require('rimraf');
5-
const globbyMainBranch = require('globby');
6-
const gs = require('glob-stream');
7-
const fastGlob = require('fast-glob');
8-
const globby = require('.');
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import {fileURLToPath} from 'node:url';
5+
import rimraf from 'rimraf';
6+
import globbyMainBranch from 'globby';
7+
import gs from 'glob-stream';
8+
import fastGlob from 'fast-glob';
9+
import {globby, globbySync} from './index.js';
910

11+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1012
const BENCH_DIR = 'bench';
1113

1214
const runners = [{
1315
name: 'globby async (working directory)',
1416
run: async (patterns, callback) => {
1517
await globby(patterns);
1618
callback();
17-
}
19+
},
1820
}, {
1921
name: 'globby async (upstream/main)',
2022
run: async (patterns, callback) => {
2123
await globbyMainBranch(patterns);
2224
callback();
23-
}
25+
},
2426
}, {
2527
name: 'globby sync (working directory)',
2628
run: patterns => {
27-
globby.sync(patterns);
28-
}
29+
globbySync(patterns);
30+
},
2931
}, {
3032
name: 'globby sync (upstream/main)',
3133
run: patterns => {
3234
globbyMainBranch.sync(patterns);
33-
}
35+
},
3436
}, {
3537
name: 'glob-stream',
3638
run: (patterns, cb) => {
3739
gs(patterns).on('data', () => {}).on('end', cb);
38-
}
40+
},
3941
}, {
4042
name: 'fast-glob async',
4143
run: async (patterns, callback) => {
4244
await fastGlob(patterns);
4345
callback();
44-
}
46+
},
4547
}, {
4648
name: 'fast-glob sync',
4749
run: patterns => {
4850
fastGlob.sync(patterns);
49-
}
51+
},
5052
}];
5153
const benchs = [{
5254
name: 'negative globs (some files inside dir)',
5355
patterns: [
5456
'a/*',
55-
'!a/c*'
56-
]
57+
'!a/c*',
58+
],
5759
}, {
5860
name: 'negative globs (whole dir)',
5961
patterns: [
6062
'a/*',
61-
'!a/**'
62-
]
63+
'!a/**',
64+
],
6365
}, {
6466
name: 'multiple positive globs',
6567
patterns: [
6668
'a/*',
67-
'b/*'
68-
]
69+
'b/*',
70+
],
6971
}];
7072

7173
before(() => {
7274
process.chdir(__dirname);
7375
rimraf.sync(BENCH_DIR);
7476
fs.mkdirSync(BENCH_DIR);
7577
process.chdir(BENCH_DIR);
76-
['a', 'b']
77-
.map(directory => `${directory}/`)
78-
.forEach(directory => {
79-
fs.mkdirSync(directory);
80-
for (let i = 0; i < 500; i++) {
81-
fs.writeFileSync(directory + (i < 100 ? 'c' : 'd') + i, '');
82-
}
83-
});
78+
const directories = ['a', 'b']
79+
.map(directory => `${directory}/`);
80+
81+
for (const directory of directories) {
82+
fs.mkdirSync(directory);
83+
for (let i = 0; i < 500; i++) {
84+
fs.writeFileSync(directory + (i < 100 ? 'c' : 'd') + i, '');
85+
}
86+
}
8487
});
8588

8689
after(() => {
8790
process.chdir(__dirname);
8891
rimraf.sync(BENCH_DIR);
8992
});
9093

91-
benchs.forEach(benchmark => {
94+
for (const benchmark of benchs) {
9295
suite(benchmark.name, () => {
93-
runners.forEach(runner => bench(runner.name, runner.run.bind(null, benchmark.patterns)));
96+
for (const runner of runners) {
97+
bench(runner.name, runner.run.bind(null, benchmark.patterns));
98+
}
9499
});
95-
});
100+
}

gitignore.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
'use strict';
2-
const {promisify} = require('util');
3-
const fs = require('fs');
4-
const path = require('path');
5-
const fastGlob = require('fast-glob');
6-
const gitIgnore = require('ignore');
7-
const slash = require('slash');
1+
import {promisify} from 'node:util';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import fastGlob from 'fast-glob';
5+
import gitIgnore from 'ignore';
6+
import slash from 'slash';
87

98
const DEFAULT_IGNORE = [
109
'**/node_modules/**',
1110
'**/flow-typed/**',
1211
'**/coverage/**',
13-
'**/.git'
12+
'**/.git',
1413
];
1514

1615
const readFileP = promisify(fs.readFile);
@@ -38,7 +37,7 @@ const reduceIgnore = files => {
3837
for (const file of files) {
3938
ignores.add(parseGitIgnore(file.content, {
4039
cwd: file.cwd,
41-
fileName: file.filePath
40+
fileName: file.filePath,
4241
}));
4342
}
4443

@@ -58,9 +57,7 @@ const ensureAbsolutePathForCwd = (cwd, p) => {
5857
return path.join(cwd, p);
5958
};
6059

61-
const getIsIgnoredPredecate = (ignores, cwd) => {
62-
return p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, p.path || p))));
63-
};
60+
const getIsIgnoredPredicate = (ignores, cwd) => p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, p.path || p))));
6461

6562
const getFile = async (file, cwd) => {
6663
const filePath = path.join(cwd, file);
@@ -69,7 +66,7 @@ const getFile = async (file, cwd) => {
6966
return {
7067
cwd,
7168
filePath,
72-
content
69+
content,
7370
};
7471
};
7572

@@ -80,41 +77,40 @@ const getFileSync = (file, cwd) => {
8077
return {
8178
cwd,
8279
filePath,
83-
content
80+
content,
8481
};
8582
};
8683

8784
const normalizeOptions = ({
8885
ignore = [],
89-
cwd = slash(process.cwd())
90-
} = {}) => {
91-
return {ignore, cwd};
92-
};
86+
cwd = slash(process.cwd()),
87+
} = {}) => ({ignore, cwd});
9388

94-
module.exports = async options => {
89+
export const isGitIgnored = async options => {
9590
options = normalizeOptions(options);
9691

9792
const paths = await fastGlob('**/.gitignore', {
9893
ignore: DEFAULT_IGNORE.concat(options.ignore),
99-
cwd: options.cwd
94+
cwd: options.cwd,
10095
});
10196

10297
const files = await Promise.all(paths.map(file => getFile(file, options.cwd)));
10398
const ignores = reduceIgnore(files);
10499

105-
return getIsIgnoredPredecate(ignores, options.cwd);
100+
return getIsIgnoredPredicate(ignores, options.cwd);
106101
};
107102

108-
module.exports.sync = options => {
103+
export const isGitIgnoredSync = options => {
109104
options = normalizeOptions(options);
110105

111106
const paths = fastGlob.sync('**/.gitignore', {
112107
ignore: DEFAULT_IGNORE.concat(options.ignore),
113-
cwd: options.cwd
108+
cwd: options.cwd,
114109
});
115110

116111
const files = paths.map(file => getFileSync(file, options.cwd));
117112
const ignores = reduceIgnore(files);
118113

119-
return getIsIgnoredPredecate(ignores, options.cwd);
114+
return getIsIgnoredPredicate(ignores, options.cwd);
120115
};
116+

gitignore.test.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
const path = require('path');
2-
const test = require('ava');
3-
const slash = require('slash');
4-
const gitignore = require('./gitignore');
1+
import path from 'node:path';
2+
import {fileURLToPath} from 'node:url';
3+
import test from 'ava';
4+
import slash from 'slash';
5+
import {isGitIgnored, isGitIgnoredSync} from './gitignore.js';
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
58

69
test('gitignore', async t => {
710
const cwd = path.join(__dirname, 'fixtures/gitignore');
8-
const isIgnored = await gitignore({cwd});
11+
const isIgnored = await isGitIgnored({cwd});
912
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
1013
const expected = ['bar.js'];
1114
t.deepEqual(actual, expected);
1215
});
1316

1417
test('gitignore - mixed path styles', async t => {
1518
const cwd = path.join(__dirname, 'fixtures/gitignore');
16-
const isIgnored = await gitignore({cwd});
19+
const isIgnored = await isGitIgnored({cwd});
1720
t.true(isIgnored(slash(path.resolve(cwd, 'foo.js'))));
1821
});
1922

2023
test('gitignore - os paths', async t => {
2124
const cwd = path.join(__dirname, 'fixtures/gitignore');
22-
const isIgnored = await gitignore({cwd});
25+
const isIgnored = await isGitIgnored({cwd});
2326
t.true(isIgnored(path.resolve(cwd, 'foo.js')));
2427
});
2528

2629
test('gitignore - sync', t => {
2730
const cwd = path.join(__dirname, 'fixtures/gitignore');
28-
const isIgnored = gitignore.sync({cwd});
31+
const isIgnored = isGitIgnoredSync({cwd});
2932
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
3033
const expected = ['bar.js'];
3134
t.deepEqual(actual, expected);
@@ -35,7 +38,7 @@ test('ignore ignored .gitignore', async t => {
3538
const cwd = path.join(__dirname, 'fixtures/gitignore');
3639
const ignore = ['**/.gitignore'];
3740

38-
const isIgnored = await gitignore({cwd, ignore});
41+
const isIgnored = await isGitIgnored({cwd, ignore});
3942
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
4043
const expected = ['foo.js', 'bar.js'];
4144
t.deepEqual(actual, expected);
@@ -45,37 +48,37 @@ test('ignore ignored .gitignore - sync', t => {
4548
const cwd = path.join(__dirname, 'fixtures/gitignore');
4649
const ignore = ['**/.gitignore'];
4750

48-
const isIgnored = gitignore.sync({cwd, ignore});
51+
const isIgnored = isGitIgnoredSync({cwd, ignore});
4952
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
5053
const expected = ['foo.js', 'bar.js'];
5154
t.deepEqual(actual, expected);
5255
});
5356

5457
test('negative gitignore', async t => {
5558
const cwd = path.join(__dirname, 'fixtures/negative');
56-
const isIgnored = await gitignore({cwd});
59+
const isIgnored = await isGitIgnored({cwd});
5760
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
5861
const expected = ['foo.js'];
5962
t.deepEqual(actual, expected);
6063
});
6164

6265
test('negative gitignore - sync', t => {
6366
const cwd = path.join(__dirname, 'fixtures/negative');
64-
const isIgnored = gitignore.sync({cwd});
67+
const isIgnored = isGitIgnoredSync({cwd});
6568
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
6669
const expected = ['foo.js'];
6770
t.deepEqual(actual, expected);
6871
});
6972

7073
test('multiple negation', async t => {
7174
const cwd = path.join(__dirname, 'fixtures/multiple-negation');
72-
const isIgnored = await gitignore({cwd});
75+
const isIgnored = await isGitIgnored({cwd});
7376

7477
const actual = [
7578
'!!!unicorn.js',
7679
'!!unicorn.js',
7780
'!unicorn.js',
78-
'unicorn.js'
81+
'unicorn.js',
7982
].filter(file => !isIgnored(file));
8083

8184
const expected = ['!!unicorn.js', '!unicorn.js'];
@@ -84,13 +87,13 @@ test('multiple negation', async t => {
8487

8588
test('multiple negation - sync', t => {
8689
const cwd = path.join(__dirname, 'fixtures/multiple-negation');
87-
const isIgnored = gitignore.sync({cwd});
90+
const isIgnored = isGitIgnoredSync({cwd});
8891

8992
const actual = [
9093
'!!!unicorn.js',
9194
'!!unicorn.js',
9295
'!unicorn.js',
93-
'unicorn.js'
96+
'unicorn.js',
9497
].filter(file => !isIgnored(file));
9598

9699
const expected = ['!!unicorn.js', '!unicorn.js'];

0 commit comments

Comments
 (0)