How can I skip a grunt task if a directory is empty
Asked Answered
K

3

8

I'm using grunt-contrib's concat and uglify modules to process some javascript. Currently if src/js/ is empty, they will still create an (empty) concat'd file, along with the minified version and a source map.

I want to task to detect if the src/js/ folder is empty before proceeding, and if it is, then the task should skip (not fail). Any ideas how to do this?

Kolnick answered 18/3, 2014 at 0:5 Comment(4)
Would you mind posting a copy of your Gruntfile.js? Have you tried doing everything through uglify only (also the concatenation), just for the sake of testing?Eighteen
Related/duplicate? #21001969Gentilesse
The source is available here: github.com/jtfairbank/MilkshakeKolnick
I'm specifically trying to solve the 2nd and 3rd TODOs in the Gruntfile.js.Kolnick
U
3

The solution may not be the prettiest, but could give you an idea. You'll need to run something like npm install --save-dev glob first. This is based on part of the Milkshake project you mentioned.

grunt.registerTask('build_js', function(){
  // get first task's `src` config property and see
  // if any file matches the glob pattern
  if (grunt.config('concat').js.src.some(function(src){
    return require('glob').sync(src).length;
  })) {
    // if so, run the task chain
    grunt.task.run([
        'trimtrailingspaces:js'
      , 'concat:js'
      , 'uglify:yomama'
    ]);
  }
});

A gist for comparison: https://gist.github.com/kosmotaur/61bff2bc807b28a9fcfa

Ujiji answered 24/3, 2014 at 21:16 Comment(1)
Thanks this ended up being just what I needed!Kolnick
A
2

With this plugin:

https://www.npmjs.org/package/grunt-file-exists

You can check file existence. (I didn't try, but the source looks like supporting grunt expands. (*, ** ...)

For example like this::

grunt.initConfig({
  fileExists: {
    scripts: ['a.js', 'b.js']
  },
});

grunt.registerTask('conditionaltask', [
    'fileExists',
    'maintask',
]);

But maybe if the file doesn't exist it will fail with error instead of simple skip. (I didn't test it.)

If this is a problem you can modify a bit the source of this plugin to run the related task if the file exists:

The config:

grunt.initConfig({
  fileExists: {
    scripts: ['a.js', 'b.js'],
    options: {tasks: ['maintask']}
  },
});

grunt.registerTask('conditionaltask', [
    'fileExists',
]);

And you should add this:

grunt.task.run(options.tasks);

In this file:

https://github.com/alexeiskachykhin/grunt-file-exists/blob/master/tasks/fileExists.js

after this line:

grunt.log.ok();
Adlee answered 22/3, 2014 at 9:16 Comment(1)
Looks like it'll work, but I'd prefer a cleaner solution than using a patched version of grunt-file-exists. :)Kolnick
C
2

Maybe this is just a more up-to-date answer as the others are more than a year old, but you don't need a plugin for this; you can use grunt.file.expand to test if files matching a certain globbing pattern exist.

Update of @Kosmotaur's answer (path is just hard-code here though for simplicity):

grunt.registerTask('build_js', function(){
  // if any file matches the glob pattern
  if (grunt.file.expand("subdir/**/*.js").length) { /** new bit here **/ 
    // if so, run the task chain
    grunt.task.run([
        'trimtrailingspaces:js'
      , 'concat:js'
      , 'uglify:yomama'
    ]);
  }
});
Colorable answered 7/1, 2016 at 14:9 Comment(2)
Thanks for the update Marc! Could you provide a bit more context for your code? Would I use that directly in a src string value or files array? Or would I use it in the top part of the function like @Ujiji did? If so, could you mimic his layout and provide a full example function?Kolnick
Worked for me,Thanks!Exactly

© 2022 - 2024 — McMap. All rights reserved.