Skip to content

Instantly share code, notes, and snippets.

@zonak
Created April 9, 2012 20:08
Show Gist options
  • Save zonak/2346249 to your computer and use it in GitHub Desktop.
Save zonak/2346249 to your computer and use it in GitHub Desktop.
A usage scenario of the extended version of grunt.file.expandFiles

A usage scenario of the extended version of grunt.file.expandFiles that has the added feature of providing lists of:

  • all - set of all files matching the src definition of the task (default option)
  • changed - set of files matching the src definition of the task that have been changed since the last iteration of the watch task
  • deleted - set of files matching the src definition of the task that have been deleted since the last iteration of the watch task

The task execution would go something like this:

  • check for changed files:
files = grunt.file.expandFiles(this.file.src, {filter: "changed"})
  • if there are any changed files (including new files) compile them:
if (files.lenght > 0) { compile_changed_files }
  • if there are no changed files continue to the next step

  • check for deleted files

files = grunt.file.expandFiles(this.file.src, {filter: "deleted"})
  • if there are any deleted files delete their compiled counterparts:
if (files.lenght > 0) { delete_compiled_files }

Variation #1

A possible variation would be best explained with adding a concatenation step. If we need to do this, no additional check is needed, we just run the concat task using all the current files matching the src definition of the task:

files = grunt.file.expandFiles(this.file.src)

or

files = grunt.file.expandFiles(this.file.src, {filter: "all"})

and then run the concat task:

result = grunt.helper('concat', files, {separator: this.data.separator});

Variation #2

If we want to make the task a more general purpose and handle calls that are not coming from the watch task only.

Using the lint task as an example:

  • if we have a changed file we just lint that file
  • if we have a deleted file we don't do anything
  • but if we don't have neither changed nor deleted file we lint all the files defined for the task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment