* Save the install counts cache to disk atomically. * Uses a temp file + rename pattern to prevent corruption.
( cache: InstallCountsCache, )
| 147 | * Uses a temp file + rename pattern to prevent corruption. |
| 148 | */ |
| 149 | async function saveInstallCountsCache( |
| 150 | cache: InstallCountsCache, |
| 151 | ): Promise<void> { |
| 152 | const cachePath = getInstallCountsCachePath() |
| 153 | const tempPath = `${cachePath}.${randomBytes(8).toString('hex')}.tmp` |
| 154 | |
| 155 | try { |
| 156 | // Ensure the plugins directory exists |
| 157 | const pluginsDir = getPluginsDirectory() |
| 158 | await getFsImplementation().mkdir(pluginsDir) |
| 159 | |
| 160 | // Write to temp file |
| 161 | const content = jsonStringify(cache, null, 2) |
| 162 | await writeFile(tempPath, content, { |
| 163 | encoding: 'utf-8', |
| 164 | mode: 0o600, |
| 165 | }) |
| 166 | |
| 167 | // Atomic rename |
| 168 | await rename(tempPath, cachePath) |
| 169 | logForDebugging('Install counts cache saved successfully') |
| 170 | } catch (error) { |
| 171 | logError(error) |
| 172 | // Clean up temp file if it exists |
| 173 | try { |
| 174 | await unlink(tempPath) |
| 175 | } catch { |
| 176 | // Ignore cleanup errors |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Fetch install counts from GitHub stats repository |
no test coverage detected