3 Commits
Author SHA1 Message Date
TJ Holowaychuk 1159d4cac8 Release v1.2.0 2020-05-12 10:34:09 +01:00
92b4625797 Do not prune *.d.ts files (#66)
* Do not prune *.d.ts files

* Added --include and --exclude flags

Co-authored-by: dmattia <david@transcend.io>
2020-05-12 10:32:12 +01:00
TJ Holowaychuk 330b83e1fc remove goreleaser.yml 2020-02-15 10:52:10 +00:00
4 changed files with 82 additions and 33 deletions
+7 -1
View File
@@ -1,5 +1,11 @@
v1.2.0 / 2020-05-12
===================
* add `--include` and `--exclude` flags
* remove goreleaser.yml
v1.1.0 / 2020-02-10 v1.1.0 / 2020-02-10
=================== ===================
* move `./cmd/node-prune/main.go` to root * move `./cmd/node-prune/main.go` to root
-19
View File
@@ -1,19 +0,0 @@
build:
main: cmd/node-prune/main.go
binary: node-prune
goos:
- darwin
- linux
- windows
- freebsd
- netbsd
- openbsd
goarch:
- amd64
- 386
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^refactor'
+47 -13
View File
@@ -128,13 +128,15 @@ type Stats struct {
// Pruner is a module pruner. // Pruner is a module pruner.
type Pruner struct { type Pruner struct {
dir string dir string
log log.Interface log log.Interface
dirs map[string]struct{} dirs map[string]struct{}
exts map[string]struct{} exts map[string]struct{}
files map[string]struct{} excepts []string
ch chan func() globs []string
wg sync.WaitGroup files map[string]struct{}
ch chan func()
wg sync.WaitGroup
} }
// Option function. // Option function.
@@ -143,12 +145,14 @@ type Option func(*Pruner)
// New with the given options. // New with the given options.
func New(options ...Option) *Pruner { func New(options ...Option) *Pruner {
v := &Pruner{ v := &Pruner{
dir: "node_modules", dir: "node_modules",
log: log.Log, log: log.Log,
exts: toMap(DefaultExtensions), exts: toMap(DefaultExtensions),
dirs: toMap(DefaultDirectories), excepts: []string{},
files: toMap(DefaultFiles), globs: []string{},
ch: make(chan func()), dirs: toMap(DefaultDirectories),
files: toMap(DefaultFiles),
ch: make(chan func()),
} }
for _, o := range options { for _, o := range options {
@@ -165,6 +169,20 @@ func WithDir(s string) Option {
} }
} }
// WithGlobs option.
func WithGlobs(s []string) Option {
return func(v *Pruner) {
v.globs = s
}
}
// WithExceptions option.
func WithExceptions(s []string) Option {
return func(v *Pruner) {
v.excepts = s
}
}
// WithExtensions option. // WithExtensions option.
func WithExtensions(s []string) Option { func WithExtensions(s []string) Option {
return func(v *Pruner) { return func(v *Pruner) {
@@ -248,6 +266,22 @@ func (p *Pruner) Prune() (*Stats, error) {
// prune returns true if the file or dir should be pruned. // prune returns true if the file or dir should be pruned.
func (p *Pruner) prune(path string, info os.FileInfo) bool { func (p *Pruner) prune(path string, info os.FileInfo) bool {
// exceptions
for _, glob := range p.excepts {
matched, _ := filepath.Match(glob, info.Name())
if matched {
return false
}
}
// globs
for _, glob := range p.globs {
matched, _ := filepath.Match(glob, info.Name())
if matched {
return true
}
}
// directories // directories
if info.IsDir() { if info.IsDir() {
_, ok := p.dirs[info.Name()] _, ok := p.dirs[info.Name()]
+28
View File
@@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"strings"
"time" "time"
"github.com/apex/log" "github.com/apex/log"
@@ -17,8 +18,27 @@ func init() {
log.SetLevel(log.WarnLevel) log.SetLevel(log.WarnLevel)
} }
type arrayFlags []string
func (i *arrayFlags) String() string {
return strings.Join(*i, ", ")
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
// Globs of files that should not be pruned
var exclusionGlobs arrayFlags
// Globs of files that should always be pruned in addition to the defaults
var inclusionGlobs arrayFlags
func main() { func main() {
debug := flag.Bool("verbose", false, "Verbose log output.") debug := flag.Bool("verbose", false, "Verbose log output.")
flag.Var(&exclusionGlobs, "exclude", "Glob of files that should not be pruned. Can be specified multiple times.")
flag.Var(&inclusionGlobs, "include", "Globs of files that should always be pruned in addition to the defaults. Can be specified multiple times.")
flag.Parse() flag.Parse()
dir := flag.Arg(0) dir := flag.Arg(0)
@@ -34,6 +54,14 @@ func main() {
options = append(options, prune.WithDir(dir)) options = append(options, prune.WithDir(dir))
} }
if len(exclusionGlobs) > 0 {
options = append(options, prune.WithExceptions(exclusionGlobs))
}
if len(inclusionGlobs) > 0 {
options = append(options, prune.WithGlobs(inclusionGlobs))
}
p := prune.New(options...) p := prune.New(options...)
stats, err := p.Prune() stats, err := p.Prune()