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 · shelljs/shelljs@fd77b7e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • package.json
  • src
    • common.js
    • 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-31Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var os = require('os');
22
var fs = require('fs');
3+
var glob = require('glob');
34
var _ls = require('./ls');
45

56
// Module globals
@@ -85,38 +86,15 @@ function parseOptions(str, map) {
8586
}
8687
exports.parseOptions = parseOptions;
8788

88-
// Expands wildcards with matching (ie. existing) file names.
89-
// For example:
90-
// expand(['file*.js']) = ['file1.js', 'file2.js', ...]
91-
// (if the files 'file1.js', 'file2.js', etc, exist in the current dir)
92-
function expand(list) {
93-
var expanded = [];
94-
list.forEach(function(listEl) {
95-
// Wildcard present on directory names ?
96-
if(listEl.search(/\*[^\/]*\//) > -1 || listEl.search(/\*\*[^\/]*\//) > -1) {
97-
var match = listEl.match(/^([^*]+\/|)(.*)/);
98-
var root = match[1];
99-
var rest = match[2];
100-
var restRegex = rest.replace(/\*\*/g, ".*").replace(/\*/g, "[^\\/]*");
101-
restRegex = new RegExp(restRegex);
102-
103-
_ls('-R', root).filter(function (e) {
104-
return restRegex.test(e);
105-
}).forEach(function(file) {
106-
expanded.push(file);
107-
});
108-
}
109-
// Wildcard present on file names ?
110-
else if (listEl.search(/\*/) > -1) {
111-
_ls('', listEl).forEach(function(file) {
112-
expanded.push(file);
113-
});
114-
} else {
115-
expanded.push(listEl);
116-
}
117-
});
118-
return expanded;
89+
// Just a thin wrapper around glob.sync, to allow passing in an array.
90+
function expand(list, opts) {
91+
if (!Array.isArray(list)) list = [list];
92+
opts = opts || [];
93+
return list.reduce(function (files, pattern) {
94+
return files.concat(glob.sync(pattern, opts));
95+
}, []);
11996
}
97+
12098
exports.expand = expand;
12199

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

‎src/ls.js‎

Copy file name to clipboard
+13-81Lines changed: 13 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var path = require('path');
22
var fs = require('fs');
3+
var glob = require('glob');
34
var common = require('./common');
45
var _cd = require('./cd');
56
var _pwd = require('./pwd');
@@ -36,91 +37,22 @@ function _ls(options, paths) {
3637
options.all = true;
3738
}
3839

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);
40+
if (typeof paths === 'string') paths = [].slice.call(arguments, 1);
41+
paths = paths || [];
4542

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-
}
43+
paths = paths.map(function (path) {
44+
path += (options.recursive ? '/**/*' : '/*');
45+
return path.replace(/\\/g, '/');
46+
});
5747

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

61-
list.push(file);
62-
return true;
50+
if (common.platform === 'win') {
51+
files = files.map(function (file) {
52+
return file.replace(/\\/g, '/');
53+
});
6354
}
6455

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;
56+
return files;
12557
}
12658
module.exports = _ls;

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.