Skip to content

Commit a555efb

Browse files
committed
feat: added guessFile "exclude" option
1 parent 3f12626 commit a555efb

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Drop in replacement for [fs](https://nodejs.org/api/fs.html).
1919

2020
## Functions
2121

22-
### guessFile(filepath)
22+
### guessFile(filepath, { exclude = [] })
2323

2424
Extends and returns filepath with the file extension as appropriate. Returns `null` if no good match was found.
2525

@@ -28,6 +28,8 @@ A few notes:
2828
- Will prefer to match the exact file
2929
- Will return `null` when multiple possible extensions are found
3030

31+
To exclude certain extensions from being matched provide them in the `exclude` option.
32+
3133
### walkDir(dirpath)
3234

3335
Iteratively walk dirpath and return relative paths of all files contained.

src/logic/guess-file.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
const assert = require('assert');
12
const fs = require('fs');
23
const path = require('path');
34

4-
module.exports = (filepath) => {
5+
module.exports = (filepath, options = {}) => {
6+
assert(typeof filepath === 'string');
7+
assert(options instanceof Object && !Array.isArray(options));
8+
9+
const ctx = {
10+
exclude: [],
11+
...options
12+
};
13+
assert(Array.isArray(ctx.exclude));
14+
515
const dirname = path.dirname(filepath);
616
const basename = path.basename(filepath);
717
if (!fs.existsSync(dirname) || !fs.lstatSync(dirname).isDirectory()) {
818
return null;
919
}
1020
const relevantFiles = fs
1121
.readdirSync(dirname)
12-
.filter((f) => f === basename || (f.startsWith(`${basename}.`) && f.lastIndexOf('.') <= basename.length));
22+
.filter((f) => f === basename || (f.startsWith(`${basename}.`) && f.lastIndexOf('.') <= basename.length))
23+
.filter((f) => !ctx.exclude.includes(f.slice(f.lastIndexOf('.') + 1)));
1324
if (relevantFiles.includes(basename)) {
1425
return filepath;
1526
}

test/logic/guess-file.spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ describe('Testing guessFile', { useTmpDir: true }, () => {
1010
tmpDir = dir;
1111
});
1212

13-
const executeTest = (files, input, expected) => {
13+
const executeTest = (files, input, expected, opts = undefined) => {
1414
files.forEach((f) => fs.writeFileSync(path.join(tmpDir, f), ''));
1515
const filepath = path.join(tmpDir, input);
16-
expect(guessFile(filepath)).to.equal(typeof expected === 'string' ? path.join(tmpDir, expected) : expected);
16+
expect(guessFile(filepath, opts)).to.equal(typeof expected === 'string' ? path.join(tmpDir, expected) : expected);
1717
};
1818

1919
it('Testing exact multi-match', () => {
@@ -35,6 +35,10 @@ describe('Testing guessFile', { useTmpDir: true }, () => {
3535
executeTest(['file.json', 'file.yml'], 'file', null);
3636
});
3737

38+
it('Testing partial multi but exclude has match', () => {
39+
executeTest(['file.json', 'file.yml'], 'file', 'file.json', { exclude: ['yml'] });
40+
});
41+
3842
it('Testing double extension', () => {
3943
executeTest(['file.json.json'], 'file', null);
4044
executeTest(['file..json'], 'file', null);

0 commit comments

Comments
 (0)