1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/npm/ignore.go
Jordi van Liempt 0ba4c2206c
chore(deps): Replace io/ioutil package (#4494)
* update all deprecated ioutil usages

* forgotten changes

* add missing imports

* undo changing comment

* add missing 'os' import

* fix integration test

---------

Co-authored-by: I557621 <jordi.van.liempt@sap.com>
Co-authored-by: Gulom Alimov <gulomjon.alimov@sap.com>
2023-08-16 12:57:04 +02:00

60 lines
1.1 KiB
Go

package npm
import (
"bufio"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
const (
ignoreFilename = ".npmignore"
)
var (
writeIgnoreFile = os.WriteFile
)
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")
if err := writeIgnoreFile(ignorefile.filepath, []byte(content+"\n"), 0644); err != nil {
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)
}