【问题标题】:Adapt dest folder to ** globbing pattern使 dest 文件夹适应 ** globbing 模式
【发布时间】:2017-09-16 18:33:43
【问题描述】:

我正在使用 grunt-contrib-copy。我有这个文件树:

`-- src
    `-- model
        |-- a4
        |   `-- public
        |       |-- css
        |       |-- img
        |       `-- js
        |-- posters
        |   `-- public
        |       |-- css
        |       |-- img
        |       `-- js
        `-- prints
            `-- public
                |-- css
                |-- img
                `-- js

我想将文件复制到 src/model/**/public/imgdist/images/{1}/ 其中{1} 是文件夹名称(a4、海报、印刷品...动态文件夹也必然会发生变化),所以:

src/model/a4/public/img/file.png -> dist/images/a4/file.png

有没有办法用 grunt-contrib-copy 指定(可能是重命名功能?)还是我必须手动遍历文件?

现在这就是我所拥有的:

grunt.registerTask 'images-generate', ->
gruntCopyFiles.images = {
  expand: true,
  cwd: './src/model/',
  src: ["**/public/img/*.png", "**/public/img/*.jpg"],
  dest: "./dist/images/"
}

但这会将src/model/a4/public/img/file.png 复制到dist/images/a4/public/img/file.png,这不是我想要的。

有什么建议吗?谢谢!

【问题讨论】:

  • 刚刚意识到我可能使用的是 grunt.file.expandMapping 而不是 grunt-contrib-copy。维护别人的项目很有趣。
  • 你还有这个问题吗?
  • 是的!只是想弄清楚技术。现在我正在使用重命名功能,但如果有更好/内置/更短的方法,我会接受。我对此表示怀疑!
  • 好的,太好了。您能否将您当前对rename 的尝试添加到问题中,以使读者更清楚?

标签: gruntjs grunt-contrib-copy


【解决方案1】:

有没有办法用grunt-contrib-copy 指定它(也许是重命名功能?)还是我必须手动遍历文件?

利用rename 函数是实现此目的的方法。单独的 Glob 模式不能满足您的要求,flatten 选项也不能。

以下内容还会复制可能可能位于源img 文件夹中的所有子文件夹:

gruntCopyFiles.images = {
    expand: true,
    cwd: 'src/model',
    dest: 'dist/images',
    src: '**/public/img/**/*.{png,jpg,gif}',
    rename: function(dest, src) {
        var items = src.split('/'),
            baseIndex = items.indexOf('img') + 1,
            subPath = items.slice(baseIndex, items.length).join('/');

        return [dest, items[0], subPath].join('/');
    }
}

示例:

src/model/a4/public/img/file.png --> dist/images/a4/file.png

src/model/a4/public/img/quux/file.jpg --> dist/images/a4/quux/file.jpg

【讨论】:

  • 我最终做了类似的事情,但包含潜在的子文件夹很重要:) 谢谢!
【解决方案2】:

看起来您可以使用flatten 来获取精简的目标路径:

gruntCopyFiles.images = {
  flatten: true,
  expand: true,
  cwd: './src/model/',
  src: ["**/public/img/*.png", "**/public/img/*.jpg"],
  dest: "./dist/images/"
}

来自the docs

  • flatten 从生成的 dest 路径中删除所有路径部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2014-10-04
    • 2014-09-16
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    相关资源
    最近更新 更多