Skip to content

Commit 48404fa

Browse files
committed
feat: add spark magic clean command for node_modules and .venv
1 parent 46ac4ff commit 48404fa

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

cmd/magic/clean.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

tasks/features/magic/cleanup.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Cleanup Commands
2+
3+
- Clean up node_modules in a given directory recursively
4+
- Cleanup .venv in a given directory recursively
5+
6+
## Task 1: create commands to clean up project
7+
8+
- [x] 创建一个新的子命令 clean,用来清理项目中的 node_modules 和 .venv 目录
9+
- [x] 新命令命名为 `spark magic clean -m node`,`spark magic clean -m python`
10+
- [x] 新命令默认清理当前目录下的 node_modules 和 .venv 目录

0 commit comments

Comments
 (0)