@code-pushup/js-packages-plugin
Version:
Code PushUp plugin for JavaScript packages 🛡️
39 lines • 1.68 kB
JavaScript
import { fromJsonLines, objectFromEntries, objectToEntries, objectToKeys, } from '@code-pushup/utils';
import { REQUIRED_OUTDATED_FIELDS, outdatedtoFieldMapper, } from './constants.js';
import { yarnClassicFieldNames, } from './types.js';
export function yarnClassicToOutdatedResult(output) {
const yarnOutdated = fromJsonLines(output);
const fields = yarnOutdated[1]?.data.head ?? [];
const dependencies = yarnOutdated[1]?.data.body ?? [];
// 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]]])
.filter((entry) => entry[1] != null)));
}
export function validateOutdatedFields(head) {
const relevantFields = head.filter(isYarnClassicFieldName);
if (hasAllRequiredFields(relevantFields)) {
return true;
}
throw new Error(`Yarn v1 outdated: Template [${head.join(', ')}] does not contain all required fields [${yarnClassicFieldNames.join(', ')}]`);
}
function isYarnClassicFieldName(value) {
const names = yarnClassicFieldNames;
return names.includes(value);
}
function hasAllRequiredFields(head) {
return REQUIRED_OUTDATED_FIELDS.every(field => head.includes(field));
}
export function getOutdatedFieldIndexes(all) {
return objectFromEntries(objectToEntries(outdatedtoFieldMapper).map(([outdatedField, yarnField]) => [
outdatedField,
all.indexOf(yarnField),
]));
}
//# sourceMappingURL=outdated-result.js.map