richwil
richwil

Reputation: 93

Grunt recursive copy

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

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

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

Upvotes: 9

Views: 9331

Answers (2)

aqm
aqm

Reputation: 3034

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

Upvotes: 22

Simon Boudrias
Simon Boudrias

Reputation: 44659

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.

Upvotes: 1

Related Questions