|
| 1 | +package magic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/pterm/pterm" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/spf13/viper" |
| 11 | +) |
| 12 | + |
| 13 | +var cleanMode string |
| 14 | + |
| 15 | +var cleanCmd = &cobra.Command{ |
| 16 | + Use: "clean", |
| 17 | + Short: "Clean up project directories", |
| 18 | + Long: `Clean up node_modules and .venv directories recursively. |
| 19 | +
|
| 20 | +Examples: |
| 21 | + spark magic clean # Clean both node_modules and .venv |
| 22 | + spark magic clean -m node # Clean only node_modules |
| 23 | + spark magic clean -m python # Clean only .venv`, |
| 24 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 25 | + paths := viper.GetStringSlice("repo-path") |
| 26 | + if len(paths) == 0 { |
| 27 | + paths = []string{"."} |
| 28 | + } |
| 29 | + |
| 30 | + cleanNode := cleanMode == "" || cleanMode == "node" |
| 31 | + cleanPython := cleanMode == "" || cleanMode == "python" |
| 32 | + |
| 33 | + if !cleanNode && !cleanPython { |
| 34 | + return fmt.Errorf("invalid mode: %s. Supported modes: node, python", cleanMode) |
| 35 | + } |
| 36 | + |
| 37 | + var targets []string |
| 38 | + if cleanNode { |
| 39 | + targets = append(targets, "node_modules") |
| 40 | + } |
| 41 | + if cleanPython { |
| 42 | + targets = append(targets, ".venv") |
| 43 | + } |
| 44 | + |
| 45 | + var cleaned []string |
| 46 | + var errs []error |
| 47 | + |
| 48 | + for _, root := range paths { |
| 49 | + err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { |
| 50 | + if err != nil { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + if d.IsDir() && d.Name() == ".git" { |
| 55 | + return filepath.SkipDir |
| 56 | + } |
| 57 | + |
| 58 | + if !d.IsDir() { |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + for _, target := range targets { |
| 63 | + if d.Name() == target { |
| 64 | + if err := os.RemoveAll(path); err != nil { |
| 65 | + errs = append(errs, fmt.Errorf("failed to remove %s: %w", path, err)) |
| 66 | + } else { |
| 67 | + cleaned = append(cleaned, path) |
| 68 | + } |
| 69 | + return filepath.SkipDir |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + errs = append(errs, err) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if len(cleaned) > 0 { |
| 81 | + pterm.Success.Printf("Cleaned %d directorie(s):\n", len(cleaned)) |
| 82 | + for _, c := range cleaned { |
| 83 | + pterm.Printf(" - %s\n", c) |
| 84 | + } |
| 85 | + } else { |
| 86 | + pterm.Info.Println("No directories to clean.") |
| 87 | + } |
| 88 | + |
| 89 | + if len(errs) > 0 { |
| 90 | + pterm.Println() |
| 91 | + pterm.Error.Printf("Encountered %d error(s):\n", len(errs)) |
| 92 | + for _, e := range errs { |
| 93 | + pterm.Printf(" - %v\n", e) |
| 94 | + } |
| 95 | + return fmt.Errorf("cleanup completed with errors") |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | + }, |
| 100 | +} |
| 101 | + |
| 102 | +func init() { |
| 103 | + cleanCmd.Flags().StringVarP(&cleanMode, "mode", "m", "", "Cleanup mode: node, python (default: both)") |
| 104 | + MagicCmd.AddCommand(cleanCmd) |
| 105 | +} |
0 commit comments