Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 10, 2020
1 parent edf70e2 commit 12728bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
10 changes: 5 additions & 5 deletions pkg/commands/dependency.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package commands

type Dependency struct {
Name string
Version string
LinkPath string
Present bool
LocalVersion string
Name string
Version string
LinkPath string
Present bool
PackageConfig *PackageConfig
}

func (d *Dependency) Linked() bool {
Expand Down
15 changes: 5 additions & 10 deletions pkg/commands/npm_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/buger/jsonparser"
"github.com/jesseduffield/lazynpm/pkg/config"
"github.com/jesseduffield/lazynpm/pkg/i18n"
"github.com/jinzhu/copier"
Expand Down Expand Up @@ -42,7 +40,7 @@ func NewNpmManager(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer,
}, nil
}

func (m *NpmManager) UnmarshalPackage(r io.Reader) (*PackageConfig, error) {
func (m *NpmManager) UnmarshalPackageConfig(r io.Reader) (*PackageConfig, error) {
var pkgInput *PackageConfigInput
d := json.NewDecoder(r)
if err := d.Decode(&pkgInput); err != nil {
Expand Down Expand Up @@ -130,8 +128,7 @@ func (m *NpmManager) GetPackages(paths []string) ([]*Package, error) {
m.Log.Error(err)
continue
}
pkgConfig, err := m.UnmarshalPackage(file)

pkgConfig, err := m.UnmarshalPackageConfig(file)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -175,7 +172,6 @@ func (m *NpmManager) ChdirToPackageRoot() (bool, error) {
}

func (m *NpmManager) GetDeps(currentPkg *Package) ([]*Dependency, error) {
// for each dep, check whether it's in node modules
deps := currentPkg.SortedDependencies()

for _, dep := range deps {
Expand All @@ -190,18 +186,17 @@ func (m *NpmManager) GetDeps(currentPkg *Package) ([]*Dependency, error) {

// get the actual version of the package in node modules
packageConfigPath := filepath.Join(nodeModulesPath, "package.json")
bytes, err := ioutil.ReadFile(packageConfigPath)
file, err := os.OpenFile(packageConfigPath, os.O_RDONLY, 0644)
if err != nil {
m.Log.Error(err)
continue
}

localVersion, err := jsonparser.GetString(bytes, "version")
pkgConfig, err := m.UnmarshalPackageConfig(file)
if err != nil {
// swallowing error
m.Log.Error(err)
} else {
dep.LocalVersion = localVersion
dep.PackageConfig = pkgConfig
}

isSymlink := fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink
Expand Down
12 changes: 7 additions & 5 deletions pkg/gui/presentation/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ func GetDependencyListDisplayStrings(dependencies []*commands.Dependency) [][]st
return lines
}

func getDepDisplayStrings(p *commands.Dependency) []string {
func getDepDisplayStrings(d *commands.Dependency) []string {

localVersionCol := utils.ColoredString(p.LocalVersion, color.FgYellow)
if p.Linked() {
localVersionCol = utils.ColoredString("linked: "+p.LinkPath, color.FgCyan)
localVersionCol := ""
if d.Linked() {
localVersionCol = utils.ColoredString("linked: "+d.LinkPath, color.FgCyan)
} else if d.PackageConfig != nil {
localVersionCol = utils.ColoredString(d.PackageConfig.Version, color.FgYellow)
}

return []string{p.Name, utils.ColoredString(p.Version, color.FgMagenta), localVersionCol}
return []string{d.Name, utils.ColoredString(d.Version, color.FgMagenta), localVersionCol}
}

0 comments on commit 12728bc

Please sign in to comment.