|
| 1 | +import { Issue } from '@code-pushup/models'; |
| 2 | +import { objectToEntries, pluralize } from '@code-pushup/utils'; |
| 3 | +import { PackageDependency } from '../../config'; |
| 4 | +import { outdatedSeverity } from './constants'; |
| 5 | +import { |
| 6 | + NormalizedOutdatedEntries, |
| 7 | + NormalizedVersionOverview, |
| 8 | + NpmOutdatedResultJson, |
| 9 | + PackageVersion, |
| 10 | + VersionType, |
| 11 | +} from './types'; |
| 12 | + |
| 13 | +export function outdatedResultToAuditOutput( |
| 14 | + result: NpmOutdatedResultJson, |
| 15 | + dependenciesType: PackageDependency, |
| 16 | +) { |
| 17 | + // current might be missing in some cases |
| 18 | + // https://stackoverflow.com/questions/42267101/npm-outdated-command-shows-missing-in-current-version |
| 19 | + const validDependencies: NormalizedOutdatedEntries = objectToEntries(result) |
| 20 | + .filter( |
| 21 | + (entry): entry is [string, NormalizedVersionOverview] => |
| 22 | + entry[1].current != null, |
| 23 | + ) |
| 24 | + .filter(([, detail]) => |
| 25 | + dependenciesType === 'prod' |
| 26 | + ? detail.type === 'dependencies' |
| 27 | + : detail.type.startsWith(dependenciesType), |
| 28 | + ); |
| 29 | + const outdatedDependencies = validDependencies.filter( |
| 30 | + ([, versions]) => versions.current !== versions.wanted, |
| 31 | + ); |
| 32 | + |
| 33 | + const issues = |
| 34 | + outdatedDependencies.length === 0 |
| 35 | + ? [] |
| 36 | + : outdatedToIssues(outdatedDependencies); |
| 37 | + |
| 38 | + return { |
| 39 | + slug: `npm-outdated-${dependenciesType}`, |
| 40 | + score: outdatedDependencies.length === 0 ? 1 : 0, |
| 41 | + value: outdatedDependencies.length, |
| 42 | + displayValue: outdatedToDisplayValue(outdatedDependencies.length), |
| 43 | + ...(issues.length > 0 && { details: { issues } }), |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +function outdatedToDisplayValue(outdatedDeps: number) { |
| 48 | + return outdatedDeps === 0 |
| 49 | + ? 'passed' |
| 50 | + : `${outdatedDeps} outdated ${ |
| 51 | + outdatedDeps === 1 ? 'dependency' : pluralize('dependency') |
| 52 | + }`; |
| 53 | +} |
| 54 | + |
| 55 | +export function outdatedToIssues( |
| 56 | + dependencies: NormalizedOutdatedEntries, |
| 57 | +): Issue[] { |
| 58 | + return dependencies.map<Issue>(([name, versions]) => { |
| 59 | + const outdatedLevel = getOutdatedLevel(versions.current, versions.wanted); |
| 60 | + const packageDocumentation = |
| 61 | + versions.homepage == null |
| 62 | + ? '' |
| 63 | + : ` Package documentation [here](${versions.homepage})`; |
| 64 | + |
| 65 | + return { |
| 66 | + message: `Package ${name} requires a ${outdatedLevel} update from **${versions.current}** to **${versions.wanted}**.${packageDocumentation}`, |
| 67 | + severity: outdatedSeverity[outdatedLevel], |
| 68 | + }; |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +export function getOutdatedLevel( |
| 73 | + currentFullVersion: string, |
| 74 | + wantedFullVersion: string, |
| 75 | +): VersionType { |
| 76 | + const current = splitPackageVersion(currentFullVersion); |
| 77 | + const wanted = splitPackageVersion(wantedFullVersion); |
| 78 | + |
| 79 | + if (current.major < wanted.major) { |
| 80 | + return 'major'; |
| 81 | + } |
| 82 | + |
| 83 | + if (current.minor < wanted.minor) { |
| 84 | + return 'minor'; |
| 85 | + } |
| 86 | + |
| 87 | + if (current.patch < wanted.patch) { |
| 88 | + return 'patch'; |
| 89 | + } |
| 90 | + |
| 91 | + throw new Error('Package is not outdated.'); |
| 92 | +} |
| 93 | + |
| 94 | +export function splitPackageVersion(fullVersion: string): PackageVersion { |
| 95 | + const [major, minor, patch] = fullVersion.split('.').map(Number); |
| 96 | + |
| 97 | + if (major == null || minor == null || patch == null) { |
| 98 | + throw new Error(`Invalid version description ${fullVersion}`); |
| 99 | + } |
| 100 | + |
| 101 | + return { major, minor, patch }; |
| 102 | +} |
0 commit comments