How to use @yarnpkg/core - 10 common examples

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-workspace-tools / sources / commands / foreach.ts View on Github external
let interlaced = this.interlaced;

    // No need to buffer the output if we're executing the commands sequentially
    if (!this.parallel)
      interlaced = true;

    const needsProcessing = new Map();
    const processing = new Set();

    const concurrency = this.parallel ? Math.max(1, cpus().length / 2) : 1;
    const limit = pLimit(this.jobs || concurrency);

    let commandCount = 0;

    const report = await StreamReport.start({
      configuration,
      stdout: this.context.stdout,
    }, async report => {
      const runCommand = async (workspace: Workspace, {commandIndex}: {commandIndex: number}) => {
        if (!this.parallel && this.verbose && commandIndex > 1)
          report.reportSeparator();

        const prefix = getPrefix(workspace, {configuration, verbose: this.verbose, commandIndex});

        const [stdout, stdoutEnd] = createStream(report, {prefix, interlaced});
        const [stderr, stderrEnd] = createStream(report, {prefix, interlaced});

        try {
          const exitCode = (await this.cli.run([this.commandName, ...this.args], {
            cwd: workspace.cwd,
            stdout,
github yarnpkg / berry / packages / plugin-essentials / sources / commands / run.ts View on Github external
async execute() {
    const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
    const {project, workspace, locator} = await Project.find(configuration, this.context.cwd);

    const effectiveLocator = this.topLevel
      ? project.topLevelWorkspace.anchoredLocator
      : locator;

    // First we check to see whether a script exist inside the current package
    // for the given name

    if (!this.binariesOnly && await scriptUtils.hasPackageScript(effectiveLocator, this.scriptName, {project}))
      return await scriptUtils.executePackageScript(effectiveLocator, this.scriptName, this.args, {project, stdin: this.context.stdin, stdout: this.context.stdout, stderr: this.context.stderr});

    // If we can't find it, we then check whether one of the dependencies of the
    // current package exports a binary with the requested name

    const binaries = await scriptUtils.getPackageAccessibleBinaries(effectiveLocator, {project});
github yarnpkg / berry / packages / yarnpkg-cli / sources / main.ts View on Github external
async function exec(cli: Cli): Promise {
    // Since we only care about a few very specific settings (yarn-path and ignore-path) we tolerate extra configuration key.
    // If we didn't, we wouldn't even be able to run `yarn config` (which is recommended in the invalid config error message)
    const configuration = await Configuration.find(npath.toPortablePath(process.cwd()), pluginConfiguration, {
      strict: false,
    });

    const yarnPath: PortablePath = configuration.get(`yarnPath`);
    const ignorePath = configuration.get(`ignorePath`);

    if (yarnPath !== null && !ignorePath) {
      if (!xfs.existsSync(yarnPath)) {
        process.stdout.write(cli.error(new Error(`The "yarn-path" option has been set (in ${configuration.sources.get(`yarnPath`)}), but the specified location doesn't exist (${yarnPath}).`)));
        process.exitCode = 1;
      } else {
        try {
          runBinary(yarnPath);
        } catch (error) {
          process.exitCode = error.code || 1;
        }
github yarnpkg / berry / packages / plugin-essentials / sources / commands / why.ts View on Github external
const printAllDependents = (pkg: Package, tree: TreeNode, range: string | null) => {
    if (!dependents.has(pkg.locatorHash))
      return;

    const label = range !== null
      ? `${structUtils.prettyLocator(configuration, pkg)} (via ${structUtils.prettyRange(configuration, range)})`
      : `${structUtils.prettyLocator(configuration, pkg)}`;

    const node = {} as TreeNode;
    tree[label] = node;

    // We don't want to reprint the children for a package that already got
    // printed as part of another branch
    if (printed.has(pkg.locatorHash))
      return;

    printed.add(pkg.locatorHash);

    // We don't want to print the children of our transitive workspace
    // dependencies, as they will be printed in their own top-level branch
    if (range !== null && project.tryWorkspaceByLocator(pkg))
      return;
github yarnpkg / berry / packages / plugin-essentials / sources / commands / add.ts View on Github external
async execute() {
    const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
    const {project, workspace} = await Project.find(configuration, this.context.cwd);
    const cache = await Cache.find(configuration);

    if (!workspace)
      throw new WorkspaceRequiredError(this.context.cwd);

    // @ts-ignore
    const prompt = inquirer.createPromptModule({
      input: this.context.stdin,
      output: this.context.stdout,
    });

    const modifier = this.exact
      ? suggestUtils.Modifier.EXACT
      : this.tilde
        ? suggestUtils.Modifier.TILDE
        : suggestUtils.Modifier.CARET;
github yarnpkg / berry / packages / plugin-essentials / sources / commands / up.ts View on Github external
async execute() {
    const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
    const {project, workspace} = await Project.find(configuration, this.context.cwd);
    const cache = await Cache.find(configuration);

    if (!workspace)
      throw new WorkspaceRequiredError(this.context.cwd);

    // @ts-ignore
    const prompt = inquirer.createPromptModule({
      input: this.context.stdin,
      output: this.context.stdout,
    });

    const modifier = this.exact
      ? suggestUtils.Modifier.EXACT
      : this.tilde
        ? suggestUtils.Modifier.TILDE
        : suggestUtils.Modifier.CARET;
github yarnpkg / berry / packages / plugin-stage / sources / drivers / GitDriver.ts View on Github external
} else if (currDep.range !== value.range) {
          actions.push([stageUtils.ActionType.MODIFY, `${pkgName} to ${currDep.range}`]);
        }
      }

      for (const [indentHash, value] of allCurrDeps) {
        if (!allPrevDeps.has(indentHash)) {
          actions.push([stageUtils.ActionType.ADD, structUtils.stringifyIdent(value)]);
        }
      }
    } else if (action === stageUtils.ActionType.CREATE) {
      // New package.json
      const manifest = await Manifest.fromFile(path)

      if (manifest.name) {
        actions.push([stageUtils.ActionType.CREATE, structUtils.stringifyIdent(manifest.name)])
      } else {
        actions.push([stageUtils.ActionType.CREATE, `a package`])
      }
    } else if (action === stageUtils.ActionType.DELETE) {
      const commitHash = await getLastCommitHash(cwd);
      const {stdout: prevSource} = await execUtils.execvp(`git`, [`show`, `${commitHash}:${relativePath}`], {cwd, strict: true});

      // Deleted package.json; we need to load it from its past sources
      const manifest = await Manifest.fromText(prevSource);

      if (manifest.name) {
        actions.push([stageUtils.ActionType.DELETE, structUtils.stringifyIdent(manifest.name)]);
      } else {
        actions.push([stageUtils.ActionType.DELETE, `a package`]);
      }
    } else {
github yarnpkg / berry / packages / plugin-stage / sources / drivers / GitDriver.ts View on Github external
for (const {action, path} of modifiedPkgJsonFiles) {
    const relativePath = ppath.relative(cwd, path);

    if (action === stageUtils.ActionType.MODIFY) {
      const commitHash = await getLastCommitHash(cwd)
      const {stdout: prevSource} = await execUtils.execvp(`git`, [`show`, `${commitHash}:${relativePath}`], {cwd, strict: true});

      const prevManifest = await Manifest.fromText(prevSource);
      const currManifest = await Manifest.fromFile(path);

      const allCurrDeps: Map = new Map([...currManifest.dependencies, ...currManifest.devDependencies]);
      const allPrevDeps: Map = new Map([...prevManifest.dependencies, ...prevManifest.devDependencies]);

      for (const [indentHash, value] of allPrevDeps) {
        const pkgName = structUtils.stringifyIdent(value);
        const currDep = allCurrDeps.get(indentHash);

        if (!currDep) {
          actions.push([stageUtils.ActionType.REMOVE, pkgName])
        } else if (currDep.range !== value.range) {
          actions.push([stageUtils.ActionType.MODIFY, `${pkgName} to ${currDep.range}`]);
        }
      }

      for (const [indentHash, value] of allCurrDeps) {
        if (!allPrevDeps.has(indentHash)) {
          actions.push([stageUtils.ActionType.ADD, structUtils.stringifyIdent(value)]);
        }
      }
    } else if (action === stageUtils.ActionType.CREATE) {
      // New package.json
github yarnpkg / berry / packages / plugin-stage / sources / drivers / GitDriver.ts View on Github external
const allPrevDeps: Map = new Map([...prevManifest.dependencies, ...prevManifest.devDependencies]);

      for (const [indentHash, value] of allPrevDeps) {
        const pkgName = structUtils.stringifyIdent(value);
        const currDep = allCurrDeps.get(indentHash);

        if (!currDep) {
          actions.push([stageUtils.ActionType.REMOVE, pkgName])
        } else if (currDep.range !== value.range) {
          actions.push([stageUtils.ActionType.MODIFY, `${pkgName} to ${currDep.range}`]);
        }
      }

      for (const [indentHash, value] of allCurrDeps) {
        if (!allPrevDeps.has(indentHash)) {
          actions.push([stageUtils.ActionType.ADD, structUtils.stringifyIdent(value)]);
        }
      }
    } else if (action === stageUtils.ActionType.CREATE) {
      // New package.json
      const manifest = await Manifest.fromFile(path)

      if (manifest.name) {
        actions.push([stageUtils.ActionType.CREATE, structUtils.stringifyIdent(manifest.name)])
      } else {
        actions.push([stageUtils.ActionType.CREATE, `a package`])
      }
    } else if (action === stageUtils.ActionType.DELETE) {
      const commitHash = await getLastCommitHash(cwd);
      const {stdout: prevSource} = await execUtils.execvp(`git`, [`show`, `${commitHash}:${relativePath}`], {cwd, strict: true});

      // Deleted package.json; we need to load it from its past sources
github yarnpkg / berry / packages / plugin-version / sources / commands / version / apply.ts View on Github external
async execute() {
    const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
    const {project, workspace} = await Project.find(configuration, this.context.cwd);
    const cache = await Cache.find(configuration);

    if (!workspace)
      throw new WorkspaceRequiredError(this.context.cwd);

    const applyReport = await StreamReport.start({
      configuration,
      json: this.json,
      stdout: this.context.stdout,
    }, async report => {
      const allDependents: Map> = new Map();

      // First we compute the reverse map to figure out which workspace is
      // depended upon by which other.
      //
      // Note that we need to do this before applying the new versions,
      // otherwise the `findWorkspacesByDescriptor` calls won't be able to
      // resolve the workspaces anymore (because the workspace versions will