2023-01-07 07:15:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func dateTypes(names []string) map[*regexp.Regexp]string {
|
|
|
|
result := make(map[*regexp.Regexp]string)
|
|
|
|
for _, name := range names {
|
|
|
|
result[regexp.MustCompile(fmt.Sprintf(`%s: string`, name))] = fmt.Sprintf(`%s: Date`, name)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) != 2 {
|
|
|
|
fmt.Println("Please provide a file path as an argument")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := os.Args[1]
|
|
|
|
|
|
|
|
fmt.Printf("Processing %s\n", path)
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
fmt.Printf("File %s does not exist\n", path)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
text := "/* post-processed by ./scripts/process-types.go */\n"
|
2023-01-19 05:44:06 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2023-01-07 07:15:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
text += string(data)
|
|
|
|
|
|
|
|
regexReplace := map[*regexp.Regexp]string{
|
|
|
|
regexp.MustCompile(` PaginationResultRepo`): " PaginationResult",
|
|
|
|
regexp.MustCompile(` Repo`): " ",
|
|
|
|
regexp.MustCompile(` Services`): " ",
|
|
|
|
regexp.MustCompile(` V1`): " ",
|
|
|
|
regexp.MustCompile(`\?:`): ":",
|
|
|
|
}
|
|
|
|
|
|
|
|
for regex, replace := range dateTypes([]string{
|
|
|
|
"createdAt",
|
|
|
|
"updatedAt",
|
|
|
|
"soldTime",
|
|
|
|
"purchaseTime",
|
|
|
|
"warrantyExpires",
|
|
|
|
"expiresAt",
|
|
|
|
"date",
|
|
|
|
}) {
|
|
|
|
regexReplace[regex] = replace
|
|
|
|
}
|
|
|
|
|
|
|
|
for regex, replace := range regexReplace {
|
|
|
|
fmt.Printf("Replacing '%v' -> '%s'\n", regex, replace)
|
|
|
|
text = regex.ReplaceAllString(text, replace)
|
|
|
|
}
|
|
|
|
|
2023-01-19 05:44:06 +00:00
|
|
|
err = os.WriteFile(path, []byte(text), 0644)
|
2023-01-07 07:15:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|