Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Grunt-Contrib-Copy to copy files/directories relative to given source path

First time using this task and what I'm trying to achieve is the following:

copy all directories/files from src/js/bower_components/* to build/assets/js/vendor/

I've tried using cwd property but it doesn't work at all when I use it.. I've set it to: src/js/bower_components/

From src

.
├── Gruntfile
└── src
    └── js
        └── bower_components
            └── jquery

I currently get:

.
├── Gruntfile
└── build
    └── assets
        └── js
            └── vendor
                src
                └── js
                    └── bower_components
                        └── jquery

What I'd like

.
├── Gruntfile
└── build
    └── assets
        └── js
            └── vendor
                └──jquery

Here's my current grunt task

copy: {
  main: {
    src: 'src/js/bower_components/*',
    dest: 'build/assets/js/vendor/',
    expand: true,
  }
},

Thanks for any help

like image 219
micahblu Avatar asked Mar 27 '14 19:03

micahblu


People also ask

How do I run a copy task in Grunt?

Run this task with the grunt copy command. Task targets, files and options may be specified according to the grunt Configuring tasks guide. This option is passed to grunt.file.copy as an advanced way to control the file contents that are copied. processContent has been renamed to process and the option name will be removed in the future.

What is processcontentexclude in Grunt?

This option is passed to grunt.file.copy as an advanced way to control which file contents are processed. processContentExclude has been renamed to noProcess and the option name will be removed in the future. The file encoding to copy files with. Whether to copy or set the destination file and directory permissions.

How to change the contents of a file as it's copied?

To change the contents of a file as it is copied, set an options.process function as follows: Here all occurrences of the letters "s", "a" and "d", as well as all spaces, will be changed to underlines in "a.bak". Of course, you are not limited to just using regex replacements.

What version of grunt is this plugin designed to work with?

This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that you upgrade, but in case you can't please use v0.3.2. Run this task with the grunt copy command.


1 Answers

I've set up an example project with tree like this:

.
├── Gruntfile.js
├── package.json
└── src
    └── js
        └── foo.js

Using the below Gruntfile:

module.exports = function(grunt) {
  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({
    copy          : {
      foo : {
        files : [
          {
            expand : true,
            dest   : 'dist',
            cwd    : 'src',
            src    : [
              '**/*.js'
            ]
          }
        ]
      }
    }
  });

  grunt.registerTask('build', function(target) {
    grunt.task.run('copy');
  });

};

This gave me this structure:

.
├── Gruntfile.js
├── dist
│   └── js
│       └── foo.js
├── package.json
└── src
    └── js
        └── foo.js

When I had changed cwd so that the Gruntfile read:

module.exports = function(grunt) {
  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({
    copy          : {
      foo : {
        files : [
          {
            expand : true,
            dest   : 'dist',
            cwd    : 'src/js',
            src    : [
              '**/*.js'
            ]
          }
        ]
      }
    }
  });

  grunt.registerTask('build', function(target) {
    grunt.task.run('copy');
  });

};

I got this dir structure:

.
├── Gruntfile.js
├── dist
│   └── foo.js
├── package.json
└── src
    └── js
        └── foo.js

So it seems like cwd does what you need. Maybe you left src at src/js/bower_components/* when setting cwd to src/js/bower_components? In that case, src should read something like **/*.js, but depending on what you really need.

like image 106
Kosmotaur Avatar answered Sep 29 '22 23:09

Kosmotaur