import {Resolver, ResolveOptions, MinimalResolveOptions} from '@yarnpkg/core'; import {Descriptor, Locator, Manifest, Package} from '@yarnpkg/core'; import {LinkType} from '@yarnpkg/core'; import {MessageName, ReportError} from '@yarnpkg/core'; import {miscUtils, structUtils, hashUtils} from '@yarnpkg/core'; import {PROTOCOL} from './constants'; import * as execUtils from './execUtils'; // We use this for the generators to be regenerated without bumping the whole cache const CACHE_VERSION = 2; export class ExecResolver implements Resolver { supportsDescriptor(descriptor: Descriptor, opts: MinimalResolveOptions) { if (!descriptor.range.startsWith(PROTOCOL)) return false; return true; } supportsLocator(locator: Locator, opts: MinimalResolveOptions) { if (!locator.reference.startsWith(PROTOCOL)) return false; return true; } shouldPersistResolution(locator: Locator, opts: MinimalResolveOptions) { return false; } bindDescriptor(descriptor: Descriptor, fromLocator: Locator, opts: MinimalResolveOptions) { if (opts.project.tryWorkspaceByLocator(fromLocator) === null) throw new ReportError(MessageName.INVALID_MANIFEST, `${structUtils.prettyLocator(opts.project.configuration, fromLocator)} lists ${structUtils.prettyDescriptor(opts.project.configuration, descriptor)} as dependency, but only workspaces can depend on exec: packages.`); return structUtils.bindDescriptor(descriptor, { locator: structUtils.stringifyLocator(fromLocator), }); } getResolutionDependencies(descriptor: Descriptor, opts: MinimalResolveOptions) { return {}; } async getCandidates(descriptor: Descriptor, dependencies: Record, opts: ResolveOptions) { if (!opts.fetchOptions) throw new Error(`Assertion failed: This resolver cannot be used unless a fetcher is configured`); const {path, parentLocator} = execUtils.parseSpec(descriptor.range); if (parentLocator === null) throw new Error(`Assertion failed: The descriptor should have been bound`); const generatorFile = await execUtils.loadGeneratorFile(structUtils.makeRange({ protocol: PROTOCOL, source: path, selector: path, params: { locator: structUtils.stringifyLocator(parentLocator), }, }), PROTOCOL, opts.fetchOptions); const generatorHash = hashUtils.makeHash(`${CACHE_VERSION}`, generatorFile).slice(0, 6); return [execUtils.makeLocator(descriptor, {parentLocator, path, generatorHash, protocol: PROTOCOL})]; } async getSatisfying(descriptor: Descriptor, dependencies: Record, locators: Array, opts: ResolveOptions) { const [locator] = await this.getCandidates(descriptor, dependencies, opts); return { locators: locators.filter(candidate => candidate.locatorHash === locator.locatorHash), sorted: false, }; } async resolve(locator: Locator, opts: ResolveOptions) { if (!opts.fetchOptions) throw new Error(`Assertion failed: This resolver cannot be used unless a fetcher is configured`); const packageFetch = await opts.fetchOptions.fetcher.fetch(locator, opts.fetchOptions); const manifest = await miscUtils.releaseAfterUseAsync(async () => { return await Manifest.find(packageFetch.prefixPath, {baseFs: packageFetch.packageFs}); }, packageFetch.releaseFs); return { ...locator, version: manifest.version || `0.0.0`, languageName: manifest.languageName || opts.project.configuration.get(`defaultLanguageName`), linkType: LinkType.HARD, conditions: manifest.getConditions(), dependencies: opts.project.configuration.normalizeDependencyMap(manifest.dependencies), peerDependencies: manifest.peerDependencies, dependenciesMeta: manifest.dependenciesMeta, peerDependenciesMeta: manifest.peerDependenciesMeta, bin: manifest.bin, }; } }