8
8
"path/filepath"
9
9
"reflect"
10
10
"regexp"
11
+ "sort"
11
12
"strings"
12
13
13
14
"golang.org/x/tools/go/packages"
@@ -19,6 +20,7 @@ import (
19
20
20
21
const (
21
22
baseDir = "./codersdk"
23
+ indent = " "
22
24
)
23
25
24
26
// TODO: Handle httpapi.Response and other types
@@ -44,6 +46,8 @@ type TypescriptTypes struct {
44
46
// String just combines all the codeblocks.
45
47
func (t TypescriptTypes ) String () string {
46
48
var s strings.Builder
49
+ s .WriteString ("// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.\n \n " )
50
+
47
51
sortedTypes := make ([]string , 0 , len (t .Types ))
48
52
sortedEnums := make ([]string , 0 , len (t .Types ))
49
53
@@ -54,6 +58,9 @@ func (t TypescriptTypes) String() string {
54
58
sortedEnums = append (sortedEnums , k )
55
59
}
56
60
61
+ sort .Strings (sortedTypes )
62
+ sort .Strings (sortedEnums )
63
+
57
64
for _ , k := range sortedTypes {
58
65
v := t .Types [k ]
59
66
s .WriteString (v )
@@ -65,7 +72,8 @@ func (t TypescriptTypes) String() string {
65
72
s .WriteString (v )
66
73
s .WriteRune ('\n' )
67
74
}
68
- return s .String ()
75
+
76
+ return strings .TrimRight (s .String (), "\n " )
69
77
}
70
78
71
79
// GenerateFromDirectory will return all the typescript code blocks for a directory
@@ -265,13 +273,13 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
265
273
}
266
274
267
275
if tsType .Comment != "" {
268
- s .WriteString (fmt .Sprintf ("\t // %s\n " , tsType .Comment ))
276
+ s .WriteString (fmt .Sprintf ("%s // %s\n " , indent , tsType .Comment ))
269
277
}
270
278
optional := ""
271
279
if tsType .Optional {
272
280
optional = "?"
273
281
}
274
- s .WriteString (fmt .Sprintf ("\t readonly %s%s: %s\n " , jsonName , optional , tsType .ValueType ))
282
+ s .WriteString (fmt .Sprintf ("%sreadonly %s%s: %s\n " , indent , jsonName , optional , tsType .ValueType ))
275
283
}
276
284
s .WriteString ("}\n " )
277
285
return s .String (), nil
0 commit comments