github.com/jfrog/jfrog-cli-core/v2@v2.52.0/artifactory/utils/npm/pack.go (about) 1 package npm 2 3 import ( 4 "fmt" 5 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 6 "strings" 7 8 gofrogcmd "github.com/jfrog/gofrog/io" 9 npmutils "github.com/jfrog/jfrog-cli-core/v2/utils/npm" 10 "github.com/jfrog/jfrog-client-go/utils/errorutils" 11 ) 12 13 const ( 14 npmPackedTarballSuffix = ".tgz" 15 ) 16 17 func Pack(npmFlags []string, executablePath string) ([]string, error) { 18 configListCmdConfig := createPackCmdConfig(executablePath, npmFlags) 19 output, err := gofrogcmd.RunCmdOutput(configListCmdConfig) 20 if err != nil { 21 return []string{}, errorutils.CheckError(err) 22 } 23 return getPackageFileNameFromOutput(output) 24 } 25 26 func createPackCmdConfig(executablePath string, splitFlags []string) *npmutils.NpmConfig { 27 return &npmutils.NpmConfig{ 28 Npm: executablePath, 29 Command: []string{"pack"}, 30 CommandFlags: append(splitFlags, "--json=false"), 31 StrWriter: nil, 32 ErrWriter: nil, 33 } 34 } 35 36 // Extracts packed file names from npm pack command output 37 // The output can differ when a prePack script exists, 38 // This function will filter the output and search for the .tgz files 39 // To avoid misidentifying files, we will verify file exists 40 func getPackageFileNameFromOutput(output string) (packedTgzFiles []string, err error) { 41 lines := strings.Split(output, "\n") 42 var packedFileNamesFromOutput []string 43 for _, line := range lines { 44 line = strings.TrimSpace(line) 45 if strings.HasSuffix(line, npmPackedTarballSuffix) { 46 packedFileNamesFromOutput = append(packedFileNamesFromOutput, line) 47 } 48 } 49 for _, file := range packedFileNamesFromOutput { 50 exists, err := fileutils.IsFileExists(file, true) 51 if err != nil { 52 return nil, fmt.Errorf("error occurred while checking packed npm tarball exists: %w", err) 53 } 54 if exists { 55 packedTgzFiles = append(packedTgzFiles, file) 56 } 57 } 58 return 59 }