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

前端 未结 3 549
無奈伤痛
無奈伤痛 2021-02-08 23:30

I\'m using grunt-contrib\'s concat and uglify modules to process some javascript. Currently if src/js/ is empty, they will still create a

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-08 23:59

    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();
    

提交回复
热议问题