-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plugin-js-packages): consider dynamic outdated fields for Yarn v1
- Loading branch information
Showing
5 changed files
with
217 additions
and
30 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/plugin-js-packages/src/lib/package-managers/yarn-classic/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,20 @@ | ||
import { OutdatedDependency } from '../../runner/outdated/types'; | ||
import { Yarnv1FieldName } from './types'; | ||
|
||
export const outdatedtoFieldMapper: Record< | ||
keyof OutdatedDependency, | ||
Yarnv1FieldName | ||
> = { | ||
name: 'Package', | ||
current: 'Current', | ||
latest: 'Latest', | ||
type: 'Package Type', | ||
url: 'URL', | ||
}; | ||
|
||
export const REQUIRED_OUTDATED_FIELDS: Yarnv1FieldName[] = [ | ||
'Package', | ||
'Current', | ||
'Latest', | ||
'Package Type', | ||
]; |
78 changes: 68 additions & 10 deletions
78
packages/plugin-js-packages/src/lib/package-managers/yarn-classic/outdated-result.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 |
---|---|---|
@@ -1,16 +1,74 @@ | ||
import { fromJsonLines } from '@code-pushup/utils'; | ||
import { OutdatedResult } from '../../runner/outdated/types'; | ||
import { Yarnv1OutdatedResultJson } from './types'; | ||
import { | ||
fromJsonLines, | ||
objectFromEntries, | ||
objectToEntries, | ||
objectToKeys, | ||
} from '@code-pushup/utils'; | ||
import { | ||
OutdatedDependency, | ||
OutdatedResult, | ||
} from '../../runner/outdated/types'; | ||
import { REQUIRED_OUTDATED_FIELDS, outdatedtoFieldMapper } from './constants'; | ||
import { | ||
Yarnv1FieldName, | ||
Yarnv1OutdatedResultJson, | ||
yarnv1FieldNames, | ||
} from './types'; | ||
|
||
export function yarnv1ToOutdatedResult(output: string): OutdatedResult { | ||
const yarnv1Outdated = fromJsonLines<Yarnv1OutdatedResultJson>(output); | ||
const fields = yarnv1Outdated[1].data.head; | ||
const dependencies = yarnv1Outdated[1].data.body; | ||
|
||
return dependencies.map(([name, current, _, latest, __, type, url]) => ({ | ||
name, | ||
current, | ||
latest, | ||
type, | ||
url, | ||
})); | ||
// no outdated dependencies | ||
if (dependencies.length === 0) { | ||
return []; | ||
} | ||
|
||
// map dynamic fields | ||
validateOutdatedFields(fields); | ||
const indexMapping = getOutdatedFieldIndexes(fields); | ||
|
||
return dependencies.map( | ||
dep => | ||
objectFromEntries( | ||
objectToKeys(indexMapping) | ||
.map(field => [field, dep[indexMapping[field]]] as const) | ||
.filter( | ||
(entry): entry is [keyof OutdatedDependency, string] => | ||
entry[1] != null, | ||
), | ||
) as OutdatedDependency, | ||
); | ||
} | ||
|
||
export function validateOutdatedFields(head: string[]) { | ||
const relevantFields = head.filter(isYarnv1FieldName); | ||
if (hasAllRequiredFields(relevantFields)) { | ||
return true; | ||
} | ||
|
||
throw new Error( | ||
`Yarn v1 outdated: Template [${head.join( | ||
', ', | ||
)}] does not contain all required fields [${yarnv1FieldNames.join(', ')}]`, | ||
); | ||
} | ||
|
||
function isYarnv1FieldName(value: string): value is Yarnv1FieldName { | ||
const names: readonly string[] = yarnv1FieldNames; | ||
return names.includes(value); | ||
} | ||
|
||
function hasAllRequiredFields(head: Yarnv1FieldName[]) { | ||
return REQUIRED_OUTDATED_FIELDS.every(field => head.includes(field)); | ||
} | ||
|
||
export function getOutdatedFieldIndexes(all: string[]) { | ||
return objectFromEntries( | ||
objectToEntries(outdatedtoFieldMapper).map(([outdatedField, yarnField]) => [ | ||
outdatedField, | ||
all.indexOf(yarnField), | ||
]), | ||
); | ||
} |
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
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
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