How to use @yarnpkg/parsers - 10 common examples

To help you get started, we’ve selected a few @yarnpkg/parsers 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 / yarnpkg-core / sources / Project.ts View on Github external
private async setupResolutions() {
    this.storedResolutions = new Map();

    this.storedDescriptors = new Map();
    this.storedPackages = new Map();

    const lockfilePath = ppath.join(this.cwd, this.configuration.get(`lockfileFilename`));
    const defaultLanguageName = this.configuration.get(`defaultLanguageName`);

    if (xfs.existsSync(lockfilePath)) {
      const content = await xfs.readFilePromise(lockfilePath, `utf8`);
      const parsed: any = parseSyml(content);

      // Protects against v1 lockfiles
      if (parsed.__metadata) {
        const lockfileVersion = parsed.__metadata.version;

        for (const key of Object.keys(parsed)) {
          if (key === `__metadata`)
            continue;

          const data = parsed[key];
          const locator = structUtils.parseLocator(data.resolution, true);

          const manifest = new Manifest();
          manifest.load(data);

          const version = manifest.version;
github yarnpkg / berry / packages / yarnpkg-core / sources / Project.ts View on Github external
const resolution = this.storedResolutions.get(dependency.descriptorHash);
          if (!resolution)
            throw new Error(`Assertion failed: The resolution (${structUtils.prettyDescriptor(this.configuration, dependency)}) should have been registered`);

          traverse(resolution, new Set(seenPackages));
        }
      };

      traverse(locator.locatorHash);

      return hash.digest(`hex`);
    };

    const bstatePath: PortablePath = this.configuration.get(`bstatePath`);
    const bstate = xfs.existsSync(bstatePath)
      ? parseSyml(await xfs.readFilePromise(bstatePath, `utf8`)) as {[key: string]: string}
      : {};

    // We reconstruct the build state from an empty object because we want to
    // remove the state from packages that got removed
    const nextBState = {} as {[key: string]: string};

    while (buildablePackages.size > 0) {
      const savedSize = buildablePackages.size;
      const buildPromises = [];

      for (const locatorHash of buildablePackages) {
        const pkg = this.storedPackages.get(locatorHash);
        if (!pkg)
          throw new Error(`Assertion failed: The package should have been registered`);

        let isBuildable = true;
github yarnpkg / berry / packages / yarnpkg-core / sources / LegacyMigrationResolver.ts View on Github external
async setup(project: Project, {report}: {report: Report}) {
    const lockfilePath = ppath.join(project.cwd, project.configuration.get(`lockfileFilename`));

    // No need to enable it if the lockfile doesn't exist
    if (!xfs.existsSync(lockfilePath))
      return;

    const content = await xfs.readFilePromise(lockfilePath, `utf8`);
    const parsed = parseSyml(content);

    // No need to enable it either if the lockfile is modern
    if (Object.prototype.hasOwnProperty.call(parsed, `__metadata`))
      return;

    const resolutions = this.resolutions = new Map();

    for (const key of Object.keys(parsed)) {
      let descriptor = structUtils.tryParseDescriptor(key);

      if (!descriptor) {
        report.reportWarning(MessageName.YARN_IMPORT_FAILED, `Failed to parse the string "${key}" into a proper descriptor`);
        continue;
      }

      if (semver.validRange(descriptor.range))
github yarnpkg / berry / packages / yarnpkg-shell / sources / index.ts View on Github external
const normalizedEnv: {[key: string]: string} = {};
  for (const [key, value] of Object.entries(env))
    if (typeof value !== `undefined`)
      normalizedEnv[key] = value;

  const normalizedBuiltins = new Map(BUILTINS);
  for (const [key, builtin] of Object.entries(builtins))
    normalizedBuiltins.set(key, builtin);

  // This is meant to be the equivalent of /dev/null
  if (stdin === null) {
    stdin = new PassThrough();
    (stdin as PassThrough).end();
  }

  const ast = parseShell(command);

  // If the shell line doesn't use the args, inject it at the end of the
  // right-most command
  if (!locateArgsVariable(ast) && ast.length > 0 && args.length > 0) {
    let command = ast[ast.length - 1];
    while (command.then)
      command = command.then.line;

    let chain = command.chain;
    while (chain.then)
      chain = chain.then.chain;

    if (chain.type === `command`) {
      chain.args = chain.args.concat(args.map(arg => {
        return {
          type: `argument` as 'argument',
github yarnpkg / berry / packages / plugin-node-modules / sources / NodeModulesLinker.ts View on Github external
const readLocatorState = async (locatorStatePath: PortablePath): Promise => {
  const locatorMap: NodeModulesLocatorMap = new Map();
  const locatorState = parseSyml(await xfs.readFilePromise(locatorStatePath, `utf8`));
  delete locatorState.__metadata;
  for (const [key, val] of Object.entries(locatorState)) {
    locatorMap.set(key, {
      size: +val.size,
      target: PortablePath.dot,
      linkType: LinkType.HARD,
      locations: val.locations,
    });
  }

  return locatorMap;
};
github yarnpkg / berry / packages / yarnpkg-core / sources / Configuration.ts View on Github external
const rcFiles = [];

    let nextCwd = startingCwd;
    let currentCwd = null;

    while (nextCwd !== currentCwd) {
      currentCwd = nextCwd;

      const rcPath = ppath.join(currentCwd, rcFilename as PortablePath);

      if (xfs.existsSync(rcPath)) {
        const content = await xfs.readFilePromise(rcPath, `utf8`);

        let data;
        try {
          data = parseSyml(content) as any;
        } catch (error) {
          let tip = ``;

          if (content.match(/^\s+(?!-)[^:]+\s+\S+/m))
            tip = ` (in particular, make sure you list the colons after each key name)`;

          throw new UsageError(`Parse error when loading ${rcPath}; please check it's proper Yaml${tip}`);
        }

        rcFiles.push({path: rcPath, cwd: currentCwd, data});
      }

      nextCwd = ppath.dirname(currentCwd);
    }

    return rcFiles;
github yarnpkg / berry / packages / plugin-essentials / sources / commands / install.ts View on Github external
const file = await xfs.readFilePromise(lockfilePath, `utf8`);
  if (!file.includes(MERGE_CONFLICT_START))
    return false;

  if (immutable)
    throw new ReportError(MessageName.AUTOMERGE_IMMUTABLE, `Cannot autofix a lockfile when running an immutable install`);

  const [left, right] = getVariants(file);

  let parsedLeft;
  let parsedRight;

  try {
    parsedLeft = parseSyml(left);
    parsedRight = parseSyml(right);
  } catch (error) {
    throw new ReportError(MessageName.AUTOMERGE_FAILED_TO_PARSE, `The individual variants of the lockfile failed to parse`);
  }

  const merged = Object.assign({}, parsedLeft, parsedRight);
  const serialized = stringifySyml(merged);

  await xfs.changeFilePromise(lockfilePath, serialized);

  return true;
}
github yarnpkg / berry / packages / plugin-essentials / sources / commands / install.ts View on Github external
return false;

  const file = await xfs.readFilePromise(lockfilePath, `utf8`);
  if (!file.includes(MERGE_CONFLICT_START))
    return false;

  if (immutable)
    throw new ReportError(MessageName.AUTOMERGE_IMMUTABLE, `Cannot autofix a lockfile when running an immutable install`);

  const [left, right] = getVariants(file);

  let parsedLeft;
  let parsedRight;

  try {
    parsedLeft = parseSyml(left);
    parsedRight = parseSyml(right);
  } catch (error) {
    throw new ReportError(MessageName.AUTOMERGE_FAILED_TO_PARSE, `The individual variants of the lockfile failed to parse`);
  }

  const merged = Object.assign({}, parsedLeft, parsedRight);
  const serialized = stringifySyml(merged);

  await xfs.changeFilePromise(lockfilePath, serialized);

  return true;
}
github yarnpkg / berry / packages / yarnpkg-core / sources / Configuration.ts View on Github external
static async updateConfiguration(cwd: PortablePath, patch: any) {
    const rcFilename = getRcFilename();
    const configurationPath =  ppath.join(cwd, rcFilename as PortablePath);

    const current = xfs.existsSync(configurationPath)
      ? parseSyml(await xfs.readFilePromise(configurationPath, `utf8`)) as any
      : {};

    let patched = false;

    if (typeof patch === `function`)
      patch = patch(current);

    for (const key of Object.keys(patch)) {
      const currentValue = current[key];

      const nextValue = typeof patch[key] === `function`
        ? patch[key](currentValue)
        : patch[key];

      if (currentValue === nextValue)
        continue;
github yarnpkg / berry / packages / yarnpkg-core / sources / Project.ts View on Github external
compatibilityMode: false,
        }),

        linkType: pkg.linkType.toLowerCase(),

        resolution: structUtils.stringifyLocator(pkg),
        checksum: this.storedChecksums.get(pkg.locatorHash),
      };
    }

    const header = `${[
      `# This file is generated by running "yarn install" inside your project.\n`,
      `# Manual changes might be lost - proceed with caution!\n`,
    ].join(``)}\n`;

    return header + stringifySyml(optimizedLockfile);
  }

@yarnpkg/parsers

BSD-2-Clause
Latest version published 8 months ago

Package Health Score

93 / 100
Full package analysis

Similar packages