2021-07-15 14:46:04 +02:00
|
|
|
package npm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ignoreFilename = ".npmignore"
|
|
|
|
)
|
|
|
|
|
2022-03-02 15:06:51 +02:00
|
|
|
var (
|
2023-08-16 12:57:04 +02:00
|
|
|
writeIgnoreFile = os.WriteFile
|
2022-03-02 15:06:51 +02:00
|
|
|
)
|
|
|
|
|
2021-07-15 14:46:04 +02:00
|
|
|
func NewNPMIgnore(path string) NPMIgnore {
|
|
|
|
if !strings.HasSuffix(path, ignoreFilename) {
|
|
|
|
path = filepath.Join(path, ignoreFilename)
|
|
|
|
}
|
|
|
|
return NPMIgnore{filepath: path, values: []string{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NPMIgnore struct {
|
|
|
|
filepath string
|
|
|
|
values []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ignorefile *NPMIgnore) Write() error {
|
|
|
|
content := strings.Join(ignorefile.values, "\n")
|
|
|
|
|
2022-03-02 15:06:51 +02:00
|
|
|
if err := writeIgnoreFile(ignorefile.filepath, []byte(content+"\n"), 0644); err != nil {
|
2021-07-15 14:46:04 +02:00
|
|
|
return errors.Wrapf(err, "failed to write %s", ignorefile.filepath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ignorefile *NPMIgnore) Load() error {
|
|
|
|
file, err := os.Open(ignorefile.filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
lines = append(lines, scanner.Text())
|
|
|
|
}
|
|
|
|
ignorefile.values = lines
|
|
|
|
return scanner.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ignorefile *NPMIgnore) Add(value string) {
|
|
|
|
ignorefile.values = append(ignorefile.values, value)
|
|
|
|
}
|