forked from gzuidhof/tygo
/
config.go
97 lines (78 loc) · 2.54 KB
/
config.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
package tygo
import (
"log"
"path/filepath"
"strings"
)
const defaultOutputFilename = "index.ts"
type PackageConfig struct {
// The package path just like you would import it in Go
Path string `yaml:"path"`
// Where this output should be written to.
// If you specify a folder it will be written to a file `index.ts` within that folder. By default it is written into the Golang package folder.
OutputPath string `yaml:"output_path"`
// Customize the indentation (use \t if you want tabs)
Indent string `yaml:"indent"`
// Specify your own custom type translations, useful for custom types, `time.Time` and `null.String`.
// Be default unrecognized types will be output as `any /* name */`.
TypeMappings map[string]string `yaml:"type_mappings"`
// This content will be put at the top of the output Typescript file.
// You would generally use this to import custom types.
Frontmatter string `yaml:"frontmatter"`
// Filenames of Go source files that should not be included in the Typescript output.
ExcludeFiles []string `yaml:"exclude_files"`
// Filenames of Go source files that should be included in the Typescript output.
IncludeFiles []string `yaml:"include_files"`
// FallbackType defines the Typescript type used as a fallback for unknown Go types.
FallbackType string `yaml:"fallback_type"`
// IncludeStructNames
IncludeStructComment string `yaml:"include_struct_comment"`
}
type Config struct {
Packages []*PackageConfig `yaml:"packages"`
}
func (c Config) PackageNames() []string {
names := make([]string, len(c.Packages))
for i, p := range c.Packages {
names[i] = p.Path
}
return names
}
func (c Config) PackageConfig(packagePath string) *PackageConfig {
for _, pc := range c.Packages {
if pc.Path == packagePath {
if pc.Indent == "" {
pc.Indent = " "
}
return pc
}
}
log.Fatalf("Config not found for package %s", packagePath)
return nil
}
func (c PackageConfig) IsFileIgnored(pathToFile string) bool {
basename := filepath.Base(pathToFile)
for _, ef := range c.ExcludeFiles {
if basename == ef {
return true
}
}
// if defined, only included files are allowed
if len(c.IncludeFiles) > 0 {
for _, include := range c.IncludeFiles {
if basename == include {
return false
}
}
return true
}
return false
}
func (c PackageConfig) ResolvedOutputPath(packageDir string) string {
if c.OutputPath == "" {
return filepath.Join(packageDir, defaultOutputFilename)
} else if !strings.HasSuffix(c.OutputPath, ".ts") {
return filepath.Join(c.OutputPath, defaultOutputFilename)
}
return c.OutputPath
}