yarn-berry-deduplicate
Version:
Deduplication tool for yarn.lock files
78 lines • 3.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackages = void 0;
const core_1 = require("@yarnpkg/core");
const extractPackages = (yarnEntries, { includeScopes = [], includePackages = [], excludePackages = [], excludeScopes = [], } = {}) => {
const packages = {};
for (const [entryName, entry] of Object.entries(yarnEntries)) {
if (entryName === '__metadata')
continue;
const resolution = entry.resolution;
const resolutionDescriptor = resolution ? core_1.structUtils.tryParseDescriptor(resolution, true) : null;
for (const descriptorString of entryName.split(', ')) {
const descriptor = core_1.structUtils.parseDescriptor(descriptorString);
const range = core_1.structUtils.parseRange(descriptor.range);
// If the range is a valid descriptor we're dealing with an alias ("foo": "npm:lodash@*")
// and need to make the locator from that instead of the original descriptor
let actualDescriptor = descriptor;
try {
const potentialDescriptor = core_1.structUtils.tryParseDescriptor(range.selector, true);
if (potentialDescriptor) {
actualDescriptor = potentialDescriptor;
}
}
catch { }
const actualRange = core_1.structUtils.parseRange(actualDescriptor.range);
const resolutionRange = resolutionDescriptor ? core_1.structUtils.parseRange(resolutionDescriptor.range) : null;
const packageName = core_1.structUtils.stringifyIdent(actualDescriptor);
const protocol = actualRange.protocol || (resolutionRange === null || resolutionRange === void 0 ? void 0 : resolutionRange.protocol) || null;
let ignored = (() => {
if (!protocol) {
return 'no protocol';
}
if (!['npm', 'npm:'].includes(protocol)) {
return 'not npm protocol';
}
if (!!entry.linkType && entry.linkType !== 'hard') {
return 'not hard link';
}
})();
// If there is a list of scopes, only process those.
if (includeScopes.length > 0 &&
!includeScopes.find((scope) => packageName.startsWith(`${scope}/`))) {
ignored = 'not in includeScopes';
}
else if (excludeScopes.length > 0 &&
excludeScopes.find((scope) => packageName.startsWith(`${scope}/`))) {
ignored = 'in excludeScopes';
}
// If there is a list of package names, only process those.
else if (includePackages.length > 0 && !includePackages.includes(packageName)) {
ignored = 'not in includePackages';
}
else if (excludePackages.length > 0 && excludePackages.includes(packageName)) {
ignored = 'in excludePackages';
}
const packageKey = ignored ? entryName : packageName + '@' + protocol;
packages[packageKey] = packages[packageKey] || [];
packages[packageKey].push({
packageKey,
packageName,
pkg: entry,
descriptorString,
descriptor,
actualDescriptor,
ignored,
requestedProtocol: protocol,
requestedVersion: range.selector,
installedVersion: entry.version,
satisfiedBy: new Set(),
versions: new Map()
});
}
}
;
return packages;
};
exports.extractPackages = extractPackages;
//# sourceMappingURL=extractPackages.js.map