-
Notifications
You must be signed in to change notification settings - Fork 55
/
pack.go
59 lines (53 loc) · 1.76 KB
/
pack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)
}
func createPackCmdConfig(executablePath string, splitFlags []string) *npmutils.NpmConfig {
return &npmutils.NpmConfig{
Npm: executablePath,
Command: []string{"pack"},
CommandFlags: append(splitFlags, "--json=false"),
StrWriter: nil,
ErrWriter: nil,
}
}
// 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
}