Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy folder structure with flatten=false and ignore first levels #56

Open
carsten-wilhelm opened this issue Aug 5, 2021 · 4 comments
Open

Comments

@carsten-wilhelm
Copy link

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?

@weihaopeng
Copy link

same question!

@trev-dev
Copy link

trev-dev commented Sep 9, 2021

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
Copy link

trev-dev commented Sep 9, 2021

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.

@alexmeier-19
Copy link

are there some updates on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants