Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: drop glob usage #1742

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"dependencies": {
"commenting": "~1.1.0",
"glob": "~7.2.0",
"fdir": "6.1.1",
"lodash": "~4.17.21",
"magic-string": "~0.30.0",
"mkdirp": "~3.0.0",
Expand Down
61 changes: 24 additions & 37 deletions src/read-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@
import path from 'path';
import fs from 'fs';
import _ from 'lodash';
import glob from 'glob';
import { fdir } from 'fdir';

const pathsMatch = (target) => {
const targetLower = target.toLowerCase();

return (p) => {
const pLower = p.toLowerCase();
return pLower === targetLower ||
pLower.slice(0, pLower.lastIndexOf('.')) === targetLower;
};
};
/**
* Find file and returns its content if file exists.
*
Expand All @@ -37,49 +46,27 @@ import glob from 'glob';
*/
export function readFile(dir, cwd, names) {
const inputs = _.castArray(names);
// eslint-disable-next-line new-cap
const finder = new fdir();

for (let i = 0; i < inputs.length; ++i) {
const input = generatePattern(inputs[i]);
const input = inputs[i];
const absolutePath = path.join(dir, input);
const relativeToCwd = path.relative(cwd, absolutePath);

const findings = glob.sync(relativeToCwd, { cwd });
for (let j = 0; j < findings.length; ++j) {
const file = path.join(cwd, findings[j]);
if (isFile(file)) {
return fs.readFileSync(file, 'utf-8');
}
}
}

return null;
}

/**
* Check that given file exists, and is a real file.
*
* @param {string} file File path.
* @returns {boolean} `true` if `file` is a file, `false` otherwise.
*/
function isFile(file) {
return !!fs.existsSync(file) && !!fs.lstatSync(file).isFile();
}
const findings = finder
.withRelativePaths()
.filter(pathsMatch(relativeToCwd))
.crawl(cwd)
.sync();

/**
* Generate glob pattern for given input.
*
* @param {string} input Given input.
* @returns {string} Glob pattern.
*/
function generatePattern(input) {
let pattern = '';
const firstPath = findings[0];

for (let i = 0; i < input.length; ++i) {
const c = input[i];
const up = c.toUpperCase();
const low = c.toLowerCase();
pattern += up !== low ? `[${low}${up}]` : low;
if (firstPath) {
const file = path.join(cwd, firstPath);
return fs.readFileSync(file, 'utf-8');
}
}

return `${pattern}*`;
return null;
}