Skip to content

Commit 2623a1b

Browse files
authored
feat: drop glob usage (#1742)
Removes `glob` and uses fdir instead with a basic directory traversal (no globs). Reduces the complexity of the file reading logic and the install size (~3MB to 56KB).
1 parent c0abf2d commit 2623a1b

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"dependencies": {
4040
"commenting": "~1.1.0",
41-
"glob": "~7.2.0",
41+
"fdir": "6.1.1",
4242
"lodash": "~4.17.21",
4343
"magic-string": "~0.30.0",
4444
"moment": "~2.30.1",

src/read-file.js

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@
2525
import path from 'path';
2626
import fs from 'fs';
2727
import _ from 'lodash';
28-
import glob from 'glob';
28+
import { fdir } from 'fdir';
2929

30+
const pathsMatch = (target) => {
31+
const targetLower = target.toLowerCase();
32+
33+
return (p) => {
34+
const pLower = p.toLowerCase();
35+
return pLower === targetLower ||
36+
pLower.slice(0, pLower.lastIndexOf('.')) === targetLower;
37+
};
38+
};
3039
/**
3140
* Find file and returns its content if file exists.
3241
*
@@ -37,49 +46,27 @@ import glob from 'glob';
3746
*/
3847
export function readFile(dir, cwd, names) {
3948
const inputs = _.castArray(names);
49+
// eslint-disable-next-line new-cap
50+
const finder = new fdir();
4051

4152
for (let i = 0; i < inputs.length; ++i) {
42-
const input = generatePattern(inputs[i]);
53+
const input = inputs[i];
4354
const absolutePath = path.join(dir, input);
4455
const relativeToCwd = path.relative(cwd, absolutePath);
4556

46-
const findings = glob.sync(relativeToCwd, { cwd });
47-
for (let j = 0; j < findings.length; ++j) {
48-
const file = path.join(cwd, findings[j]);
49-
if (isFile(file)) {
50-
return fs.readFileSync(file, 'utf-8');
51-
}
52-
}
53-
}
54-
55-
return null;
56-
}
57-
58-
/**
59-
* Check that given file exists, and is a real file.
60-
*
61-
* @param {string} file File path.
62-
* @returns {boolean} `true` if `file` is a file, `false` otherwise.
63-
*/
64-
function isFile(file) {
65-
return !!fs.existsSync(file) && !!fs.lstatSync(file).isFile();
66-
}
57+
const findings = finder
58+
.withRelativePaths()
59+
.filter(pathsMatch(relativeToCwd))
60+
.crawl(cwd)
61+
.sync();
6762

68-
/**
69-
* Generate glob pattern for given input.
70-
*
71-
* @param {string} input Given input.
72-
* @returns {string} Glob pattern.
73-
*/
74-
function generatePattern(input) {
75-
let pattern = '';
63+
const firstPath = findings[0];
7664

77-
for (let i = 0; i < input.length; ++i) {
78-
const c = input[i];
79-
const up = c.toUpperCase();
80-
const low = c.toLowerCase();
81-
pattern += up !== low ? `[${low}${up}]` : low;
65+
if (firstPath) {
66+
const file = path.join(cwd, firstPath);
67+
return fs.readFileSync(file, 'utf-8');
68+
}
8269
}
8370

84-
return `${pattern}*`;
71+
return null;
8572
}

0 commit comments

Comments
 (0)