woodpecker/pkg/plugin/publish/npm.go

73 lines
1.8 KiB
Go
Raw Normal View History

2014-02-07 10:10:01 +00:00
package publish
2014-05-04 17:29:51 +00:00
import (
"fmt"
"github.com/drone/drone/pkg/build/buildfile"
)
2014-05-05 01:48:03 +00:00
// use npm trick instead of running npm adduser that requires stdin
2014-05-04 17:29:51 +00:00
var npmLoginCmd = `
2014-05-05 01:48:03 +00:00
cat <<EOF > ~/.npmrc
_auth = $(echo "%s:%s" | tr -d "\r\n" | base64)
email = %s
2014-05-04 17:29:51 +00:00
EOF
`
type NPM struct {
// The Email address used by NPM to connect
// and publish to a repository
Email string `yaml:"email,omitempty"`
// The Username used by NPM to connect
// and publish to a repository
Username string `yaml:"username,omitempty"`
// The Password used by NPM to connect
// and publish to a repository
Password string `yaml:"password,omitempty"`
// Fails if the package name and version combination already
// exists in the registry. Overwrites when the "--force" flag is set.
Force bool `yaml:"force"`
// The registry URL of custom npm repository
Registry string `yaml:"registry,omitempty"`
// A folder containing the package.json file
Folder string `yaml:"folder,omitempty"`
// Registers the published package with the given tag
Tag string `yaml:"tag,omitempty"`
Branch string `yaml:"branch,omitempty"`
}
func (n *NPM) Write(f *buildfile.Buildfile) {
if len(n.Email) == 0 || len(n.Username) == 0 || len(n.Password) == 0 {
return
}
npmPublishCmd := "npm publish %s"
if n.Tag != "" {
npmPublishCmd += fmt.Sprintf(" --tag %s", n.Tag)
}
if n.Force {
npmPublishCmd += " --force"
}
f.WriteCmdSilent("echo 'publishing to NPM ...'")
// Login to registry
f.WriteCmdSilent(fmt.Sprintf(npmLoginCmd, n.Username, n.Password, n.Email))
2014-05-05 02:05:24 +00:00
// Setup custom npm registry
if n.Registry != "" {
f.WriteCmdSilent(fmt.Sprintf("npm config set registry %s", n.Registry))
}
2014-05-04 17:29:51 +00:00
f.WriteCmd(fmt.Sprintf(npmPublishCmd, n.Folder))
}