|
1 | 1 | var path = require('path'); |
2 | 2 | var fs = require('fs'); |
| 3 | +var glob = require('glob'); |
3 | 4 | var common = require('./common'); |
4 | 5 | var _cd = require('./cd'); |
5 | 6 | var _pwd = require('./pwd'); |
@@ -36,91 +37,22 @@ function _ls(options, paths) { |
36 | 37 | options.all = true; |
37 | 38 | } |
38 | 39 |
|
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 || []; |
45 | 42 |
|
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 | + }); |
57 | 47 |
|
58 | | - if (common.platform === 'win') |
59 | | - file = file.replace(/\\/g, '/'); |
| 48 | + var files = common.expand(paths, { dot: options.all }); |
60 | 49 |
|
61 | | - list.push(file); |
62 | | - return true; |
| 50 | + if (common.platform === 'win') { |
| 51 | + files = files.map(function (file) { |
| 52 | + return file.replace(/\\/g, '/'); |
| 53 | + }); |
63 | 54 | } |
64 | 55 |
|
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; |
125 | 57 | } |
126 | 58 | module.exports = _ls; |