vladshcherbin / rollup-plugin-copy

Copy files and folders using Rollup

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

copy folder structure with flatten=false and ignore first levels

carsten-wilhelm opened this issue · 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?

same question!

commented

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
    })
commented

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.