Grunt relative file path globbing

447 views Asked by At

Is it possible to use Globbing partially on a directory in a file path?

I have a grunt-contrib-less task set up, the file path for my task looks something like this:

files: {
  "../../application/user/themes/some-theme-5.1.1.5830/css/main.css": "less/base.less",
}

However the version number in the relative path may sometime change, such as:

files: {
  "../../application/user/themes/some-theme-5.1.1.5831/css/main.css": "less/base.less",
}

Ideally I'd like to something like this:

files: {
  "../../application/user/themes/some-theme-*/css/main.css": "less/base.less",
}

Is there a way of doing this? With the above syntax it stops searching after the asterisk.

1

There are 1 answers

1
RobC On BEST ANSWER

One potential solution to achieve this is to utilize grunts --options feature.

When running a grunt task via the command line it is possible to specify an additional options value.

In your scenario you could pass in the version number of the folder name that is going to change. (I.e. In your case the part that you tried to specify using the asterisk character (*) E.g. '5.1.1.5830'

Caveat: For this solution to be of any use it does require knowing what that value, (the version number), of the destination folder is upfront prior to running the task via the command line.


Example Gruntfile.js

module.exports = function(grunt) {

    grunt.initConfig({

        themesFolder: {
            namePart: '0.0.0.0' // <-- If no option is passed via the CLI this name will be used.
        },

        less: {
            production: {
                options: {
                    // ...
                },
                files: {
                    // The destination path below utilizes a grunt template for the part
                    // of the folder name that will change. E.g. '5.1.1.0'
                    '../../application/user/themes/some-theme-<%= themesFolder.name %>/css/main.css': 'less/base.less'
                }
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-less');

    grunt.registerTask('saveFolderNameFromOption', 'Uses the option provided to configure name part.', function(n) {
        var themesFolder = grunt.option('themesFolder');
        
        if (themesFolder) {
            // Store the option value so it can be referenced in the less task.
            grunt.config('themesFolder.namePart', themesFolder);
        }
    });

    grunt.registerTask('processLess', ['saveFolderNameFromOption', 'less:production']);

};

Running the ProcessLess task

Run the task via the command line as follows:

$ grunt processLess --themesFolder=5.1.1.5830

Note: The additional option that is specified. Namely: --themesFolder=5.1.1.5830

When using the above command the .css output will be directed to the following path:

'../../application/user/themes/some-theme-5.1.1.5830/css/main.css': 'less/base.less'


Now, each time you run the task you modify the options accordingly.

Benefits: By providing the version number as an option via the CLI will avoid having to reconfigure your Gruntfile.js each time it is run.