Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit87593b3

Browse files
committed
refactor: switch to fast-glob
This removes `node-glob` in favor of `fast-glob`. The main motivationfor this is because `node-glob` has a security warning and I can'tupdate to `node-glob@9` unless we drop compatibility for node v8.Switching to `fast-glob` seems to be fairly straightforward, althoughsome options need to be changed by default for bash compatibility.Fixes#828Fixes#1149
1 parentd9d7f4d commit87593b3

File tree

5 files changed

+78
-20
lines changed

5 files changed

+78
-20
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ shell.echo('hello world');
135135

136136
All commands run synchronously, unless otherwise stated.
137137
All commands accept standard bash globbing characters (`*`,`?`, etc.),
138-
compatible withthe[node`glob` module](https://github.com/isaacs/node-glob).
138+
compatible with[`fast-glob`](https://www.npmjs.com/package/fast-glob).
139139

140140
For less-commonly used commands and features, please check out our[wiki
141141
page](https://github.com/shelljs/shelljs/wiki).
@@ -868,7 +868,7 @@ config.globOptions = {nodir: true};
868868

869869
`config.globOptions` changes how ShellJS expands glob (wildcard)
870870
expressions. See
871-
[node-glob](https://github.com/isaacs/node-glob?tab=readme-ov-file#options)
871+
[fast-glob](https://github.com/mrmlnc/fast-glob?tab=readme-ov-file#options-3)
872872
for available options. Be aware that modifying`config.globOptions`**may
873873
break ShellJS functionality.**
874874

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
},
5656
"dependencies": {
5757
"execa":"^1.0.0",
58-
"glob":"^7.0.0",
58+
"fast-glob":"^3.3.2",
5959
"interpret":"^1.0.0",
6060
"rechoir":"^0.6.2"
6161
},

‎shell.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var common = require('./src/common');
1111
//@
1212
//@ All commands run synchronously, unless otherwise stated.
1313
//@ All commands accept standard bash globbing characters (`*`, `?`, etc.),
14-
//@ compatible withthe [node `glob` module](https://github.com/isaacs/node-glob).
14+
//@ compatible with[`fast-glob`](https://www.npmjs.com/package/fast-glob).
1515
//@
1616
//@ For less-commonly used commands and features, please check out our [wiki
1717
//@ page](https://github.com/shelljs/shelljs/wiki).
@@ -150,7 +150,7 @@ exports.config = common.config;
150150
//@
151151
//@ `config.globOptions` changes how ShellJS expands glob (wildcard)
152152
//@ expressions. See
153-
//@ [node-glob](https://github.com/isaacs/node-glob?tab=readme-ov-file#options)
153+
//@ [fast-glob](https://github.com/mrmlnc/fast-glob?tab=readme-ov-file#options-3)
154154
//@ for available options. Be aware that modifying `config.globOptions` **may
155155
//@ break ShellJS functionality.**
156156

‎src/common.js‎

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@
66

77
varos=require('os');
88
varfs=require('fs');
9-
varglob=require('glob');
9+
varglob=require('fast-glob');
1010
varshell=require('..');
1111

12+
// Suppress experimental warnings for fast-glob. This only happens on Node v11.
13+
varoriginalProcessEmit=process.emit;
14+
functionsuppressExperimentalWarnings(event,err){
15+
if(event==='warning'&&err.name==='ExperimentalWarning'){
16+
returnfalse;
17+
}else{
18+
returnoriginalProcessEmit.apply(process,arguments);
19+
}
20+
}
21+
varparts=process.versions.node.split('.').map(Number);
22+
varmajor=parts[0];
23+
if(major===11){
24+
process.emit=suppressExperimentalWarnings;
25+
}
26+
1227
varshellMethods=Object.create(shell);
1328

1429
exports.extend=Object.assign;
@@ -252,9 +267,39 @@ function parseOptions(opt, map, errorOptions) {
252267
exports.parseOptions=parseOptions;
253268

254269
functionglobOptions(){
255-
// TODO(nfischer): if this changes glob implementation in the future, convert
256-
// options back to node-glob's option format for backward compatibility.
257-
returnconfig.globOptions;
270+
// These options are just to make fast-glob be compatible with POSIX (bash)
271+
// wildcard behavior.
272+
vardefaultGlobOptions={
273+
onlyFiles:false,
274+
followSymbolicLinks:false,
275+
};
276+
277+
varnewGlobOptions=Object.assign({},config.globOptions);
278+
varoptionRenames={
279+
// node-glob's 'nodir' is not quote the same as fast-glob's 'onlyFiles'.
280+
// Compatibility for this is implemented at the call site.
281+
mark:'markDirectories',
282+
matchBase:'baseNameMatch',
283+
};
284+
Object.keys(optionRenames).forEach(function(oldKey){
285+
varnewKey=optionRenames[oldKey];
286+
if(oldKeyinconfig.globOptions){
287+
newGlobOptions[newKey]=config.globOptions[oldKey];
288+
}
289+
});
290+
varinvertedOptionRenames={
291+
nobrace:'braceExpansion',
292+
noglobstar:'globstar',
293+
noext:'extglob',
294+
nocase:'caseSensitiveMatch',
295+
};
296+
Object.keys(invertedOptionRenames).forEach(function(oldKey){
297+
varnewKey=invertedOptionRenames[oldKey];
298+
if(oldKeyinconfig.globOptions){
299+
newGlobOptions[newKey]=!config.globOptions[oldKey];
300+
}
301+
});
302+
returnObject.assign({},defaultGlobOptions,newGlobOptions);
258303
}
259304

260305
// Expands wildcards with matching (ie. existing) file names.
@@ -272,14 +317,20 @@ function expand(list) {
272317
expanded.push(listEl);
273318
}else{
274319
varret;
320+
varglobOpts=globOptions();
275321
try{
276-
ret=glob.sync(listEl,globOptions());
277-
// if nothing matched, interpret the string literally
278-
ret=ret.length>0 ?ret :[listEl];
322+
ret=glob.sync(listEl,globOpts);
279323
}catch(e){
280324
// if glob fails, interpret the string literally
281325
ret=[listEl];
282326
}
327+
// if nothing matched, interpret the string literally
328+
ret=ret.length>0 ?ret.sort() :[listEl];
329+
if(globOpts.nodir){
330+
ret=ret.filter(function(file){
331+
return!statNoFollowLinks(file).isDirectory();
332+
});
333+
}
283334
expanded=expanded.concat(ret);
284335
}
285336
});

‎src/ls.js‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
varpath=require('path');
22
varfs=require('fs');
33
varcommon=require('./common');
4-
varglob=require('glob');
4+
varglob=require('fast-glob');
55

66
// glob patterns use the UNIX path seperator
77
varglobPatternRecursive='/**';
@@ -106,13 +106,20 @@ function _ls(options, paths) {
106106
if(stat.isDirectory()&&!options.directory){
107107
if(options.recursive){
108108
// use glob, because it's simple
109-
glob.sync(p+globPatternRecursive,{dot:options.all,follow:options.link})
110-
.forEach(function(item){
111-
// Glob pattern returns the directory itself and needs to be filtered out.
112-
if(path.relative(p,item)){
113-
pushFile(item,path.relative(p,item));
114-
}
115-
});
109+
glob.sync(p+globPatternRecursive,{
110+
// These options are just to make fast-glob be compatible with POSIX
111+
// (bash) wildcard behavior.
112+
onlyFiles:false,
113+
114+
// These options depend on the cmdOptions provided to ls.
115+
dot:options.all,
116+
followSymbolicLinks:options.link,
117+
}).forEach(function(item){
118+
// Glob pattern returns the directory itself and needs to be filtered out.
119+
if(path.relative(p,item)){
120+
pushFile(item,path.relative(p,item));
121+
}
122+
});
116123
}elseif(options.all){
117124
// use fs.readdirSync, because it's fast
118125
fs.readdirSync(p).forEach(function(item){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp