MCPcopy
hub / github.com/esm-dev/esm.sh / updateImportMap

Function updateImportMap

cli/command_add.go:85–190  ·  view source on GitHub ↗
(specifiers []string, all bool, noPrompt bool, noSRI bool)

Source from the content-addressed store, hash-verified

83}
84
85func updateImportMap(specifiers []string, all bool, noPrompt bool, noSRI bool) (err error) {
86 indexHtml, exists, err := lookupClosestFile("index.html")
87 if err != nil {
88 return
89 }
90
91 if exists {
92 var f *os.File
93 f, err = os.Open(indexHtml)
94 if err != nil {
95 return
96 }
97 tokenizer := html.NewTokenizer(f)
98 buf := bytes.NewBuffer(nil)
99 updated := false
100 for {
101 token := tokenizer.Next()
102 if token == html.ErrorToken && tokenizer.Err() == io.EOF {
103 break
104 }
105 if token == html.EndTagToken {
106 tagName, _ := tokenizer.TagName()
107 if string(tagName) == "head" && !updated {
108 buf.WriteString(" <script type=\"importmap\">\n")
109 var importMap importmap.ImportMap
110 if addImports(&importMap, specifiers, all, noPrompt, noSRI) {
111 buf.WriteString(importMap.FormatJSON(2))
112 buf.WriteString("\n </script>\n")
113 }
114 buf.Write(tokenizer.Raw())
115 updated = true
116 continue
117 }
118 }
119 if token == html.StartTagToken {
120 tagName, moreAttr := tokenizer.TagName()
121 if string(tagName) == "script" && moreAttr {
122 var typeAttr string
123 for moreAttr {
124 var key, val []byte
125 key, val, moreAttr = tokenizer.TagAttr()
126 if string(key) == "type" {
127 typeAttr = string(val)
128 break
129 }
130 }
131 if typeAttr != "importmap" && !updated {
132 buf.WriteString("<script type=\"importmap\">\n")
133 importMap := importmap.Blank()
134 if addImports(importMap, specifiers, all, noPrompt, noSRI) {
135 buf.WriteString(importMap.FormatJSON(2))
136 buf.WriteString("\n </script>\n ")
137 }
138 buf.Write(tokenizer.Raw())
139 updated = true
140 continue
141 }
142 if typeAttr == "importmap" && !updated {

Callers 1

AddFunction · 0.85

Calls 9

FormatJSONMethod · 0.95
BlankFunction · 0.92
ParseFunction · 0.92
lookupClosestFileFunction · 0.85
addImportsFunction · 0.85
NextMethod · 0.80
WriteMethod · 0.80
StatMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected