Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement more healthly way to access to iconsjson string #17

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 Handler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(strings.NewReader(iconsJSON))
decoder := json.NewDecoder(strings.NewReader(IconsJSON))

Check failure on line 232 in api/index.go

View workflow job for this annotation

GitHub Actions / Build-Attempt

undefined: 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")
}
Loading