diff --git a/plugin/publish/npm/npm.go b/plugin/publish/npm/npm.go index 069396dbd..c51982c10 100644 --- a/plugin/publish/npm/npm.go +++ b/plugin/publish/npm/npm.go @@ -18,8 +18,22 @@ email = %s 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 ( - CmdPublish = "npm publish %s" CmdAlwaysAuth = "npm set always-auth true" CmdSetRegistry = "npm config set registry %s" ) @@ -43,10 +57,6 @@ type NPM struct { // 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"` @@ -94,16 +104,11 @@ func (n *NPM) Write(f *buildfile.Buildfile) { f.WriteCmd(CmdAlwaysAuth) } - var cmd = fmt.Sprintf(CmdPublish, n.Folder) - if len(n.Tag) != 0 { - cmd += fmt.Sprintf(" --tag %s", n.Tag) + if len(n.Folder) == 0 { + n.Folder = "." } - if n.Force { - cmd += " --force" - } - - f.WriteCmd(cmd) + f.WriteString(fmt.Sprintf(CmdPublish, n.Folder, n.Tag, n.Folder)) } func (n *NPM) GetCondition() *condition.Condition { diff --git a/plugin/publish/npm/npm_test.go b/plugin/publish/npm/npm_test.go index 575f249b1..0ea8c4fec 100644 --- a/plugin/publish/npm/npm_test.go +++ b/plugin/publish/npm/npm_test.go @@ -31,23 +31,21 @@ func Test_NPM(t *testing.T) { n.Write(b) 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 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) n := NPM{ Email: "foo@bar.com", Username: "foo", Password: "bar", - Folder: "/path/to/repo", - Force: true, } 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() { @@ -61,7 +59,8 @@ func Test_NPM(t *testing.T) { } 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() {