MCPcopy Create free account
hub / github.com/anomalyco/opencode / save

Function save

script/stats.ts:125–189  ·  view source on GitHub ↗
(githubTotal: number, npmDownloads: number)

Source from the content-addressed store, hash-verified

123}
124
125async function save(githubTotal: number, npmDownloads: number) {
126 const file = "STATS.md"
127 const date = new Date().toISOString().split("T")[0]
128 const total = githubTotal + npmDownloads
129
130 let previousGithub = 0
131 let previousNpm = 0
132 let previousTotal = 0
133 let content = ""
134
135 try {
136 content = await Bun.file(file).text()
137 const lines = content.trim().split("\n")
138
139 for (let i = lines.length - 1; i >= 0; i--) {
140 const line = lines[i].trim()
141 if (line.startsWith("|") && !line.includes("Date") && !line.includes("---")) {
142 const match = line.match(
143 /\|\s*[\d-]+\s*\|\s*([\d,]+)\s*(?:\([^)]*\))?\s*\|\s*([\d,]+)\s*(?:\([^)]*\))?\s*\|\s*([\d,]+)\s*(?:\([^)]*\))?\s*\|/,
144 )
145 if (match) {
146 previousGithub = parseInt(match[1].replace(/,/g, ""))
147 previousNpm = parseInt(match[2].replace(/,/g, ""))
148 previousTotal = parseInt(match[3].replace(/,/g, ""))
149 break
150 }
151 }
152 }
153 } catch {
154 content =
155 "# Download Stats\n\n| Date | GitHub Downloads | npm Downloads | Total |\n|------|------------------|---------------|-------|\n"
156 }
157
158 const githubChange = githubTotal - previousGithub
159 const npmChange = npmDownloads - previousNpm
160 const totalChange = total - previousTotal
161
162 const githubChangeStr =
163 githubChange > 0
164 ? ` (+${githubChange.toLocaleString()})`
165 : githubChange < 0
166 ? ` (${githubChange.toLocaleString()})`
167 : " (+0)"
168 const npmChangeStr =
169 npmChange > 0 ? ` (+${npmChange.toLocaleString()})` : npmChange < 0 ? ` (${npmChange.toLocaleString()})` : " (+0)"
170 const totalChangeStr =
171 totalChange > 0
172 ? ` (+${totalChange.toLocaleString()})`
173 : totalChange < 0
174 ? ` (${totalChange.toLocaleString()})`
175 : " (+0)"
176 const line = `| ${date} | ${githubTotal.toLocaleString()}${githubChangeStr} | ${npmDownloads.toLocaleString()}${npmChangeStr} | ${total.toLocaleString()}${totalChangeStr} |\n`
177
178 if (!content.includes("# Download Stats")) {
179 content =
180 "# Download Stats\n\n| Date | GitHub Downloads | npm Downloads | Total |\n|------|------------------|---------------|-------|\n"
181 }
182

Callers 1

stats.tsFile · 0.70

Calls 5

fileMethod · 0.80
writeMethod · 0.80
spawnMethod · 0.80
textMethod · 0.65
logMethod · 0.45

Tested by

no test coverage detected