Grunt recursive copy

跟風遠走 提交于 2019-12-23 07:34:06

问题


I'm setting up a Grunt script that needs to copy and reorganise directories of images from A to B. Simple enough.

Directory structure:

components

  • componentA
    • js
    • img
      • imgfolderA
      • imgfolderB
    • css
  • componentB
    • js
    • img
      • imgfolderA

Each img directory could contain other directories and directories within those directories to help organise the images.

I want to use Grunt to take all those images and put them under one directory (assets/img):

assets

  • img
    • dirA
      • imgfolderA
      • imgfolderB
    • dirB
      • imgfolderA

Any ideas on how could I do this in grunt without specifying each component directory (it needs to be fully automated)?


回答1:


This is rather simple using grunt.file.expand.

Just pass matching glob patterns (e.g. **/img/**), and then recurse on the returned matching file values to copy.




回答2:


know it's a bit late but this should do the job, use 'grunt-contrib-copy' like so

module.exports = function (grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    copy: {
      production: {
        files: [{
            expand: true,
            cwd: "componentA/img/imgfolderA/",
            src: ["*.*", "**/*.*"],
            dest: "./assets/img/dirA/",
          },
          {
            expand: true,
            cwd: "componentB/img/imgfolderB/",
            src: ["*.*", "**/*.*"],
            dest: "./assets/img/dirB/",
          },
        ]
      }
    }
  });

  // Production Build Tools
  grunt.loadNpmTasks('grunt-contrib-copy');

  // Default Production Build task(s).
  grunt.registerTask('default', ['copy']);
};

ps magic is in the files objects, there not very well documented, but the documentation is here, after one or two reads it makes sense honest!

grunt-contrib-copy setup: https://github.com/gruntjs/grunt-contrib-copy (the readme at the bottom)

files object setup: http://gruntjs.com/configuring-tasks#globbing-patterns

task setup: http://gruntjs.com/configuring-tasks



来源:https://stackoverflow.com/questions/19138329/grunt-recursive-copy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!