You've already forked go-semantic-release
feat(integrations): add first simple npm integration
Integrations are simple helpers to make integration with existing tools easier.
At basic npm support, the integration will set the version before release to the `package.json`
```yml
integrations:
npm:
enabled: true
```
This commit is contained in:
committed by
Felix Wiedmann
parent
47a54436f5
commit
c7d6c7cc7b
26
internal/integrations/integrations.go
Normal file
26
internal/integrations/integrations.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package integrations
|
||||
|
||||
import (
|
||||
"github.com/Nightapes/go-semantic-release/internal/shared"
|
||||
"github.com/Nightapes/go-semantic-release/pkg/config"
|
||||
)
|
||||
|
||||
// Integrations struct
|
||||
type Integrations struct {
|
||||
version *shared.ReleaseVersion
|
||||
config *config.Integrations
|
||||
}
|
||||
|
||||
func New(config *config.Integrations, version *shared.ReleaseVersion) *Integrations {
|
||||
return &Integrations{
|
||||
config: config,
|
||||
version: version,
|
||||
}
|
||||
}
|
||||
|
||||
func (i Integrations) Run() error {
|
||||
if i.config.NPM.Enabled {
|
||||
return i.updateNPM()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
28
internal/integrations/npm.go
Normal file
28
internal/integrations/npm.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package integrations
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/sjson"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func (i *Integrations) updateNPM() error {
|
||||
|
||||
npmConfig := i.config.NPM
|
||||
if npmConfig.Path == "" {
|
||||
npmConfig.Path = "./package.json"
|
||||
}
|
||||
|
||||
log.Debugf("Set version %s to %s", i.version.Next.Version, npmConfig.Path)
|
||||
data, err := ioutil.ReadFile(npmConfig.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newData, err := sjson.Set(string(data), "version", i.version.Next.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(npmConfig.Path, []byte(newData), 0777)
|
||||
}
|
||||
62
internal/integrations/npm_test.go
Normal file
62
internal/integrations/npm_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package integrations
|
||||
|
||||
import (
|
||||
"github.com/Masterminds/semver"
|
||||
"github.com/Nightapes/go-semantic-release/internal/shared"
|
||||
"github.com/Nightapes/go-semantic-release/pkg/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIntegrations_updateNPM(t *testing.T) {
|
||||
file, err := ioutil.TempFile("", "package")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
err = ioutil.WriteFile(file.Name(), []byte(`{
|
||||
"name": "test",
|
||||
"version": "0.0.0",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"ng": "ng",
|
||||
"nx": "nx"
|
||||
}
|
||||
}`), 0777)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testVersion, err := semver.NewVersion("1.2.0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
i := New(&config.Integrations{NPM: config.IntegrationNPM{
|
||||
Enabled: true,
|
||||
Path: file.Name(),
|
||||
}}, &shared.ReleaseVersion{
|
||||
Next: shared.ReleaseVersionEntry{
|
||||
Version: testVersion,
|
||||
},
|
||||
})
|
||||
|
||||
assert.NoError(t, i.updateNPM())
|
||||
updatedFile, err := ioutil.ReadFile(file.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, `{
|
||||
"name": "test",
|
||||
"version": "1.2.0",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"ng": "ng",
|
||||
"nx": "nx"
|
||||
}
|
||||
}`, string(updatedFile))
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user