/
parser.go
63 lines (49 loc) · 1.67 KB
/
parser.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
60
61
62
63
package gazelle
import (
"fmt"
"log"
"os"
"regexp"
semver "github.com/Masterminds/semver/v3"
)
type WorkspacePackageVersionMap map[string]map[string]string
/* Parse a lockfile and return a map of workspace projects to a map of dependency name to version.
*/
func ParsePnpmLockFileDependencies(lockfilePath string) WorkspacePackageVersionMap {
yamlFileContent, readErr := os.ReadFile(lockfilePath)
if readErr != nil {
log.Fatalf("failed to read lockfile '%s': %s", lockfilePath, readErr.Error())
}
deps, err := parsePnpmLockDependencies(yamlFileContent)
if err != nil {
log.Fatalf("pnpm parse - %v\n", err)
}
return deps
}
var lockVersionRegex = regexp.MustCompile(`^\s*lockfileVersion: '?(?P<Version>\d\.\d)'?`)
func parsePnpmLockVersion(yamlFileContent []byte) (string, error) {
match := lockVersionRegex.FindStringSubmatch(string(yamlFileContent))
if len(match) != 2 {
return "", fmt.Errorf("failed to find lockfile version in: %q", string(yamlFileContent))
}
return match[1], nil
}
func parsePnpmLockDependencies(yamlFileContent []byte) (WorkspacePackageVersionMap, error) {
if len(yamlFileContent) == 0 {
return WorkspacePackageVersionMap{}, nil
}
versionStr, versionErr := parsePnpmLockVersion(yamlFileContent)
if versionErr != nil {
return nil, versionErr
}
version, versionErr := semver.NewVersion(versionStr)
if versionErr != nil {
return nil, fmt.Errorf("failed to parse semver %q: %v", versionStr, versionErr)
}
if version.Major() == 5 {
return parsePnpmLockDependenciesV5(yamlFileContent)
} else if version.Major() == 6 {
return parsePnpmLockDependenciesV6(yamlFileContent)
}
return nil, fmt.Errorf("unsupported version: %v", versionStr)
}