Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.13
require (
github.com/apex/log v1.1.1
github.com/dustin/go-humanize v1.0.0
github.com/karrick/godirwalk v1.16.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0=
github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw=
github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
Expand Down
44 changes: 41 additions & 3 deletions internal/prune/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync/atomic"

"github.com/apex/log"
"github.com/karrick/godirwalk"
)

// DefaultFiles pruned.
Expand Down Expand Up @@ -97,14 +98,17 @@ var DefaultDirectories = []string{
".idea",
".vscode",
"website",
"images",
"assets",
// "images",
// "assets",
"example",
"examples",
"coverage",
".nyc_output",
".circleci",
".github",
"man",
// "features",
".git",
}

// DefaultExtensions pruned.
Expand Down Expand Up @@ -209,7 +213,6 @@ func (p *Pruner) Prune() (*Stats, error) {
var stats Stats

p.startN(runtime.NumCPU())
defer p.stop()

err := filepath.Walk(p.dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -261,6 +264,41 @@ func (p *Pruner) Prune() (*Stats, error) {
return nil
})

p.stop()

if err != nil {
return &stats, err
}

err = godirwalk.Walk(p.dir, &godirwalk.Options{
Unsorted: true,
Callback: func(_ string, _ *godirwalk.Dirent) error { return nil },
PostChildrenCallback: func(osPathname string, _ *godirwalk.Dirent) error {
s, err := godirwalk.NewScanner(osPathname)
if err != nil {
return err
}

hasAtLeastOneChild := s.Scan()
if err := s.Err(); err != nil {
return err
}

if hasAtLeastOneChild {
return nil // do not remove directory with at least one child
}
if osPathname == p.dir {
return nil // do not remove directory that was provided top-level directory
}

err = os.Remove(osPathname)
if err == nil {
stats.FilesRemoved++
}
return err
},
})

return &stats, err
}

Expand Down