Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt recursive copy

Tags:

gruntjs

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)?

like image 933
richwil Avatar asked Oct 02 '13 13:10

richwil


2 Answers

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

like image 83
aqm Avatar answered Oct 07 '22 17:10

aqm


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.

like image 43
Simon Boudrias Avatar answered Oct 07 '22 17:10

Simon Boudrias