-
Notifications
You must be signed in to change notification settings - Fork 18
/
parser_v6.go
111 lines (86 loc) · 2.3 KB
/
parser_v6.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package gazelle
import (
"fmt"
"gopkg.in/yaml.v3"
)
func parsePnpmLockDependenciesV6(yamlFileContent []byte) (WorkspacePackageVersionMap, error) {
lockfile := PnpmLockfileV6{}
unmarshalErr := yaml.Unmarshal(yamlFileContent, &lockfile)
if unmarshalErr != nil {
return nil, fmt.Errorf("parse error: %v", unmarshalErr)
}
result := make(WorkspacePackageVersionMap)
// Pnpm workspace lockfiles contain a list of projects under Importers, including
// the root project as the "." importer.
if lockfile.Importers != nil {
for pkg, pkgDeps := range lockfile.Importers {
result[pkg] = mergeDependenciesV6(pkgDeps.Dependencies, pkgDeps.DevDependencies, pkgDeps.PeerDependencies, pkgDeps.OptionalDependencies)
}
} else {
// Non-workspace lockfiles have one set of dependencies at the root
result["."] = mergeDependenciesV6(lockfile.Dependencies, lockfile.DevDependencies, lockfile.PeerDependencies, lockfile.OptionalDependencies)
}
return result, nil
}
/*
Example v6 pnpm-lock.yaml without workspaces
```
lockfileVersion: '6.0'
dependencies:
'@aspect-test/c':
specifier: ^2.0.2
version: 2.0.2
devDependencies:
jquery:
specifier: 3.6.1
version: 3.6.1
packages:
/@aspect-test/c@2.0.2:
...
...
...
```
or with pnpm-workspace.yaml:
```
lockfileVersion: '6.0'
importers:
.:
dependencies:
'@aspect-test/c':
specifier: ^2.0.2
version: 2.0.2
devDependencies:
jquery:
specifier: 3.6.1
version: 3.6.1
packages:
/@aspect-test/c@2.0.2:
...
...
...
```
*/
type packageInfoV6 = map[string]struct {
Version string
}
type PnpmLockfileV6 struct {
Dependencies packageInfoV6
DevDependencies packageInfoV6 `yaml:"devDependencies"`
PeerDependencies packageInfoV6 `yaml:"peerDependencies"`
OptionalDependencies packageInfoV6 `yaml:"optionalDependencies"`
Importers map[string]struct {
Dependencies packageInfoV6
DevDependencies packageInfoV6 `yaml:"devDependencies"`
PeerDependencies packageInfoV6 `yaml:"peerDependencies"`
OptionalDependencies packageInfoV6 `yaml:"optionalDependencies"`
}
}
func mergeDependenciesV6(d ...packageInfoV6) map[string]string {
result := make(map[string]string)
for _, m := range d {
for k, v := range m {
result[k] = v.Version
}
}
return result
}