0

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.

HastingsDirect
  • 628
  • 1
  • 9
  • 17
  • 1
    I'm pretty certain _globbing_ patterns are not going to help with this. _Globbing_ is typically used for specifying the source file paths and not the destination paths as shown in your example code. See the part that reads _"...specify all source filepaths..."_ in grunts documentation for [globbing-patterns](http://gruntjs.com/configuring-tasks#globbing-patterns). – RobC Jan 06 '17 at 14:30

1 Answers1

0

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.

Community
  • 1
  • 1
RobC
  • 22,977
  • 20
  • 73
  • 80
  • Thanks for the suggestions, I went with half this solution in the end as it suited my needs better - just setting the version number as a property in `grunt.initConfig` as `theme: 'theme-5.1.1.5847'` then using `<%= theme %>` in the path. Then updating the Gruntfile each time the version number changes. – HastingsDirect Jan 12 '17 at 15:17