2021-07-15 14:46:04 +02:00
|
|
|
package npm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/magiconair/properties"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-02 15:06:51 +02:00
|
|
|
defaultConfigFilename = ".piperNpmrc" // default by npm
|
2021-07-15 14:46:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-03-02 15:06:51 +02:00
|
|
|
propertiesLoadFile = properties.LoadFile
|
|
|
|
propertiesWriteFile = ioutil.WriteFile
|
2021-07-15 14:46:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewNPMRC(path string) NPMRC {
|
2022-03-02 15:06:51 +02:00
|
|
|
if !strings.HasSuffix(path, defaultConfigFilename) {
|
|
|
|
path = filepath.Join(path, defaultConfigFilename)
|
2021-07-15 14:46:04 +02:00
|
|
|
}
|
2022-03-02 15:06:51 +02:00
|
|
|
|
2021-07-15 14:46:04 +02:00
|
|
|
return NPMRC{filepath: path, values: properties.NewProperties()}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NPMRC struct {
|
|
|
|
filepath string
|
|
|
|
values *properties.Properties
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *NPMRC) Write() error {
|
2022-03-02 15:06:51 +02:00
|
|
|
if err := propertiesWriteFile(rc.filepath, []byte(rc.values.String()), 0644); err != nil {
|
2021-07-15 14:46:04 +02:00
|
|
|
return errors.Wrapf(err, "failed to write %s", rc.filepath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *NPMRC) Load() error {
|
|
|
|
values, err := propertiesLoadFile(rc.filepath, properties.UTF8)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rc.values = values
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *NPMRC) Set(key, value string) {
|
|
|
|
rc.values.Set(key, value)
|
|
|
|
}
|