-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-js-packages): implement runner for npm outdated
- Loading branch information
Showing
5 changed files
with
399 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/plugin-js-packages/src/lib/runner/outdated/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { IssueSeverity } from '@code-pushup/models'; | ||
import { VersionType } from './types'; | ||
|
||
export const outdatedSeverity: Record<VersionType, IssueSeverity> = { | ||
major: 'error', | ||
minor: 'warning', | ||
patch: 'info', | ||
}; |
102 changes: 102 additions & 0 deletions
102
packages/plugin-js-packages/src/lib/runner/outdated/transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Issue } from '@code-pushup/models'; | ||
import { objectToEntries, pluralize } from '@code-pushup/utils'; | ||
import { PackageDependency } from '../../config'; | ||
import { outdatedSeverity } from './constants'; | ||
import { | ||
NormalizedOutdatedEntries, | ||
NormalizedVersionOverview, | ||
NpmOutdatedResultJson, | ||
PackageVersion, | ||
VersionType, | ||
} from './types'; | ||
|
||
export function outdatedResultToAuditOutput( | ||
result: NpmOutdatedResultJson, | ||
dependenciesType: PackageDependency, | ||
) { | ||
// current might be missing in some cases | ||
// https://stackoverflow.com/questions/42267101/npm-outdated-command-shows-missing-in-current-version | ||
const validDependencies: NormalizedOutdatedEntries = objectToEntries(result) | ||
.filter( | ||
(entry): entry is [string, NormalizedVersionOverview] => | ||
entry[1].current != null, | ||
) | ||
.filter(([, detail]) => | ||
dependenciesType === 'prod' | ||
? detail.type === 'dependencies' | ||
: detail.type.startsWith(dependenciesType), | ||
); | ||
const outdatedDependencies = validDependencies.filter( | ||
([, versions]) => versions.current !== versions.wanted, | ||
); | ||
|
||
const issues = | ||
outdatedDependencies.length === 0 | ||
? [] | ||
: outdatedToIssues(outdatedDependencies); | ||
|
||
return { | ||
slug: `npm-outdated-${dependenciesType}`, | ||
score: outdatedDependencies.length === 0 ? 1 : 0, | ||
value: outdatedDependencies.length, | ||
displayValue: outdatedToDisplayValue(outdatedDependencies.length), | ||
...(issues.length > 0 && { details: { issues } }), | ||
}; | ||
} | ||
|
||
function outdatedToDisplayValue(outdatedDeps: number) { | ||
return outdatedDeps === 0 | ||
? 'passed' | ||
: `${outdatedDeps} outdated ${ | ||
outdatedDeps === 1 ? 'dependency' : pluralize('dependency') | ||
}`; | ||
} | ||
|
||
export function outdatedToIssues( | ||
dependencies: NormalizedOutdatedEntries, | ||
): Issue[] { | ||
return dependencies.map<Issue>(([name, versions]) => { | ||
const outdatedLevel = getOutdatedLevel(versions.current, versions.wanted); | ||
const packageDocumentation = | ||
versions.homepage == null | ||
? '' | ||
: ` Package documentation [here](${versions.homepage})`; | ||
|
||
return { | ||
message: `Package ${name} requires a ${outdatedLevel} update from **${versions.current}** to **${versions.wanted}**.${packageDocumentation}`, | ||
severity: outdatedSeverity[outdatedLevel], | ||
}; | ||
}); | ||
} | ||
|
||
export function getOutdatedLevel( | ||
currentFullVersion: string, | ||
wantedFullVersion: string, | ||
): VersionType { | ||
const current = splitPackageVersion(currentFullVersion); | ||
const wanted = splitPackageVersion(wantedFullVersion); | ||
|
||
if (current.major < wanted.major) { | ||
return 'major'; | ||
} | ||
|
||
if (current.minor < wanted.minor) { | ||
return 'minor'; | ||
} | ||
|
||
if (current.patch < wanted.patch) { | ||
return 'patch'; | ||
} | ||
|
||
throw new Error('Package is not outdated.'); | ||
} | ||
|
||
export function splitPackageVersion(fullVersion: string): PackageVersion { | ||
const [major, minor, patch] = fullVersion.split('.').map(Number); | ||
|
||
if (major == null || minor == null || patch == null) { | ||
throw new Error(`Invalid version description ${fullVersion}`); | ||
} | ||
|
||
return { major, minor, patch }; | ||
} |
Oops, something went wrong.