PWA Manifest Generator — Go source
Assemble a valid Web App Manifest — name, colors, display mode, icon sizes, maskable purpose — with a stable key order and instant JSON you can copy or download.
This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Package pwamanifestgenerator is the Go twin of CosmoDev's
// src/lib/pwa-manifest.ts (dual source: the web lib is TypeScript, the CLI lib
// is Go — kept in lock-step). Pure + deterministic, never panics. The
// table-driven tests in pwa-manifest-generator_test.go share vectors with
// src/lib/pwa-manifest.test.ts so the two implementations are held to the same
// contract.
//
// Behaviour mirrors the TS lib exactly: build the manifest document with a
// fixed key order (name, short_name, description, start_url, display,
// orientation, background_color, theme_color, icons). Blank optional fields
// are omitted; icon sizes default to 192 and 512 and emit one entry each.
// Hard errors (missing name, non-hex colors, invalid icon sizes) suppress
// emission; the short_name >12 warning never blocks.
package pwamanifestgenerator
import (
"fmt"
"regexp"
"strings"
)
// ManifestInput mirrors the ManifestInput interface in src/lib/pwa-manifest.ts.
// The zero value (ManifestInput{}) matches buildManifest({}): no name, default
// icon sizes [192 512], purpose "any". Display and Orientation are plain
// strings so an empty value means "absent", exactly like the TS optional
// fields.
type ManifestInput struct {
Name string
ShortName string
Description string
StartURL string
Display string // e.g. "browser", "standalone", "minimal-ui", "fullscreen"
Orientation string // e.g. "any", "portrait", "landscape"
BackgroundColor string
ThemeColor string
// IconSizes are icon edge sizes in px; nil (the zero value) falls back to
// the TS default [192, 512], while an explicit empty slice emits an empty
// icons array.
IconSizes []int
// Maskable marks every icon purpose "maskable" instead of "any".
Maskable bool
}
// ManifestResult mirrors the ManifestResult interface in src/lib/pwa-manifest.ts.
type ManifestResult struct {
OK bool
Errors []string
Warnings []string
JSON string
}
// hexColor matches the TS HEX = /^#[0-9a-fA-F]{6}$/.
var hexColor = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
// defaultIconSizes mirrors the TS `input.iconSizes ?? [192, 512]` fallback.
var defaultIconSizes = []int{192, 512}
// BuildManifest is the Go twin of buildManifest() in src/lib/pwa-manifest.ts
// and must agree with it on every shared vector: form fields in, a
// stable-key-order manifest document out.
func BuildManifest(input ManifestInput) ManifestResult {
errors := []string{}
warnings := []string{}
name := strings.TrimSpace(input.Name)
shortName := strings.TrimSpace(input.ShortName)
description := strings.TrimSpace(input.Description)
startURL := strings.TrimSpace(input.StartURL)
backgroundColor := strings.TrimSpace(input.BackgroundColor)
themeColor := strings.TrimSpace(input.ThemeColor)
sizes := input.IconSizes
// nil (the zero value) means "not provided" → TS default [192, 512]; an
// explicit empty slice is kept, emitting an empty icons array — the same
// distinction the TS lib draws with `??`.
if sizes == nil {
sizes = defaultIconSizes
}
if name == "" {
errors = append(errors, "name is required.")
}
if backgroundColor != "" && !hexColor.MatchString(backgroundColor) {
errors = append(errors, "background_color must be a #rrggbb hex value.")
}
if themeColor != "" && !hexColor.MatchString(themeColor) {
errors = append(errors, "theme_color must be a #rrggbb hex value.")
}
for _, s := range sizes {
if s <= 0 {
errors = append(errors, "icon sizes must be positive integers.")
break
}
}
seenSizes := map[int]bool{}
for _, s := range sizes {
if seenSizes[s] {
errors = append(errors, fmt.Sprintf("icon size %d is listed twice.", s))
}
seenSizes[s] = true
}
if len(errors) > 0 {
return ManifestResult{OK: false, Errors: errors, Warnings: []string{}, JSON: ""}
}
if len([]rune(shortName)) > 12 {
warnings = append(warnings, fmt.Sprintf("short_name is %d characters — home-screen labels truncate around 12.", len([]rune(shortName))))
}
var lines []string
add := func(key, value string) {
lines = append(lines, " "+jsonString(key)+": "+jsonString(value))
}
add("name", name)
if shortName != "" {
add("short_name", shortName)
}
if description != "" {
add("description", description)
}
if startURL != "" {
add("start_url", startURL)
}
if input.Display != "" {
add("display", input.Display)
}
if input.Orientation != "" {
add("orientation", input.Orientation)
}
if backgroundColor != "" {
add("background_color", backgroundColor)
}
if themeColor != "" {
add("theme_color", themeColor)
}
purpose := "any"
if input.Maskable {
purpose = "maskable"
}
var iconEntries []string
for _, s := range sizes {
iconEntries = append(iconEntries,
" {\n"+
" \"src\": "+jsonString(fmt.Sprintf("icons/icon-%dx%d.png", s, s))+",\n"+
" \"sizes\": "+jsonString(fmt.Sprintf("%dx%d", s, s))+",\n"+
" \"type\": "+jsonString("image/png")+",\n"+
" \"purpose\": "+jsonString(purpose)+"\n"+
" }")
}
var iconsLine string
if len(iconEntries) == 0 {
iconsLine = " \"icons\": []" // JSON.stringify renders an empty array inline
} else {
iconsLine = " \"icons\": [\n" + strings.Join(iconEntries, ",\n") + "\n ]"
}
lines = append(lines, iconsLine)
return ManifestResult{
OK: true,
Errors: errors,
Warnings: warnings,
JSON: "{\n" + strings.Join(lines, ",\n") + "\n}\n",
}
}
// jsonString double-quotes s exactly like JavaScript's JSON.stringify: escape
// ", \, and the control characters (short escapes for \b \f \n \r \t, \u00xx
// for the rest), leave everything else — including non-ASCII — untouched.
// encoding/json is not used because it HTML-escapes <, >, & by default.
func jsonString(s string) string {
var b strings.Builder
b.WriteByte('"')
for _, r := range s {
switch r {
case '"':
b.WriteString(`\"`)
case '\\':
b.WriteString(`\\`)
case '\b':
b.WriteString(`\b`)
case '\f':
b.WriteString(`\f`)
case '\n':
b.WriteString(`\n`)
case '\r':
b.WriteString(`\r`)
case '\t':
b.WriteString(`\t`)
default:
if r < 0x20 {
fmt.Fprintf(&b, `\u%04x`, r)
} else {
b.WriteRune(r)
}
}
}
b.WriteByte('"')
return b.String()
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →