Skip to content

Commit

Permalink
Made minimatch an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Nov 21, 2024
1 parent c590dcf commit d1b63a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@
"@types/readable-stream": "^4.0.10",
"buffer": "^6.0.3",
"eventemitter3": "^5.0.1",
"minimatch": "^9.0.3",
"readable-stream": "^4.5.2",
"utilium": "^1.0.0"
},
"optionalDependencies": {
"minimatch": "^9.0.3"
},
"devDependencies": {
"@eslint/js": "^9.8.0",
"@types/eslint__js": "^8.42.3",
Expand Down
31 changes: 24 additions & 7 deletions scripts/make-index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node
import { readdirSync, statSync, writeFileSync } from 'node:fs';
import { minimatch } from 'minimatch';
import { join, relative, resolve } from 'node:path/posix';
import _path from 'node:path/posix';
import { parseArgs } from 'node:util';

const { values: options, positionals } = parseArgs({
Expand Down Expand Up @@ -37,6 +36,24 @@ if (options.quiet && options.verbose) {
process.exit();
}

let matchesGlob = _path.matchesGlob;

if (matchesGlob && options.verbose) {
console.debug('[debug] path.matchesGlob is available.');
}

if (!matchesGlob) {
console.warn('Warning: path.matchesGlob is not available, falling back to minimatch. (Node 20.17.0+ or 22.5.0+ needed)');

try {
const { minimatch } = await import('minimatch');
matchesGlob = minimatch;
} catch {
console.error('Fatal error: Failed to fall back to minimatch (is it installed?)');
process.exit(1);
}
}

function fixSlash(path) {
return path.replaceAll('\\', '/');
}
Expand Down Expand Up @@ -71,25 +88,25 @@ const entries = new Map();

function computeEntries(path) {
try {
if (options.ignore.some(pattern => minimatch(path, pattern))) {
if (options.ignore.some(pattern => matchesGlob(path, pattern))) {
if (!options.quiet) console.log(`${color('yellow', 'skip')} ${path}`);
return;
}

const stats = statSync(path);

if (stats.isFile()) {
entries.set('/' + relative(resolvedRoot, path), stats);
entries.set('/' + _path.relative(resolvedRoot, path), stats);
if (options.verbose) {
console.log(`${color('green', 'file')} ${path}`);
}
return;
}

for (const file of readdirSync(path)) {
computeEntries(join(path, file));
computeEntries(_path.join(path, file));
}
entries.set('/' + relative(resolvedRoot, path), stats);
entries.set('/' + _path.relative(resolvedRoot, path), stats);
if (options.verbose) {
console.log(`${color('bright_green', ' dir')} ${path}`);
}
Expand All @@ -102,7 +119,7 @@ function computeEntries(path) {

computeEntries(resolvedRoot);
if (!options.quiet) {
console.log('Generated listing for ' + fixSlash(resolve(root)));
console.log('Generated listing for ' + fixSlash(_path.resolve(root)));
}

const index = {
Expand Down

0 comments on commit d1b63a3

Please sign in to comment.