(sourceFs fs.FS, sourceDir, targetDir string, ignoreDir ...string)
| 485 | } |
| 486 | |
| 487 | func copyDirEntries(sourceFs fs.FS, sourceDir, targetDir string, ignoreDir ...string) (err error) { |
| 488 | err = dir.CreateDirIfNotExist(targetDir) |
| 489 | if err != nil { |
| 490 | return err |
| 491 | } |
| 492 | ignoreThisDir := func(path string) bool { |
| 493 | for _, s := range ignoreDir { |
| 494 | if strings.HasPrefix(path, s) { |
| 495 | return true |
| 496 | } |
| 497 | // Also ignore nested occurrences, e.g. src/plugins/foo/node_modules |
| 498 | if strings.Contains(path, string(filepath.Separator)+s) { |
| 499 | return true |
| 500 | } |
| 501 | if strings.Contains(path, "/"+s) { |
| 502 | return true |
| 503 | } |
| 504 | } |
| 505 | return false |
| 506 | } |
| 507 | |
| 508 | err = fs.WalkDir(sourceFs, sourceDir, func(path string, d fs.DirEntry, err error) error { |
| 509 | if err != nil { |
| 510 | return err |
| 511 | } |
| 512 | if ignoreThisDir(path) { |
| 513 | return nil |
| 514 | } |
| 515 | |
| 516 | // Convert the path to use forward slashes, important because we use embedded FS which always uses forward slashes |
| 517 | path = filepath.ToSlash(path) |
| 518 | |
| 519 | // Construct the absolute path for the source file/directory |
| 520 | srcPath := filepath.Join(sourceDir, path) |
| 521 | srcPath = filepath.ToSlash(srcPath) |
| 522 | |
| 523 | // Construct the absolute path for the destination file/directory |
| 524 | dstPath := filepath.Join(targetDir, path) |
| 525 | |
| 526 | if d.IsDir() { |
| 527 | // Create the directory in the destination |
| 528 | err := os.MkdirAll(dstPath, os.ModePerm) |
| 529 | if err != nil { |
| 530 | return fmt.Errorf("failed to create directory %s: %w", dstPath, err) |
| 531 | } |
| 532 | } else { |
| 533 | // Open the source file |
| 534 | srcFile, err := sourceFs.Open(srcPath) |
| 535 | if err != nil { |
| 536 | return fmt.Errorf("failed to open source file %s: %w", srcPath, err) |
| 537 | } |
| 538 | defer srcFile.Close() |
| 539 | |
| 540 | // Create the destination file |
| 541 | dstFile, err := os.Create(dstPath) |
| 542 | if err != nil { |
| 543 | return fmt.Errorf("failed to create destination file %s: %w", dstPath, err) |
| 544 | } |
no test coverage detected