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

Function tidy

cli/command_tidy.go:42–152  ·  view source on GitHub ↗
(noSRI bool)

Source from the content-addressed store, hash-verified

40}
41
42func tidy(noSRI bool) (err error) {
43 indexHtml, exists, err := lookupClosestFile("index.html")
44 if err != nil {
45 err = fmt.Errorf("Failed to lookup index.html: %w", err)
46 return
47 }
48
49 if !exists {
50 err = fmt.Errorf("index.html not found")
51 return
52 }
53
54 f, err := os.Open(indexHtml)
55 if err != nil {
56 return
57 }
58
59 tokenizer := html.NewTokenizer(f)
60 buf := bytes.NewBuffer(nil)
61 for {
62 token := tokenizer.Next()
63 if token == html.ErrorToken && tokenizer.Err() == io.EOF {
64 break
65 }
66 if token == html.StartTagToken {
67 tagName, moreAttr := tokenizer.TagName()
68 if string(tagName) == "script" && moreAttr {
69 var typeAttr string
70 for moreAttr {
71 var key, val []byte
72 key, val, moreAttr = tokenizer.TagAttr()
73 if string(key) == "type" {
74 typeAttr = string(val)
75 break
76 }
77 }
78 if typeAttr == "importmap" {
79 buf.Write(tokenizer.Raw())
80 var prevImportMap *importmap.ImportMap
81 if tokenizer.Next() == html.TextToken {
82 importMapJson := bytes.TrimSpace(tokenizer.Text())
83 if len(importMapJson) > 0 {
84 prevImportMap, err = importmap.Parse(nil, importMapJson)
85 if err != nil {
86 err = fmt.Errorf("invalid importmap script: %w", err)
87 return
88 }
89 }
90 }
91 if prevImportMap == nil || prevImportMap.Imports.Len() == 0 {
92 fmt.Println(term.Dim("No imports found."))
93 return
94 }
95 buf.WriteString("\n")
96 importMap := importmap.Blank()
97 importMap.SetConfig(prevImportMap.Config())
98 importMap.SetIntegrity(prevImportMap.Integrity())
99 imports := make([]importmap.Import, 0, prevImportMap.Imports.Len())

Callers 1

TidyFunction · 0.85

Calls 15

ConfigMethod · 0.95
IntegrityMethod · 0.95
RangeScopesMethod · 0.95
ParseFunction · 0.92
BlankFunction · 0.92
ParseEsmPathFunction · 0.92
IsExactVersionFunction · 0.92
lookupClosestFileFunction · 0.85
addImportsFunction · 0.85
NextMethod · 0.80
WriteMethod · 0.80
SetConfigMethod · 0.80

Tested by

no test coverage detected