Skip to content

Cannot recursively list all *.js files #162

@pdehaan

Description

@pdehaan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions