Skip to content

Commit

Permalink
fix: implement more healthly way to access iconsjson string (#17)
Browse files Browse the repository at this point in the history
* Make a more healthly way to access IconsJSON

* Remove lines for readability
  • Loading branch information
hawl1 authored May 11, 2024
1 parent ab7c2f6 commit 2d31066
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
7 changes: 7 additions & 0 deletions api/icons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package handler

// Hey there!
// This file is for build.go to add the icons file as json, since it can make issue on index.go if we do a silly change
// So please dont touch this file unless you know what you're doing.

var IconsJSON string = `{}`
5 changes: 1 addition & 4 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (
var icons map[string]string = make(map[string]string)
var iconNameList []string
var themedIcons []string



var shortNames = map[string]string{
"js": "javascript",
"ts": "typescript",
Expand Down Expand Up @@ -232,7 +229,7 @@ func init() {
}

func Handler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(strings.NewReader(iconsJSON))
decoder := json.NewDecoder(strings.NewReader(IconsJSON))
if err := decoder.Decode(&icons); err != nil {
panic(err)
}
Expand Down
17 changes: 4 additions & 13 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,50 @@ import (
)

func main() {
// Read directory contents
iconsDir, err := os.ReadDir("./assets")
if err != nil {
panic(err)
}

// Initialize map for icons
icons := make(map[string]string)

// Iterate through directory contents
for _, fileInfo := range iconsDir {
if !fileInfo.IsDir() {
// Read file contents
data, err := os.ReadFile("./assets/" + fileInfo.Name())
if err != nil {
panic(err)
}

// Add file content to map
name := strings.TrimSuffix(strings.ToLower(fileInfo.Name()), ".svg")
icons[name] = string(data)
}
}

// Convert icons to JSON
iconsJSON, err := json.Marshal(icons)
if err != nil {
panic(err)
}

// Read content of api/index.go
indexFile, err := os.ReadFile("./api/index.go")
indexFile, err := os.ReadFile("./api/icons.go")
if err != nil {
panic(err)
}

// Convert content to string
indexContent := string(indexFile)

// Split the content by newlines
lines := strings.Split(indexContent, "\n")

// Insert the iconsJSON into line 18
lines[17] = fmt.Sprintf("\n\tvar iconsJSON = `%s`\n", iconsJSON)
lines[6] = fmt.Sprintf("\n\tvar iconsJSON string = `%s`\n", iconsJSON)

// Join the lines back into a single string
modifiedContent := strings.Join(lines, "\n")

// Write the modified content back to the file
err = os.WriteFile("./api/index.go", []byte(modifiedContent), 0644)
err = os.WriteFile("./api/icons.go", []byte(modifiedContent), 0644)
if err != nil {
panic(err)
}

fmt.Println("Modified content saved to api/index.go")
fmt.Println("Modified content saved to api/icons.go")
}

0 comments on commit 2d31066

Please sign in to comment.