@size-limit/time
Version:
Downloading and running time plugin for Size Limit
36 lines (31 loc) • 949 B
JavaScript
import { existsSync } from 'node:fs'
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
const VERSION = 1
const CACHE = join(
import.meta.dirname,
'..',
'.cache',
'size-limit',
'cache.json'
)
export async function createCacheDir() {
let sizeLimitCache = dirname(CACHE)
let npmCache = dirname(sizeLimitCache)
if (!existsSync(npmCache)) await mkdir(npmCache)
if (!existsSync(sizeLimitCache)) await mkdir(sizeLimitCache)
}
export async function getCache() {
if (!existsSync(CACHE)) return false
try {
let cache = JSON.parse(await readFile(CACHE))
if (typeof cache !== 'object' || cache.version !== VERSION) cache = false
return cache.throttling || false
} catch {
return false
}
}
export async function saveCache(throttling) {
if (!existsSync(CACHE)) await createCacheDir()
await writeFile(CACHE, JSON.stringify({ throttling, version: VERSION }))
}