kezhenxu94 commented on a change in pull request #52:
URL: https://github.com/apache/skywalking-eyes/pull/52#discussion_r684602822
##########
File path: pkg/deps/npm.go
##########
@@ -206,10 +215,53 @@ func (resolver *NpmResolver) ResolvePkgFile(pkgFile
string) (string, error) {
if err != nil {
return "", err
}
- if packageInfo.License == "" {
- return "", fmt.Errorf("cannot capture the license field")
+
+ if lcs, ok := resolver.ResolveLicenseField(packageInfo.License); ok {
+ return lcs, nil
+ }
+
+ if lcs, ok := resolver.ResolveLicensesField(packageInfo.Licenses); ok {
+ return lcs, nil
+ }
+
+ return "", fmt.Errorf("cannot parse the \"license\"/\"licenses\" field")
+}
+
+// ResolveLicenseField parses and validates the "license" field in
package.json file
+func (resolver *NpmResolver) ResolveLicenseField(rawData []byte) (string,
bool) {
+ if len(rawData) > 0 {
+ switch rawData[0] {
+ case '"':
+ var lcs string
+ _ = json.Unmarshal(rawData, &lcs)
+ if lcs != "" {
+ return lcs, true
+ }
+ case '{':
+ var lcs Lcs
+ _ = json.Unmarshal(rawData, &lcs)
+ if t := lcs.Type; t != "" {
+ return t, true
+ }
+ }
+ }
+ return "", false
+}
+
+// ResolveLicensesField parses and validates the "licenses" field in
package.json file
+// Additionally, the output is converted into the SPDX license expression
syntax version 2.0 string, like "(ISC OR GPL-3.0)"
+func (resolver *NpmResolver) ResolveLicensesField(licenses []Lcs) (string,
bool) {
Review comment:
Let's remove `()` in the licenses and simply this
```go
var lcs []string
for _, l := range licenses {
lcs = append(lcs, l.Type)
}
if len(lcs) == 0 {
return "", false
}
return strings.Join(lcs, " OR "), true
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]