GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use `glob` for globbing, instead of home-grown solutions · ariporad/shelljs@9c31f72

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • package.json
  • src
    • common.js
    • ls.js
  • test
    • ls.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎package.json‎

Copy file name to clipboardExpand all lines: package.json
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
"bin": {
2525
"shjs": "./bin/shjs"
2626
},
27-
"dependencies": {},
27+
"dependencies": {
28+
"glob": "^6.0.3"
29+
},
2830
"devDependencies": {
2931
"jshint": "~2.1.11"
3032
},
Collapse file

‎src/common.js‎

Copy file name to clipboardExpand all lines: src/common.js
+9-28Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var os = require('os');
22
var fs = require('fs');
3-
var _ls = require('./ls');
3+
var glob = require('glob');
44

55
// Module globals
66
var config = {
@@ -106,34 +106,15 @@ exports.parseOptions = parseOptions;
106106
// For example:
107107
// expand(['file*.js']) = ['file1.js', 'file2.js', ...]
108108
// (if the files 'file1.js', 'file2.js', etc, exist in the current dir)
109-
function expand(list) {
110-
var expanded = [];
111-
list.forEach(function(listEl) {
112-
// Wildcard present on directory names ?
113-
if(listEl.search(/\*[^\/]*\//) > -1 || listEl.search(/\*\*[^\/]*\//) > -1) {
114-
var match = listEl.match(/^([^*]+\/|)(.*)/);
115-
var root = match[1];
116-
var rest = match[2];
117-
var restRegex = rest.replace(/\*\*/g, ".*").replace(/\*/g, "[^\\/]*");
118-
restRegex = new RegExp(restRegex);
119-
120-
_ls('-R', root).filter(function (e) {
121-
return restRegex.test(e);
122-
}).forEach(function(file) {
123-
expanded.push(file);
124-
});
125-
}
126-
// Wildcard present on file names ?
127-
else if (listEl.search(/\*/) > -1) {
128-
_ls('', listEl).forEach(function(file) {
129-
expanded.push(file);
130-
});
131-
} else {
132-
expanded.push(listEl);
133-
}
134-
});
135-
return expanded;
109+
// It's Just a thin wrapper around glob.sync, to allow passing in an array.
110+
function expand(list, opts) {
111+
if (!Array.isArray(list)) list = [list];
112+
opts = opts || [];
113+
return list.reduce(function (files, pattern) {
114+
return files.concat(glob.sync(pattern, opts));
115+
}, []);
136116
}
117+
137118
exports.expand = expand;
138119

139120
// Normalizes _unlinkSync() across platforms to match Unix behavior, i.e.
Collapse file

‎src/ls.js‎

Copy file name to clipboard
+12-85Lines changed: 12 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
var path = require('path');
2-
var fs = require('fs');
31
var common = require('./common');
4-
var _cd = require('./cd');
5-
var _pwd = require('./pwd');
62

73
//@
84
//@ ### ls([options,] [path, ...])
@@ -36,91 +32,22 @@ function _ls(options, paths) {
3632
options.all = true;
3733
}
3834

39-
if (!paths)
40-
paths = ['.'];
41-
else if (typeof paths === 'object')
42-
paths = paths; // assume array
43-
else if (typeof paths === 'string')
44-
paths = [].slice.call(arguments, 1);
35+
if (typeof paths === 'string') paths = [].slice.call(arguments, 1);
36+
paths = paths || [];
4537

46-
var list = [];
47-
48-
// Conditionally pushes file to list - returns true if pushed, false otherwise
49-
// (e.g. prevents hidden files to be included unless explicitly told so)
50-
function pushFile(file, query) {
51-
// hidden file?
52-
if (path.basename(file)[0] === '.') {
53-
// not explicitly asking for hidden files?
54-
if (!options.all && !(path.basename(query)[0] === '.' && path.basename(query).length > 1))
55-
return false;
56-
}
38+
paths = paths.map(function (path) {
39+
path += (options.recursive ? '/**/*' : '/*');
40+
return path.replace(/\\/g, '/');
41+
});
5742

58-
if (common.platform === 'win')
59-
file = file.replace(/\\/g, '/');
43+
var files = common.expand(paths, { dot: options.all });
6044

61-
list.push(file);
62-
return true;
45+
if (common.platform === 'win') {
46+
files = files.map(function (file) {
47+
return file.replace(/\\/g, '/');
48+
});
6349
}
6450

65-
paths.forEach(function(p) {
66-
if (fs.existsSync(p)) {
67-
var stats = fs.statSync(p);
68-
// Simple file?
69-
if (stats.isFile()) {
70-
pushFile(p, p);
71-
return; // continue
72-
}
73-
74-
// Simple dir?
75-
if (stats.isDirectory()) {
76-
// Iterate over p contents
77-
fs.readdirSync(p).forEach(function(file) {
78-
if (!pushFile(file, p))
79-
return;
80-
81-
// Recursive?
82-
if (options.recursive) {
83-
var oldDir = _pwd();
84-
_cd('', p);
85-
if (fs.statSync(file).isDirectory())
86-
list = list.concat(_ls('-R'+(options.all?'A':''), file+'/*'));
87-
_cd('', oldDir);
88-
}
89-
});
90-
return; // continue
91-
}
92-
}
93-
94-
// p does not exist - possible wildcard present
95-
96-
var basename = path.basename(p);
97-
var dirname = path.dirname(p);
98-
// Wildcard present on an existing dir? (e.g. '/tmp/*.js')
99-
if (basename.search(/\*/) > -1 && fs.existsSync(dirname) && fs.statSync(dirname).isDirectory) {
100-
// Escape special regular expression chars
101-
var regexp = basename.replace(/(\^|\$|\(|\)|<|>|\[|\]|\{|\}|\.|\+|\?)/g, '\\$1');
102-
// Translates wildcard into regex
103-
regexp = '^' + regexp.replace(/\*/g, '.*') + '$';
104-
// Iterate over directory contents
105-
fs.readdirSync(dirname).forEach(function(file) {
106-
if (file.match(new RegExp(regexp))) {
107-
if (!pushFile(path.normalize(dirname+'/'+file), basename))
108-
return;
109-
110-
// Recursive?
111-
if (options.recursive) {
112-
var pp = dirname + '/' + file;
113-
if (fs.lstatSync(pp).isDirectory())
114-
list = list.concat(_ls('-R'+(options.all?'A':''), pp+'/*'));
115-
} // recursive
116-
} // if file matches
117-
}); // forEach
118-
return;
119-
}
120-
121-
common.error('no such file or directory: ' + p, true);
122-
});
123-
124-
return list;
51+
return files;
12552
}
12653
module.exports = _ls;
Collapse file

‎test/ls.js‎

Copy file name to clipboardExpand all lines: test/ls.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,11 @@ assert.equal(result.indexOf('resources/ls/a_dir/b_dir') > -1, true);
191191
assert.equal(result.indexOf('resources/ls/a_dir/b_dir/z') > -1, true);
192192
assert.equal(result.length, 9);
193193

194+
// globs without -A
195+
var result = shell.ls('resources/ls/*');
196+
assert.equal(shell.error(), null);
197+
assert.equal(result.indexOf('resources/ls/.hidden_file') === -1, true);
198+
assert.equal(result.indexOf('resources/ls/.hidden_dir') === -1, true);
199+
assert.equal(result.length, 6);
200+
194201
shell.exit(123);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.