mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
refactor: deduplicate / type license feature code (#5734)
This commit is contained in:
@@ -63,7 +63,11 @@ type TypescriptTypes struct {
|
||||
// String just combines all the codeblocks.
|
||||
func (t TypescriptTypes) String() string {
|
||||
var s strings.Builder
|
||||
_, _ = s.WriteString("// Code generated by 'make site/src/api/typesGenerated.ts'. DO NOT EDIT.\n\n")
|
||||
const prelude = `
|
||||
// Code generated by 'make site/src/api/typesGenerated.ts'. DO NOT EDIT.
|
||||
|
||||
`
|
||||
_, _ = s.WriteString(prelude)
|
||||
|
||||
sortedTypes := make([]string, 0, len(t.Types))
|
||||
sortedEnums := make([]string, 0, len(t.Enums))
|
||||
@@ -223,6 +227,18 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
|
||||
name, strings.Join(values, " | "),
|
||||
))
|
||||
|
||||
var pluralName string
|
||||
if strings.HasSuffix(name, "s") {
|
||||
pluralName = name + "es"
|
||||
} else {
|
||||
pluralName = name + "s"
|
||||
}
|
||||
|
||||
// Generate array used for enumerating all possible values.
|
||||
_, _ = s.WriteString(fmt.Sprintf("export const %s: %s[] = [%s]\n",
|
||||
pluralName, name, strings.Join(values, ", "),
|
||||
))
|
||||
|
||||
enumCodeBlocks[name] = s.String()
|
||||
}
|
||||
|
||||
@@ -644,6 +660,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
|
||||
aboveTypeLine = aboveTypeLine + "\n"
|
||||
}
|
||||
aboveTypeLine = aboveTypeLine + valueType.AboveTypeLine
|
||||
|
||||
return TypescriptType{
|
||||
ValueType: fmt.Sprintf("Record<%s, %s>", keyType.ValueType, valueType.ValueType),
|
||||
AboveTypeLine: aboveTypeLine,
|
||||
|
||||
+1
@@ -2,3 +2,4 @@
|
||||
|
||||
// From codersdk/enums.go
|
||||
export type Enum = "bar" | "baz" | "foo" | "qux"
|
||||
export const Enums: Enum[] = ["bar", "baz", "foo", "qux"]
|
||||
|
||||
Reference in New Issue
Block a user