How to copy files without the full path with grunt.js?
Asked Answered
C

2

27

I want to copy the content of /pckg to /dist with grunt.js. Here is the structure:

  |-- folder1
  |     |
  |     |-- folder2
  |           |
  |           |-- pckg
  |                 |
  |                 |--myfolder
  |                 |    |
  |                 |    |-- myfiles
  |                 |
  |                 |--myfiles
  |
  |
  |-- dist
        |
        |--myfolder
        |   |
        |   |-- myfiles
        |
        |--myfiles

Here's my Gruntfile.js

module.exports = function (grunt) {

  // Package configuration
  grunt.initConfig({

    // Metadata
    pkg: grunt.file.readJSON('package.json'),

    //Copy files
    copy: {
      main: {
        expand: true,
        src: 'folder1/folder2/pckg/**',
        dest: 'dest/'
      }
    }

  });

  // Load the plugin that provides the "copy" task.
  grunt.loadNpmTasks('grunt-contrib-copy');

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

When I run Grunt, it keep the path. It copy everything in dit/folder1/folder2/pckg. What is wrong ?

Thanks for your help !

Cretin answered 10/10, 2013 at 13:25 Comment(0)
C
55

Here's what I've used:

copy: {
  main: {
    expand: true,
    cwd: 'folder1/folder2/pckg/',
    src: ['**'],
    dest: 'dist/'
  }
}
Cretin answered 11/10, 2013 at 5:39 Comment(2)
This is the correct answer. The documentation uses the cwd param but never explains it.Donnetta
grunt is such a "great" tool.. documentation is almost on the same levelJanel
C
16

use flatten:true

copy: {
    main: {
        files: [ 
            {expand: true, src: ['components/xxx/*'], dest: 'dist/', flatten: true}
        ]
    }
}
Conclusive answered 10/10, 2013 at 13:49 Comment(2)
I want to keep the folder structure inside pckg. Is it ok with this config ?Cretin
@alienlebarge, faltten dose not keep the folder structure inside pckg.Apgar

© 2022 - 2024 — McMap. All rights reserved.