mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-23 10:21:00 +00:00
Merge pull request #834 from shawnzhu/npm
npm publish only when version not exist
This commit is contained in:
commit
8408cfd055
2 changed files with 23 additions and 19 deletions
|
@ -18,8 +18,22 @@ email = %s
|
||||||
EOF
|
EOF
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// command to publish npm package if not published
|
||||||
|
const CmdPublish = `
|
||||||
|
_NPM_PACKAGE_NAME=$(cd %s && npm list | head -n 1 | cut -d ' ' -f1)
|
||||||
|
_NPM_PACKAGE_TAG="%s"
|
||||||
|
if [ -z "$(npm info ${_NPM_PACKAGE_NAME})" ]
|
||||||
|
then
|
||||||
|
npm publish %s
|
||||||
|
[ -n ${_NPM_PACKAGE_TAG} ] && npm tag ${_NPM_PACKAGE_NAME} ${_NPM_PACKAGE_TAG}
|
||||||
|
else
|
||||||
|
echo "skipping publish, package ${_NPM_PACKAGE_NAME} already published"
|
||||||
|
fi
|
||||||
|
unset _NPM_PACKAGE_NAME
|
||||||
|
unset _NPM_PACKAGE_TAG
|
||||||
|
`
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CmdPublish = "npm publish %s"
|
|
||||||
CmdAlwaysAuth = "npm set always-auth true"
|
CmdAlwaysAuth = "npm set always-auth true"
|
||||||
CmdSetRegistry = "npm config set registry %s"
|
CmdSetRegistry = "npm config set registry %s"
|
||||||
)
|
)
|
||||||
|
@ -43,10 +57,6 @@ type NPM struct {
|
||||||
// and publish to a repository
|
// and publish to a repository
|
||||||
Password string `yaml:"password,omitempty"`
|
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
|
// The registry URL of custom npm repository
|
||||||
Registry string `yaml:"registry,omitempty"`
|
Registry string `yaml:"registry,omitempty"`
|
||||||
|
|
||||||
|
@ -94,16 +104,11 @@ func (n *NPM) Write(f *buildfile.Buildfile) {
|
||||||
f.WriteCmd(CmdAlwaysAuth)
|
f.WriteCmd(CmdAlwaysAuth)
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmd = fmt.Sprintf(CmdPublish, n.Folder)
|
if len(n.Folder) == 0 {
|
||||||
if len(n.Tag) != 0 {
|
n.Folder = "."
|
||||||
cmd += fmt.Sprintf(" --tag %s", n.Tag)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.Force {
|
f.WriteString(fmt.Sprintf(CmdPublish, n.Folder, n.Tag, n.Folder))
|
||||||
cmd += " --force"
|
|
||||||
}
|
|
||||||
|
|
||||||
f.WriteCmd(cmd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NPM) GetCondition() *condition.Condition {
|
func (n *NPM) GetCondition() *condition.Condition {
|
||||||
|
|
|
@ -31,23 +31,21 @@ func Test_NPM(t *testing.T) {
|
||||||
|
|
||||||
n.Write(b)
|
n.Write(b)
|
||||||
out := b.String()
|
out := b.String()
|
||||||
g.Assert(strings.Contains(out, "\nnpm publish /path/to/repo\n")).Equal(true)
|
g.Assert(strings.Contains(out, "npm publish /path/to/repo\n")).Equal(true)
|
||||||
g.Assert(strings.Contains(out, "\nnpm set")).Equal(false)
|
g.Assert(strings.Contains(out, "\nnpm set")).Equal(false)
|
||||||
g.Assert(strings.Contains(out, "\nnpm config set")).Equal(false)
|
g.Assert(strings.Contains(out, "\nnpm config set")).Equal(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
g.It("Should set force", func() {
|
g.It("Should use current directory if folder is empty", func() {
|
||||||
b := new(buildfile.Buildfile)
|
b := new(buildfile.Buildfile)
|
||||||
n := NPM{
|
n := NPM{
|
||||||
Email: "foo@bar.com",
|
Email: "foo@bar.com",
|
||||||
Username: "foo",
|
Username: "foo",
|
||||||
Password: "bar",
|
Password: "bar",
|
||||||
Folder: "/path/to/repo",
|
|
||||||
Force: true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Write(b)
|
n.Write(b)
|
||||||
g.Assert(strings.Contains(b.String(), "\nnpm publish /path/to/repo --force\n")).Equal(true)
|
g.Assert(strings.Contains(b.String(), "npm publish .\n")).Equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
g.It("Should set tag", func() {
|
g.It("Should set tag", func() {
|
||||||
|
@ -61,7 +59,8 @@ func Test_NPM(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Write(b)
|
n.Write(b)
|
||||||
g.Assert(strings.Contains(b.String(), "\nnpm publish /path/to/repo --tag 1.0.0\n")).Equal(true)
|
g.Assert(strings.Contains(b.String(), "\n_NPM_PACKAGE_TAG=\"1.0.0\"\n")).Equal(true)
|
||||||
|
g.Assert(strings.Contains(b.String(), "npm tag ${_NPM_PACKAGE_NAME} ${_NPM_PACKAGE_TAG}\n")).Equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
g.It("Should set registry", func() {
|
g.It("Should set registry", func() {
|
||||||
|
|
Loading…
Reference in a new issue