Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot recursively list all *.js files #162

Closed
pdehaan opened this issue Oct 17, 2014 · 1 comment
Closed

Cannot recursively list all *.js files #162

pdehaan opened this issue Oct 17, 2014 · 1 comment

Comments

@pdehaan
Copy link

pdehaan commented Oct 17, 2014

I think I'm either missing something... It seems like you can pass an -R flag to the ls() command and it should search recursively, but if I specify a file filter (*.js only) then it seems to ignore the recursive flag...

Steps to reproduce:

'use strict';

var shell = require('shelljs');

console.log('1. Finds all *.html files, regardless of directory depth.');
shell.find('./').filter(function (file) {
  return file.match(/\.html?$/i);
}).forEach(function (file) {
  console.log(file);
});

console.log('\n2. Finds all *.js files in the current directory only.');
shell.ls('*.js').forEach(function (file) {
  console.log(file);
});

console.log('\n3. Finds all *.js files in the nested directories.');
shell.ls('-R', ['*.js']).forEach(function (file) {
  console.log(file);
});

Output:

1. Finds all *.html files, regardless of directory depth.
node_modules/eslint/node_modules/doctrine/test/test.html
node_modules/eslint/node_modules/js-yaml/demo/index.html
node_modules/eslint/node_modules/js-yaml/node_modules/argparse/node_modules/underscore/index.html
node_modules/eslint/node_modules/js-yaml/node_modules/argparse/node_modules/underscore.string/test/test.html
node_modules/eslint/node_modules/js-yaml/node_modules/argparse/node_modules/underscore.string/test/test_standalone.html
node_modules/eslint/node_modules/js-yaml/node_modules/argparse/node_modules/underscore.string/test/test_underscore/index.html

2. Finds all *.js files in the current directory only.
index.js

3. Finds all *.js files in the nested directories.
index.js

Workaround:

Use find() and do your own file filtering using the Array.filter() method.

@nfischer
Copy link
Member

nfischer commented Jan 8, 2016

I just tested this in Bash, and saw this behavior:

$ git clone https://github.com/shelljs/shelljs.git # a repo we all know and love
$ cd shelljs/
$ ls *.js
global.js  make.js  shell.js
$ ls -R *.js # only goes in the current directory
global.js  make.js  shell.js
$ ls test/*.js # even though a subdirectory has many .js files
test/cat.js
test/cd.js
test/chmod.js
... # and many more .js files

Since ShellJS seems to emulate the Bash behavior, I think we should close this issue. But excellent work finding a suitable workaround for your use case! If you see different behavior from your version of ls, please ping this thread and we can definitely reconsider making a PR for this.

@nfischer nfischer closed this as completed Jan 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants