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

refactor: switch to fast-glob #1153

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions README.md
Expand Up @@ -854,16 +854,6 @@ rm -rf foo.txt bar.txt
exec echo hello
```

### config.globOptions

Example:

```javascript
config.globOptions = {nodir: true};
```

Use this value for calls to `glob.sync()` instead of the default options.

### config.reset()

Example:
Expand All @@ -882,7 +872,6 @@ Reset `shell.config` to the defaults:
```javascript
{
fatal: false,
globOptions: {},
maxdepth: 255,
noglob: false,
silent: false,
Expand Down
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
12 changes: 0 additions & 12 deletions shell.js
Expand Up @@ -135,17 +135,6 @@ exports.config = common.config;
//@ exec echo hello
//@ ```

//@
//@ ### config.globOptions
//@
//@ Example:
//@
//@ ```javascript
//@ config.globOptions = {nodir: true};
//@ ```
//@
//@ Use this value for calls to `glob.sync()` instead of the default options.

//@
//@ ### config.reset()
//@
Expand All @@ -165,7 +154,6 @@ exports.config = common.config;
//@ ```javascript
//@ {
//@ fatal: false,
//@ globOptions: {},
//@ maxdepth: 255,
//@ noglob: false,
//@ silent: false,
Expand Down
10 changes: 7 additions & 3 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 All @@ -19,7 +19,6 @@ var isElectron = Boolean(process.versions.electron);
// Module globals (assume no execPath by default)
var DEFAULT_CONFIG = {
fatal: false,
globOptions: {},
maxdepth: 255,
noglob: false,
silent: false,
Expand Down Expand Up @@ -263,7 +262,12 @@ function expand(list) {
} else {
var ret;
try {
ret = glob.sync(listEl, config.globOptions);
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
15 changes: 1 addition & 14 deletions test/config.js
Expand Up @@ -48,7 +48,7 @@ test.cb('config.fatal = true', t => {
});

//
// config.globOptions
// Default glob expansion behavior
//

test('Expands to directories by default', t => {
Expand All @@ -60,16 +60,3 @@ test('Expands to directories by default', t => {
t.truthy(result.indexOf('test/resources/head') > -1);
t.truthy(result.indexOf('test/resources/external') > -1);
});

test(
'Check to make sure options get passed through (nodir is an example)',
t => {
shell.config.globOptions = { nodir: true };
const result = common.expand(['test/resources/*a*']);
t.is(result.length, 2);
t.truthy(result.indexOf('test/resources/a.txt') > -1);
t.truthy(result.indexOf('test/resources/badlink') > -1);
t.truthy(result.indexOf('test/resources/cat') < 0);
t.truthy(result.indexOf('test/resources/external') < 0);
}
);