1 | var path = require('path');
|
2 | var common = require('./common');
|
3 | var _ls = require('./ls');
|
4 |
|
5 | common.register('find', _find, {});
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function _find(options, paths) {
|
23 | if (!paths) {
|
24 | common.error('no path specified');
|
25 | } else if (typeof paths === 'string') {
|
26 | paths = [].slice.call(arguments, 1);
|
27 | }
|
28 |
|
29 | var list = [];
|
30 |
|
31 | function pushFile(file) {
|
32 | if (process.platform === 'win32') {
|
33 | file = file.replace(/\\/g, '/');
|
34 | }
|
35 | list.push(file);
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | paths.forEach(function (file) {
|
42 | var stat;
|
43 | try {
|
44 | stat = common.statFollowLinks(file);
|
45 | } catch (e) {
|
46 | common.error('no such file or directory: ' + file);
|
47 | }
|
48 |
|
49 | pushFile(file);
|
50 |
|
51 | if (stat.isDirectory()) {
|
52 | _ls({ recursive: true, all: true }, file).forEach(function (subfile) {
|
53 | pushFile(path.join(file, subfile));
|
54 | });
|
55 | }
|
56 | });
|
57 |
|
58 | return list;
|
59 | }
|
60 | module.exports = _find;
|