0

是否可以在文件路径中的目录上部分使用 Globbing?

我设置了一个 grunt-contrib-less 任务,我的任务的文件路径如下所示:

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

但是,相对路径中的版本号有时可能会发生变化,例如:

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

理想情况下,我想要这样的事情:

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

有没有办法做到这一点?使用上述语法,它会在星号之后停止搜索。

4

1 回答 1

0

实现此目的的一种潜在解决方案是利用grunts --options功能。

通过命令行运行 grunt 任务时,可以指定附加选项值。

在您的场景中,您可以传入将要更改的文件夹名称的版本号。(即在您的情况下,您尝试使用星号字符(*)指定的部分,例如“5.1.1.5830”

警告要使此解决方案发挥作用,它确实需要在通过命令行运行任务之前预先知道目标文件夹的值(版本号)是什么。


示例 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']);

};

运行ProcessLess任务

通过命令行运行任务,如下所示:

$ grunt processLess --themesFolder=5.1.1.5830

注意:指定的附加选项。即:--themesFolder=5.1.1.5830

使用上述命令时,.css输出将定向到以下路径:

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


现在,每次运行任务时,您都会相应地修改选项。

好处:通过 CLI 提供版本号作为选项,将避免Gruntfile.js每次运行时都必须重新配置。

于 2017-01-06T16:15:41.213 回答