0

How can I fix ./src/js/**/!(*.spec.js|*.test.js), so it doesn't return directories? I've tried it like this ./src/js/**/*.!(spec.js|test.js), but it matches all files except the directories, the same as ./src/js/**/*. Example output of ./src/js/**/!(*.spec.js|*.test.js):

[ './src/js/__tests__',
  './src/js/__tests__/example.js',
  './src/js/example.js',
  './src/js/index.js' ]

Desired output:

[ './src/js/__tests__/example.js',
  './src/js/example.js',
  './src/js/index.js' ]

glob version 7.1.3

emanuilov
  • 122
  • 1
  • 10

1 Answers1

1

Basically you want to exclude folders in your case, which can be done by adding (^/) at the end.

The complete glob should be ./src/js/**/!(*.spec.js|*.test.js|^/)

You can verify the micromatch syntax here

UPDATE:

With the npm module glob there is an option which is called nodir. If it is set to true, there wont be any directory paths in the output.

Example:

var glob = require("glob")

glob("./src/js/**/!(*.spec.js|*.test.js)", { nodir: true }, (er, files) => {
  console.log(er, files)
})

You can find more information on all available options in their section about options.

Leo
  • 1,702
  • 13
  • 15
  • 1
    I didn't clarify enough, the wildcard that I've posted returns the needed files, but it also includes any directory that it finds, I need to make it return just the files. – emanuilov Mar 05 '19 at 19:55
  • Ah it does make more sense with this clarification. Basically you want to exclude folders in your case, which can be done by adding `(^/)` at the end. The complete glob should be `./src/js/**/!(*.spec.js|*.test.js)(^/)` – Leo Mar 05 '19 at 20:07
  • When I execute this I get an empty array. – emanuilov Mar 05 '19 at 20:15
  • Ah if you are using the module glob, you can just set the nodir option to true. I will add an example to the answer. – Leo Mar 05 '19 at 20:44