Skip to content

Commit

Permalink
refactor: peer dep failure state labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Feb 22, 2021
1 parent 2173bf0 commit 03d3c64
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/lib/peerDepValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import * as Semver from 'semver'
import { PackageJson } from 'type-fest'

type Failure =
| { message: string; kind: 'peer_dep_not_installed' }
| { message: string; kind: 'peer_dep_invalid_json'; error: unknown }
| { message: string; kind: 'peer_dep_invalid_package_json' }
// todo
// | { message: string; kind: 'peer_dep_import_error' }
// | { message: string; kind: 'peer_dep_missing_from_node_modules' }
// | { message: string; kind: 'peer_dep_missing_from_package_json' }
| { message: string; kind: 'peer_dep_not_installed' } // replace with peer_dep_missing_from_package_json
| { message: string; kind: 'peer_dep_invalid_version' }
| { message: string; kind: 'peer_dep_package_json_invalid' }
| { message: string; kind: 'peer_dep_package_json_read_error'; error: unknown }
| { message: string; kind: 'unexpected_error'; error: unknown }

export function enforceValidPeerDependencies({ packageJson }: { packageJson: PackageJson }): void {
Expand Down Expand Up @@ -74,7 +78,7 @@ export function validatePeerDependencyRangeSatisfied({
} catch (error: unknown) {
if (!isModuleNotFoundError(error)) {
return {
kind: 'peer_dep_invalid_json',
kind: 'peer_dep_package_json_read_error',
message: `Peer dependency check found ${peerDependencyName} requried by ${requireer.name} to be installed but encountered an error while reading its package.json.`,
error,
}
Expand All @@ -99,7 +103,7 @@ export function validatePeerDependencyRangeSatisfied({
// case _should_ never happen under normal circumstances.
if (!pdVersion) {
return {
kind: 'peer_dep_invalid_package_json',
kind: 'peer_dep_package_json_invalid',
message: renderWarning(
`Peer dependency validation check failed unexpectedly. ${requireer.name} requires peer dependency ${pdPackageJson.name}. No version info for ${pdPackageJson.name} could be found in its package.json thus preventing a check if its version satisfies the peer dependency version range.`
),
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/__snapshots__/peerDepValidator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[`ValidatePeerDependencies if peer dep missing, then returns failure 1`]

exports[`ValidatePeerDependencies if peer dep package.json missing version field, then returns failure 1`] = `
"{
kind: 'peer_dep_invalid_package_json',
kind: 'peer_dep_package_json_invalid',
message: 'WARNING: Peer dependency validation check failed unexpectedly. alpha requires peer dependency charlie. No version info for charlie could be found in its package.json thus preventing a check if its version satisfies the peer dependency version range.'
}"
`;
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/peerDepValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('ValidatePeerDependencies', () => {
return badJson
},
})
expect(runValidatePeerDependencies().stdout).toMatch(/peer_dep_invalid_json/)
expect(runValidatePeerDependencies().stdout).toMatch(/peer_dep_package_json_read_error/)
})

it('if project peer dep version does not satisfy required range, then returns failure', () => {
Expand Down

0 comments on commit 03d3c64

Please sign in to comment.