rollup-plugin-copy icon indicating copy to clipboard operation
rollup-plugin-copy copied to clipboard

copy folder structure with flatten=false and ignore first levels

Open carsten-wilhelm opened this issue 4 years ago • 4 comments

I need to copy a folder structure (so flatten=false is necessary), but do not want to have the full path in destination.

Example:

targets:[{
    src: 'folder1/folder2',
    dest: 'destfolder'
}];

If the folder1/folder2 structure looks like this:

folder3.1/a.txt
folder3.2/folder3/b.txt

I need this in destination folder:

destfolder/folder3.1/a.txt
destfolder/folder3.2/folder3/b.txt

Instead, I get this (since the source folder gets copied in addition except the first one):

destfolder/folder2/folder3.1/a.txt
destfolder/folder2/folder3.2/folder3/b.txt

Is this possible with the copy plugin at all?

carsten-wilhelm avatar Aug 05 '21 16:08 carsten-wilhelm

same question!

weihaopeng avatar Aug 19 '21 05:08 weihaopeng

I just came up against this myself and learned that flatten=false is the wrong way to go. You want to flatten the destination, but then use the rename option to manipulate the destination! Take my config as an example.

I'm working on a shopify theme using shopify's theme kit and I want to track my custom liquid file changes separately from the rest of the theme, because dozens of other devs might mutate the theme without warning.

I need to move my liquid files from ./src/liquid/**/* to shopify-theme/**/* but I want the subdirectories of ./src/liquid/** to be preserved.

To do this, I need to write a rename function:

    import path from 'path'
    // in plugins:
    copy({
      targets: [
        {
          src: "src/liquid/**/*",
          dest: "shopify-theme",
          rename: (_name, _extension, fullpath) => {
            const keptParts = fullpath.split(path.sep).filter(dir => {
              return dir !== "src" && dir !== "liquid"
            })
            return path.join(...keptParts)
          }
        }
      ],
      verbose: true
    })

In @macaw-germany's case I think the config might look more like this:

    import path from 'path'
    // in plugins:
    copy({
      targets: [
        {
          src: "folder2",
          dest: "destfolder",
          rename: (_name, _extension, fullpath) => {
            const keptParts = fullpath.split(path.sep).filter(dir => {
              return dir !== "folder2"
            })
            return path.join(...keptParts)
          }
        }
      ],
      verbose: true
    })

trev-dev avatar Sep 09 '21 20:09 trev-dev

Assuming your structure is always consistent, you can also simply use a fullpath.split(path.sep).slice(1) instead of explicitly removing directories by name. Filtering by name will get rid of further nested directories, not just the first occurrences.

trev-dev avatar Sep 09 '21 20:09 trev-dev