Skip to content

Commit

Permalink
Fix npm packed tarball files identification (#1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea authored Apr 15, 2024
1 parent 13680c0 commit d5bc1eb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
34 changes: 30 additions & 4 deletions artifactory/utils/npm/pack.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package npm

import (
"fmt"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"strings"

gofrogcmd "github.com/jfrog/gofrog/io"
npmutils "github.com/jfrog/jfrog-cli-core/v2/utils/npm"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

const (
npmPackedTarballSuffix = ".tgz"
)

func Pack(npmFlags []string, executablePath string) ([]string, error) {
configListCmdConfig := createPackCmdConfig(executablePath, npmFlags)
output, err := gofrogcmd.RunCmdOutput(configListCmdConfig)
if err != nil {
return []string{}, errorutils.CheckError(err)
}
return getPackageFileNameFromOutput(output), nil
return getPackageFileNameFromOutput(output)
}

func createPackCmdConfig(executablePath string, splitFlags []string) *npmutils.NpmConfig {
Expand All @@ -27,7 +33,27 @@ func createPackCmdConfig(executablePath string, splitFlags []string) *npmutils.N
}
}

func getPackageFileNameFromOutput(output string) []string {
output = strings.TrimSpace(output)
return strings.Split(output, "\n")
// Extracts packed file names from npm pack command output
// The output can differ when a prePack script exists,
// This function will filter the output and search for the .tgz files
// To avoid misidentifying files, we will verify file exists
func getPackageFileNameFromOutput(output string) (packedTgzFiles []string, err error) {
lines := strings.Split(output, "\n")
var packedFileNamesFromOutput []string
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasSuffix(line, npmPackedTarballSuffix) {
packedFileNamesFromOutput = append(packedFileNamesFromOutput, line)
}
}
for _, file := range packedFileNamesFromOutput {
exists, err := fileutils.IsFileExists(file, true)
if err != nil {
return nil, fmt.Errorf("error occurred while checking packed npm tarball exists: %w", err)
}
if exists {
packedTgzFiles = append(packedTgzFiles, file)
}
}
return
}
3 changes: 2 additions & 1 deletion tests/testdata/npm-workspaces/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"prepack": "echo 'prepack script executed'"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit d5bc1eb

Please sign in to comment.