()
| 2 | import { useEffect, useState } from 'preact/hooks' |
| 3 | |
| 4 | export const PackageJsonPanel = () => { |
| 5 | const [packageJson, setPackageJson] = useState<any>(null) |
| 6 | const [outdatedDeps, setOutdatedDeps] = useState< |
| 7 | Record< |
| 8 | string, |
| 9 | { |
| 10 | current: string |
| 11 | wanted: string |
| 12 | latest: string |
| 13 | type?: 'dependencies' | 'devDependencies' |
| 14 | } |
| 15 | > |
| 16 | >({}) |
| 17 | |
| 18 | useEffect(() => { |
| 19 | devtoolsEventClient.emit('mounted', undefined as any) |
| 20 | const cleanupOutdated = devtoolsEventClient.on( |
| 21 | 'outdated-deps-read', |
| 22 | (event) => { |
| 23 | setOutdatedDeps(event.payload.outdatedDeps || {}) |
| 24 | }, |
| 25 | ) |
| 26 | const cleanupPackageJson = devtoolsEventClient.on( |
| 27 | 'package-json-read', |
| 28 | (event) => { |
| 29 | console.log('package-json-read', event) |
| 30 | setPackageJson(event.payload.packageJson) |
| 31 | }, |
| 32 | ) |
| 33 | return () => { |
| 34 | cleanupOutdated() |
| 35 | cleanupPackageJson() |
| 36 | } |
| 37 | }, []) |
| 38 | |
| 39 | const hasOutdated = Object.keys(outdatedDeps).length > 0 |
| 40 | |
| 41 | // Helpers |
| 42 | const stripRange = (v?: string) => (v ?? '').replace(/^[~^><=v\s]*/, '') |
| 43 | const parseSemver = (v?: string) => { |
| 44 | const s = stripRange(v) |
| 45 | const m = s.match(/^(\d+)\.(\d+)\.(\d+)/) |
| 46 | if (!m) return null |
| 47 | return { major: +m[1], minor: +m[2], patch: +m[3] } |
| 48 | } |
| 49 | const diffType = ( |
| 50 | current?: string, |
| 51 | latest?: string, |
| 52 | ): 'major' | 'minor' | 'patch' | null => { |
| 53 | const c = parseSemver(current) |
| 54 | const l = parseSemver(latest) |
| 55 | if (!c || !l) return null |
| 56 | if (l.major > c.major) return 'major' |
| 57 | if (l.major === c.major && l.minor > c.minor) return 'minor' |
| 58 | if (l.major === c.major && l.minor === c.minor && l.patch > c.patch) |
| 59 | return 'patch' |
| 60 | return null |
| 61 | } |
nothing calls this directly
no test coverage detected