Skip to content

Commit

Permalink
feat: switch to fast-glob
Browse files Browse the repository at this point in the history
This removes `node-glob` in favor of `fast-glob`. The main motivation
for this is because `node-glob` has a security warning and I can't
update to `node-glob@9` unless we drop compatibility for node v8.

Switching to `fast-glob` seems to be fairly straightforward, although
some options need to be changed by default for bash compatibility.

Fixes #828
Fixes #1149
  • Loading branch information
nfischer committed Feb 18, 2024
1 parent 7cad93a commit 3015415
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"execa": "^1.0.0",
"glob": "^7.0.0",
"fast-glob": "^3.3.2",
"interpret": "^1.0.0",
"rechoir": "^0.6.2"
},
Expand Down
9 changes: 7 additions & 2 deletions src/common.js
Expand Up @@ -6,7 +6,7 @@

var os = require('os');
var fs = require('fs');
var glob = require('glob');
var glob = require('fast-glob');
var shell = require('..');

var shellMethods = Object.create(shell);
Expand Down Expand Up @@ -262,7 +262,12 @@ function expand(list) {
} else {
var ret;
try {
ret = glob.sync(listEl, {});
ret = glob.sync(listEl, {
// These options are just to make fast-glob be compatible with POSIX
// (bash) wildcard behavior.
onlyFiles: false,
followSymbolicLinks: false,
});
// if nothing matched, interpret the string literally
ret = ret.length > 0 ? ret : [listEl];
} catch (e) {
Expand Down
23 changes: 15 additions & 8 deletions src/ls.js
@@ -1,7 +1,7 @@
var path = require('path');
var fs = require('fs');
var common = require('./common');
var glob = require('glob');
var glob = require('fast-glob');

var globPatternRecursive = path.sep + '**';

Expand Down Expand Up @@ -105,13 +105,20 @@ function _ls(options, paths) {
if (stat.isDirectory() && !options.directory) {
if (options.recursive) {
// use glob, because it's simple
glob.sync(p + globPatternRecursive, { dot: options.all, follow: options.link })
.forEach(function (item) {
// Glob pattern returns the directory itself and needs to be filtered out.
if (path.relative(p, item)) {
pushFile(item, path.relative(p, item));
}
});
glob.sync(p + globPatternRecursive, {
// These options are just to make fast-glob be compatible with POSIX
// (bash) wildcard behavior.
onlyFiles: false,

// These options depend on the cmdOptions provided to ls.
dot: options.all,
followSymbolicLinks: options.link,
}).forEach(function (item) {
// Glob pattern returns the directory itself and needs to be filtered out.
if (path.relative(p, item)) {
pushFile(item, path.relative(p, item));
}
});
} else if (options.all) {
// use fs.readdirSync, because it's fast
fs.readdirSync(p).forEach(function (item) {
Expand Down

0 comments on commit 3015415

Please sign in to comment.