-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManifest.go
77 lines (63 loc) · 2.01 KB
/
Manifest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package manifest
import (
"os"
jsoniter "github.com/json-iterator/go"
)
// Manifest represents a web manifest
type Manifest struct {
Name string `json:"name"`
ShortName string `json:"short_name"`
Description string `json:"description,omitempty"`
Icons []Icon `json:"icons,omitempty"`
StartURL string `json:"start_url"`
Display string `json:"display"`
Orientation string `json:"orientation,omitempty"`
Language string `json:"lang,omitempty"`
ThemeColor string `json:"theme_color,omitempty"`
BackgroundColor string `json:"background_color,omitempty"`
TextDirection string `json:"dir,omitempty"`
ServiceWorker *ServiceWorker `json:"serviceworker,omitempty"`
ScreenShots []ScreenShot `json:"screenshots,omitempty"`
}
// Icon represents a single icon in the web manifest.
type Icon struct {
Source string `json:"src"`
Sizes string `json:"sizes"`
Type string `json:"type,omitempty"`
}
// ServiceWorker represents a service worker definition in the web manifest.
type ServiceWorker struct {
Source string `json:"src"`
Scope string `json:"scope"`
UpdateViaCache string `json:"update_via_cache"`
}
// ScreenShot represents a screenshot definition in the web manifest.
type ScreenShot = Icon
// New creates a new manifest.
func New() *Manifest {
return &Manifest{
StartURL: "/",
Display: "standalone",
Language: "en",
ShortName: "Untitled",
}
}
// FromFile creates a new manifest.
func FromFile(fileName string) (*Manifest, error) {
// Open the file
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
// Decode JSON
webManifest := New()
decoder := jsoniter.NewDecoder(file)
err = decoder.Decode(webManifest)
if err != nil {
return nil, err
}
return webManifest, nil
}
// TODO: Warn about short name length (Google Lighthouse)
// https://developer.chrome.com/apps/manifest/name#short_name