Search code examples
javascriptnode.jsgruntjsglob

Grunt globbing ignore all directories


Say I have a directory structure and I have a grunt task that does something with source files:

src: [
  "foo/bar/**/*"
]

This will match all files and folders inside foo/bar.

For instance, if I have a grunt task that runs "this.files.forEach", all files and folders will be iterated over. I know I can just check if the item is a file by "grunt.file.isFile", but is there a way to specify in the globbing pattern to only match files?


Solution

  • I think what you are looking for is the filter option which you can pass 'isFile' to.

    Based on the gruntjs docs example, your configuration should look like:

    grunt.initConfig({
      clean: {
        foo: {
          src: ["foo/bar/**/*"],
          filter: "isFile",
        },
      },
    });
    

    Not quite in the globbing pattern, but still more "declarative" than having to do a this.files.forEach with grunt.file.isFile.