14

I have a directory structure as follows:

source/
    libraries/
        d3.js
        lodash.js
        //etc

I have grunt-copy setup as follows:

copy: {
  main: {
    files: [
      {
        src: ["source/libraries/*.js"], 
        dest: "build/", 
        flatten: true
      }

I expect it to flatten the output into build, so that I will have

build/
    d3.js
    //etc

Instead, I get a reproduction of the original directory structure in build:

build/
    source/
        libraries/
            d3.js
            //etc

What gives? Am I not using flatten properly?

dandelion
  • 1,742
  • 1
  • 15
  • 18

1 Answers1

21

Well, if you're only using flatten because you want everything in source/libraries to go into build, I would suggest actually using the cwd (current working directory) option instead. If, on the other hand, you actually have subfolders in source/libraries then you probably want that src line to be source/libraries/**/*.js.

In any case, if you can use cwd instead it would look like this:

copy: {
  main: {
    files: [
      {
        src: ["*.js"],
        dest: "build/",
        cwd: "source/libraries/"
      }
    ]
  }

For the other case, maybe this? (Notice the expand option set to true)

copy: {
  main: {
    files: [
      {
        src: ["source/libraries/**/*.js"],
        dest: "build/",
        flatten: true,
        expand: true
      }
    ]
  }
}
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
  • Using cwd does fix my issue. However, based on the grunt docs, it looks like flatten _should_ work too: `"flatten Remove all path parts from generated dest paths."` Do you know why it doesnt? – dandelion Dec 23 '13 at 23:40
  • 4
    Did you try adding the `expand` option as well? Every time I've ever seen `flatten` I see it with `expand`. – Jordan Kasper Dec 23 '13 at 23:40
  • 4
    heh... just read up on the docs... right above that line you mention is this: "`expand` Set to `true` to enable the following options:" (`flatten` is one of the "following options"). ;) – Jordan Kasper Dec 23 '13 at 23:48