How to use the @yarnpkg/core.structUtils.parseRange function in @yarnpkg/core

To help you get started, we’ve selected a few @yarnpkg/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yarnpkg / berry / packages / plugin-pack / sources / index.ts View on Github external
rawManifest.main = rawManifest.publishConfig.main;

    if (rawManifest.publishConfig.module)
      rawManifest.module = rawManifest.publishConfig.module;

    if (rawManifest.publishConfig.bin) {
      rawManifest.bin = rawManifest.publishConfig.bin;
    }
  }

  const project = workspace.project;

  for (const dependencyType of DEPENDENCY_TYPES) {
    for (const [identHash, descriptor] of workspace.manifest.getForScope(dependencyType)) {
      const matchingWorkspaces = project.findWorkspacesByDescriptor(descriptor);
      const range = structUtils.parseRange(descriptor.range);

      if (range.protocol !== WORKSPACE_PROTOCOL)
        continue;

      if (matchingWorkspaces.length === 0) {
        if (project.workspacesByIdent.has(identHash)) {
          throw new ReportError(MessageName.WORKSPACE_NOT_FOUND, `${structUtils.prettyDescriptor(project.configuration, descriptor)}: No local workspace found for this range`);
        }
      } else if (matchingWorkspaces.length > 1) {
        throw new ReportError(MessageName.TOO_MANY_MATCHING_WORKSPACES, `${structUtils.prettyDescriptor(project.configuration, descriptor)}: Too many workspaces match this range, please disambiguate`);
      } else {
        const [matchingWorkspace] = matchingWorkspaces;
        let versionToWrite: string;

        // For workspace:path/to/workspace and workspace:* we look up the workspace version
        if (structUtils.areDescriptorsEqual(descriptor, matchingWorkspace.anchoredDescriptor) || range.selector === `*`)
github yarnpkg / berry / packages / plugin-essentials / sources / suggestUtils.ts View on Github external
const resolverOptions = {checksums: project.storedChecksums, project, cache, fetcher, report, resolver};

  let candidateLocators;
  try {
    candidateLocators = await resolver.getCandidates(latestDescriptor, new Map(), resolverOptions);
  } catch {
    return null;
  }

  if (candidateLocators.length === 0)
    return null;

  // Per the requirements exposed in Resolver.ts, the best is the first one
  const bestLocator = candidateLocators[0];

  let {protocol, source, params, selector} = structUtils.parseRange(structUtils.convertToManifestRange(bestLocator.reference));
  if (protocol === project.configuration.get(`defaultProtocol`))
    protocol = null;

  if (semver.valid(selector) && preserveModifier) {
    const modifier = extractModifier(latestDescriptor, {project});
    selector = modifier + selector;
  }

  return structUtils.makeDescriptor(bestLocator, structUtils.makeRange({protocol, source, params, selector}));
}
github yarnpkg / berry / packages / plugin-patch / sources / patchUtils.ts View on Github external
function parseSpec(spec: string, sourceParser: (source: string) => T) {
  const {source, selector, params} = structUtils.parseRange(spec);
  if (source === null)
    throw new Error(`Patch locators must explicitly define their source`);

  const patchPaths = selector
    ? selector.split(/&/).map(path => npath.toPortablePath(path))
    : [];

  const parentLocator = params && typeof params.locator === `string`
    ? structUtils.parseLocator(params.locator)
    : null;

  const sourceItem = sourceParser(source);

  return {parentLocator, sourceItem, patchPaths};
}
github yarnpkg / berry / packages / plugin-npm / sources / NpmSemverResolver.ts View on Github external
async resolve(locator: Locator, opts: ResolveOptions) {
    const {selector} = structUtils.parseRange(locator.reference);

    const version = semver.clean(selector);
    if (version === null)
      throw new ReportError(MessageName.RESOLVER_NOT_FOUND, `The npm semver resolver got selected, but the version isn't semver`);

    const registryData = await npmHttpUtils.get(npmHttpUtils.getIdentUrl(locator), {
      configuration: opts.project.configuration,
      ident: locator,
      json: true,
    });

    if (!Object.prototype.hasOwnProperty.call(registryData, `versions`))
      throw new ReportError(MessageName.REMOTE_INVALID, `Registry returned invalid data for - missing "versions" field`);

    if (!Object.prototype.hasOwnProperty.call(registryData.versions, version))
      throw new ReportError(MessageName.REMOTE_NOT_FOUND, `Registry failed to return reference "${version}"`);
github yarnpkg / berry / packages / plugin-npm / sources / NpmHttpFetcher.ts View on Github external
supports(locator: Locator, opts: MinimalFetchOptions) {
    if (!locator.reference.startsWith(PROTOCOL))
      return false;

    const {selector, params} = structUtils.parseRange(locator.reference);
    if (!semver.valid(selector))
      return false;

    if (params === null || typeof params.__archiveUrl !== `string`)
      return false;

    return true;
  }
github yarnpkg / berry / packages / plugin-essentials / sources / suggestUtils.ts View on Github external
export function applyModifier(descriptor: Descriptor, modifier: Modifier) {
  let {protocol, source, params, selector} = structUtils.parseRange(descriptor.range);

  if (semver.valid(selector))
    selector = `${modifier}${descriptor.range}`;

  return structUtils.makeDescriptor(descriptor, structUtils.makeRange({protocol, source, params, selector}));
}
github yarnpkg / berry / packages / plugin-npm / sources / NpmHttpFetcher.ts View on Github external
async fetchFromNetwork(locator: Locator, opts: FetchOptions) {
    const {params} = structUtils.parseRange(locator.reference);
    if (params === null || typeof params.__archiveUrl !== `string`)
      throw new Error(`Assertion failed: The archiveUrl querystring parameter should have been available`);

    const sourceBuffer = await npmHttpUtils.get(params.__archiveUrl, {
      configuration: opts.project.configuration,
      ident: locator,
    });

    return await tgzUtils.convertToZip(sourceBuffer, {
      stripComponents: 1,
      prefixPath: structUtils.getIdentVendorPath(locator),
    });
  }
}