-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
47 lines (40 loc) · 1.01 KB
/
writer.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
package writer
import (
"github.com/specgen-io/specgen/v2/goven/generator"
"github.com/specgen-io/specgen/v2/goven/typescript/module"
"strings"
)
func TsConfig() generator.Config {
return generator.Config{" ", 2, nil}
}
type Writer struct {
generator.Writer
filename string
module module.Module
Imports *imports
}
func New(module module.Module) *Writer {
return &Writer{
generator.NewWriter(TsConfig()),
module.GetPath(),
module,
NewImports(module),
}
}
func (w *Writer) Indented() *Writer {
return &Writer{w.Writer.Indented(), w.filename, w.module, w.Imports}
}
func (w *Writer) IndentedWith(size int) *Writer {
return &Writer{w.Writer.IndentedWith(size), w.filename, w.module, w.Imports}
}
func (w *Writer) ToCodeFile() *generator.CodeFile {
lines := []string{}
imports := w.Imports.Lines()
if len(imports) > 0 {
lines = append(lines, imports...)
lines = append(lines, "")
}
lines = append(lines, w.Code()...)
code := strings.Join(lines, "\n")
return &generator.CodeFile{w.filename, code}
}