This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add package-lock.json walker, make it multi walker aware
"package-lock.json" walker can force being used thanks to "-only-package-lock" bool CLI option.
- Loading branch information
Showing
17 changed files
with
10,112 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package nodepackage | ||
|
||
// NodePackage represents a package.json (only the interesting fields) | ||
type NodePackage struct { | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
} | ||
|
||
// Walker interface is used to allow multiple backends using this interface to find out dependencies for a given dir | ||
type Walker interface { | ||
Walk(dir string) ([]NodePackage, error) | ||
ErrorContext(error) string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package packagelockrunner | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/nearform/gammaray/nodepackage" | ||
) | ||
|
||
type PackageLock struct { | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
Dependencies PackageLockDependencies `json:"dependencies"` | ||
} | ||
|
||
type PackageLockDependencies map[string]PackageLockDependency | ||
|
||
type PackageLockDependency struct { | ||
Version string `json:"version"` | ||
Dependencies PackageLockDependencies `json:"dependencies"` | ||
} | ||
|
||
// PackageLockRunner used is used as a Walker interface | ||
type PackageLockRunner struct { | ||
directory string | ||
} | ||
|
||
func unwrapDependencies(deps PackageLockDependencies) []nodepackage.NodePackage { | ||
var packageList []nodepackage.NodePackage | ||
|
||
for name, dep := range deps { | ||
packageList = append(packageList, nodepackage.NodePackage{Name: name, Version: dep.Version}) | ||
if dep.Dependencies != nil { | ||
packageList = append(packageList, unwrapDependencies(dep.Dependencies)...) | ||
} | ||
} | ||
return packageList | ||
} | ||
|
||
// ErrorContext tries to give enough context to the user for understanding what walker was impacted by this error | ||
func (self PackageLockRunner) ErrorContext(err error) string { | ||
return "While trying to walk the dependencies from the 'package-lock.json' of " + self.directory | ||
} | ||
|
||
// Walk inspects a folder's package-lock.json to get all the packages used | ||
func (self PackageLockRunner) Walk(dir string) ([]nodepackage.NodePackage, error) { | ||
self.directory = dir | ||
var packageList []nodepackage.NodePackage | ||
|
||
fileInfo, err := os.Stat(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !fileInfo.IsDir() { | ||
return nil, fmt.Errorf("<%s> is not a directory, make sure to put the proper path to your project", dir) | ||
} | ||
packageLockFile := path.Join(dir, "package-lock.json") | ||
jsonFile, err := os.Open(packageLockFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer jsonFile.Close() | ||
|
||
jsonParser := json.NewDecoder(jsonFile) | ||
|
||
var packageLock PackageLock | ||
err = jsonParser.Decode(&packageLock) | ||
if err != nil { | ||
return nil, err | ||
} | ||
packageDeps := unwrapDependencies(packageLock.Dependencies) | ||
|
||
packageList = append(packageList, nodepackage.NodePackage{Name: packageLock.Name, Version: packageLock.Version}) | ||
packageList = append(packageList, packageDeps...) | ||
|
||
return packageList, nil | ||
} |
Oops, something went wrong.