Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I skip a grunt task if a directory is empty

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?

like image 380
jtfairbank Avatar asked Mar 18 '14 00:03

jtfairbank


People also ask

Is grunt a task runner?

Grunt is a JavaScript task runner, a tool used to automatically perform frequent tasks such as minification, compilation, unit testing, and linting. It uses a command-line interface to run custom tasks defined in a file (known as a Gruntfile). Grunt was created by Ben Alman and is written in Node.

What is grunt concat?

Concatenating with a custom separator In this example, running grunt concat:dist (or grunt concat because concat is a multi task) will concatenate the three specified source files (in order), joining files with ; and writing the output to dist/built. js . // Project configuration.

Where do you define configuration of grunt plugin?

Grunt Configuration Task configuration is specified in your Gruntfile via the grunt. initConfig method. This configuration will mostly be under task-named properties, but may contain any arbitrary data.

What is grunt js file?

Grunt is a command line Javascript task runner utilizing Node. js platform. It runs custom defined repetitious tasks and manages process automation. The project's homepage lists many big players in software development that use Grunt in their development as part of continuous integration workflow.


1 Answers

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

like image 198
Kosmotaur Avatar answered Oct 19 '22 07:10

Kosmotaur