Skip to content

Commit

Permalink
change some args names
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Dec 26, 2023
1 parent 4a54930 commit 4ca319a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 38 deletions.
6 changes: 3 additions & 3 deletions cmd/.root_test/.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Available Commands:
Flags:
-c, --config string path to dep-tree's config file (default .dep-tree.yml)
--exclude stringArray Files that match this glob pattern will be ignored. You can provide an arbitrary number of --exclude flags
--follow-re-exports follow re-exports while resolving imports between files
-h, --help help for dep-tree
--js-follow-ts-config-paths follow the tsconfig.json paths while resolving imports (default true)
--js-follow-workspaces take the workspaces attribute in the root package.json into account for resolving paths (default true)
--js-tsconfig-paths follow the tsconfig.json paths while resolving imports (default true)
--js-workspaces take the workspaces attribute in the root package.json into account for resolving paths (default true)
--python-exclude-conditional-imports exclude conditional imports while calculating file dependencies, like imports wrapped inside if statements
--unwrap-exports follow re-exports while resolving imports between files
-v, --version version for dep-tree

Use "dep-tree [command] --help" for more information about a command.
6 changes: 3 additions & 3 deletions cmd/.root_test/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Available Commands:
Flags:
-c, --config string path to dep-tree's config file (default .dep-tree.yml)
--exclude stringArray Files that match this glob pattern will be ignored. You can provide an arbitrary number of --exclude flags
--follow-re-exports follow re-exports while resolving imports between files
-h, --help help for dep-tree
--js-follow-ts-config-paths follow the tsconfig.json paths while resolving imports (default true)
--js-follow-workspaces take the workspaces attribute in the root package.json into account for resolving paths (default true)
--js-tsconfig-paths follow the tsconfig.json paths while resolving imports (default true)
--js-workspaces take the workspaces attribute in the root package.json into account for resolving paths (default true)
--python-exclude-conditional-imports exclude conditional imports while calculating file dependencies, like imports wrapped inside if statements
--unwrap-exports follow re-exports while resolving imports between files
-v, --version version for dep-tree

Use "dep-tree [command] --help" for more information about a command.
24 changes: 12 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
)

var configPath string
var jsFollowTsConfigPaths bool
var jsFollowWorkspaces bool
var unwrapExports bool
var jsTsConfigPaths bool
var jsWorkspaces bool
var pythonExcludeConditionalImports bool
var followReExports bool
var exclude []string

var root *cobra.Command
Expand Down Expand Up @@ -54,11 +54,11 @@ func NewRoot(args []string) *cobra.Command {

root.PersistentFlags().StringVarP(&configPath, "config", "c", "", "path to dep-tree's config file (default .dep-tree.yml)")
// TODO: call this '--unwrap-exports'
root.PersistentFlags().BoolVar(&followReExports, "follow-re-exports", false, "follow re-exports while resolving imports between files")
root.PersistentFlags().BoolVar(&unwrapExports, "unwrap-exports", false, "follow re-exports while resolving imports between files")
// TODO: call this '--js-tsconfig-paths'
root.PersistentFlags().BoolVar(&jsFollowTsConfigPaths, "js-follow-ts-config-paths", true, "follow the tsconfig.json paths while resolving imports")
root.PersistentFlags().BoolVar(&jsTsConfigPaths, "js-tsconfig-paths", true, "follow the tsconfig.json paths while resolving imports")
// TODO: call this '--js-workspaces'
root.PersistentFlags().BoolVar(&jsFollowWorkspaces, "js-follow-workspaces", true, "take the workspaces attribute in the root package.json into account for resolving paths")
root.PersistentFlags().BoolVar(&jsWorkspaces, "js-workspaces", true, "take the workspaces attribute in the root package.json into account for resolving paths")
root.PersistentFlags().BoolVar(&pythonExcludeConditionalImports, "python-exclude-conditional-imports", false, "exclude conditional imports while calculating file dependencies, like imports wrapped inside if statements")
root.PersistentFlags().StringArrayVar(&exclude, "exclude", nil, "Files that match this glob pattern will be ignored. You can provide an arbitrary number of --exclude flags")

Expand Down Expand Up @@ -97,14 +97,14 @@ func loadConfig() (*config.Config, error) {
if err != nil {
return nil, err
}
if root.PersistentFlags().Changed("follow-re-exports") {
cfg.FollowReExports = followReExports
if root.PersistentFlags().Changed("unwrap-exports") {
cfg.UnwrapExports = unwrapExports
}
if root.PersistentFlags().Changed("js-follow-ts-config-paths") {
cfg.Js.FollowTsConfigPaths = jsFollowTsConfigPaths
if root.PersistentFlags().Changed("js-tsconfig-paths") {
cfg.Js.TsConfigPaths = jsTsConfigPaths
}
if root.PersistentFlags().Changed("js-follow-workspaces") {
cfg.Js.FollowWorkspaces = jsFollowWorkspaces
if root.PersistentFlags().Changed("js-workspaces") {
cfg.Js.Workspaces = jsWorkspaces
}
if root.PersistentFlags().Changed("python-exclude-conditional-imports") {
cfg.Python.ExcludeConditionalImports = pythonExcludeConditionalImports
Expand Down
31 changes: 18 additions & 13 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bytes"
_ "embed"
"fmt"
"os"
Expand All @@ -21,17 +22,17 @@ const DefaultConfigPath = ".dep-tree.yml"
var SampleConfig string

type Config struct {
Path string
Exclude []string `yaml:"exclude"`
FollowReExports bool `yaml:"followReExports"`
Check check.Config `yaml:"check"`
Js js.Config `yaml:"js"`
Rust rust.Config `yaml:"rust"`
Python python.Config `yaml:"python"`
Path string
Exclude []string `yaml:"exclude"`
UnwrapExports bool `yaml:"unwrapExports"`
Check check.Config `yaml:"check"`
Js js.Config `yaml:"js"`
Rust rust.Config `yaml:"rust"`
Python python.Config `yaml:"python"`
}

func (c *Config) UnwrapProxyExports() bool {
return c.FollowReExports
return c.UnwrapExports
}

func (c *Config) IgnoreFiles() []string {
Expand All @@ -41,10 +42,10 @@ func (c *Config) IgnoreFiles() []string {
func ParseConfig(cfgPath string) (*Config, error) {
// Default values.
cfg := Config{
FollowReExports: false,
UnwrapExports: false,
Js: js.Config{
FollowWorkspaces: true,
FollowTsConfigPaths: true,
Workspaces: true,
TsConfigPaths: true,
},
Python: python.Config{
ExcludeConditionalImports: false,
Expand All @@ -60,6 +61,8 @@ func ParseConfig(cfgPath string) (*Config, error) {
if os.IsNotExist(err) {
if !isDefault {
return &cfg, err
} else {
return &cfg, nil
}
} else if err != nil {
return &cfg, err
Expand All @@ -70,9 +73,11 @@ func ParseConfig(cfgPath string) (*Config, error) {
}
cfg.Path = path.Dir(absCfgPath)

err = yaml.Unmarshal(content, &cfg)
decoder := yaml.NewDecoder(bytes.NewReader(content))
decoder.KnownFields(true)
err = decoder.Decode(&cfg)
if err != nil {
return &cfg, fmt.Errorf(`config file "%s" is not a valid yml file`, cfgPath)
return &cfg, fmt.Errorf(`config file "%s" is not a valid yml file: %w`, cfgPath, err)
}
cfg.Check.Init(path.Dir(absCfgPath))
return &cfg, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/config/sample-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exclude:
#
# Entropy visualization tends to lead to better results if this is set to `false`,
# but CLI rendering is slightly better with this set to `true`.
followReExports: false
unwrapExports: false

# Check configuration for the `dep-tree check` command. Dep Tree will check for dependency
# violation rules declared here, and fail if there is at least one unsatisfied rule.
Expand Down Expand Up @@ -83,11 +83,11 @@ js:
# Whether to take package.json workspaces into account while resolving paths
# or not. You might want to disable if you only want to analyze one workspace
# in a monorepo.
followWorkspaces: true
workspaces: true
# Whether to follow tsconfig.json paths or not. You will typically want to
# enable this, but for some monorepo setups, it might be better to leave this off
# if you want to analyze only one package.
followTsConfigPaths: true
tsConfigPaths: true

# Python specific settings.
python:
Expand Down
4 changes: 2 additions & 2 deletions internal/js/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package js

type Config struct {
FollowTsConfigPaths bool `yaml:"followTsConfigPaths"`
FollowWorkspaces bool `yaml:"followWorkspaces"`
TsConfigPaths bool `yaml:"tsConfigPaths"`
Workspaces bool `yaml:"workspaces"`
}
4 changes: 2 additions & 2 deletions internal/js/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
}

// 2. If is imported from a workspace.
if l.Cfg == nil || l.Cfg.FollowWorkspaces {
if l.Cfg == nil || l.Cfg.Workspaces {
absPath, err = l.Workspaces.ResolveFromWorkspaces(unresolved)
if absPath != "" || err != nil {
return absPath, err
Expand All @@ -51,7 +51,7 @@ func (l *Language) ResolvePath(unresolved string, dir string) (string, error) {
}

// 4. If imported from a path override.
if l.Cfg == nil || l.Cfg.FollowTsConfigPaths {
if l.Cfg == nil || l.Cfg.TsConfigPaths {
absPath, err = tsConfig.ResolveFromPaths(unresolved)
if err != nil {
return "", err
Expand Down

0 comments on commit 4ca319a

Please sign in to comment.