Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Mar 10, 2024
1 parent 15eda5f commit e9ebbfa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
49 changes: 25 additions & 24 deletions artifactory/commands/npm/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type NpmPublishCommandArgs struct {
executablePath string
workingDirectory string
collectBuildInfo bool
packedFilesPath []string
packedFilePaths []string
packageInfo *biutils.PackageInfo
publishPath string
tarballProvided bool
Expand Down Expand Up @@ -173,11 +173,11 @@ func (npc *NpmPublishCommand) Run() (err error) {
return err
}
// We should delete the tarball we created
return deleteCreatedTarballAndError(npc.packedFilesPath, err)
return deleteCreatedTarballAndError(npc.packedFilePaths, err)
}

if !npc.tarballProvided {
if err := deleteCreatedTarball(npc.packedFilesPath); err != nil {
if err := deleteCreatedTarball(npc.packedFilePaths); err != nil {
return err
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (npc *NpmPublishCommand) CommandName() string {
}

func (npc *NpmPublishCommand) preparePrerequisites() error {
npc.packedFilesPath = make([]string, 0)
npc.packedFilePaths = make([]string, 0)
currentDir, err := os.Getwd()
if err != nil {
return errorutils.CheckError(err)
Expand Down Expand Up @@ -263,9 +263,8 @@ func (npc *NpmPublishCommand) pack() error {
return err
}

for index, packageFileName := range packedFileNames {
npc.packedFilesPath = append(npc.packedFilesPath, filepath.Join(tarballDir, packageFileName))
log.Debug("Created npm package at", npc.packedFilesPath[index])
for _, packageFileName := range packedFileNames {
npc.packedFilePaths = append(npc.packedFilePaths, filepath.Join(tarballDir, packageFileName))
}

return nil
Expand All @@ -285,7 +284,7 @@ func (npc *NpmPublishCommand) getTarballDir() (string, error) {
}

func (npc *NpmPublishCommand) publish() (err error) {
for _, packedFilePath := range npc.packedFilesPath {
for _, packedFilePath := range npc.packedFilePaths {
log.Debug("Deploying npm package.")
if err = npc.readPackageInfoFromTarball(packedFilePath); err != nil {
return
Expand Down Expand Up @@ -348,18 +347,11 @@ func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerD
}
}
if npc.detailedSummary {
npc.result.SetFailCount(npc.result.FailCount() + totalFailed)
npc.result.SetSuccessCount(npc.result.SuccessCount() + summary.TotalSucceeded)
if npc.result.Reader() == nil {
npc.result.SetReader(summary.TransferDetailsReader)
} else {
if err = npc.appendReader(summary); err != nil {
return err
}
if err = npc.setDetailedSummary(summary); err != nil {
return err
}
} else {
err = summary.TransferDetailsReader.Close()
if err != nil {
if err = summary.TransferDetailsReader.Close(); err != nil {
return err
}
}
Expand All @@ -377,6 +369,19 @@ func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerD
return nil
}

func (npc *NpmPublishCommand) setDetailedSummary(summary *specutils.OperationSummary) (err error) {
npc.result.SetFailCount(npc.result.FailCount() + summary.TotalFailed)
npc.result.SetSuccessCount(npc.result.SuccessCount() + summary.TotalSucceeded)
if npc.result.Reader() == nil {
npc.result.SetReader(summary.TransferDetailsReader)
} else {
if err = npc.appendReader(summary); err != nil {
return
}
}
return
}

func (npc *NpmPublishCommand) appendReader(summary *specutils.OperationSummary) error {
readersSlice := []*content.ContentReader{npc.result.Reader(), summary.TransferDetailsReader}
reader, err := content.MergeReaders(readersSlice, content.DefaultKey)
Expand Down Expand Up @@ -421,7 +426,7 @@ func (npc *NpmPublishCommand) setPackageInfo() error {
}

func (npc *NpmPublishCommand) readPackageInfoFromTarball(packedFilePath string) (err error) {
log.Debug("Extracting info from npm package:", npc.packedFilesPath)
log.Debug("Extracting info from npm package:", npc.packedFilePaths)
tarball, err := os.Open(packedFilePath)
if err != nil {
return errorutils.CheckError(err)
Expand Down Expand Up @@ -459,11 +464,7 @@ func (npc *NpmPublishCommand) readPackageInfoFromTarball(packedFilePath string)
}

func deleteCreatedTarballAndError(packedFilesPath []string, currentError error) error {
if err := deleteCreatedTarball(packedFilesPath); err != nil {
errorText := fmt.Sprintf("Two errors occurred: \n%s \n%s", currentError, err)
return errorutils.CheckErrorf(errorText)
}
return currentError
return errors.Join(currentError, deleteCreatedTarball(packedFilesPath))
}

func deleteCreatedTarball(packedFilesPath []string) error {
Expand Down
8 changes: 4 additions & 4 deletions artifactory/commands/npm/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (

func TestReadPackageInfoFromTarball(t *testing.T) {
npmPublish := NewNpmPublishCommand()
npmPublish.packedFilesPath = append(npmPublish.packedFilesPath, filepath.Join("..", "testdata", "npm", "npm-example-0.0.3.tgz"))
npmPublish.packedFilesPath = append(npmPublish.packedFilesPath, filepath.Join("..", "testdata", "npm", "npm-example-0.0.4.tgz"))
npmPublish.packedFilePaths = append(npmPublish.packedFilePaths, filepath.Join("..", "testdata", "npm", "npm-example-0.0.3.tgz"))
npmPublish.packedFilePaths = append(npmPublish.packedFilePaths, filepath.Join("..", "testdata", "npm", "npm-example-0.0.4.tgz"))

err := npmPublish.readPackageInfoFromTarball(npmPublish.packedFilesPath[0])
err := npmPublish.readPackageInfoFromTarball(npmPublish.packedFilePaths[0])
assert.NoError(t, err)
assert.Equal(t, "npm-example", npmPublish.packageInfo.Name)
assert.Equal(t, "0.0.3", npmPublish.packageInfo.Version)

err = npmPublish.readPackageInfoFromTarball(npmPublish.packedFilesPath[1])
err = npmPublish.readPackageInfoFromTarball(npmPublish.packedFilePaths[1])
assert.NoError(t, err)
assert.Equal(t, "npm-example", npmPublish.packageInfo.Name)
assert.Equal(t, "0.0.4", npmPublish.packageInfo.Version)
Expand Down

0 comments on commit e9ebbfa

Please sign in to comment.