Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .yarn/versions/cad178bf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/plugin-workspace-tools": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
13 changes: 11 additions & 2 deletions packages/plugin-workspace-tools/sources/commands/foreach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import {formatUtils, miscUtils, structUtils} from '@yarn
import {gitUtils} from '@yarnpkg/plugin-git';
import {Command, Option, Usage, UsageError} from 'clipanion';
import micromatch from 'micromatch';
import {cpus} from 'os';
import os from 'os';
import pLimit from 'p-limit';
import {Writable} from 'stream';
import {WriteStream} from 'tty';
import * as t from 'typanion';

function availableParallelism() {
// TODO: Use os.availableParallelism directly when dropping support for Node.js < 19.4.0
if (`availableParallelism` in os)
// @ts-expect-error - No types yet
return os.availableParallelism();

return Math.max(1, os.cpus().length);
}

// eslint-disable-next-line arca/no-default-export
export default class WorkspacesForeachCommand extends BaseCommand {
static paths = [
Expand Down Expand Up @@ -200,7 +209,7 @@ export default class WorkspacesForeachCommand extends BaseCommand {
const concurrency = this.parallel ?
(this.jobs === `unlimited`
? Infinity
: Number(this.jobs) || Math.max(1, cpus().length / 2))
: Number(this.jobs) || Math.ceil(availableParallelism() / 2))
: 1;

// No need to parallelize if we were explicitly asked for one job
Expand Down
13 changes: 11 additions & 2 deletions packages/yarnpkg-core/sources/WorkerPool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {cpus} from 'os';
import os from 'os';
import PLimit from 'p-limit';
import {Worker} from 'worker_threads';

Expand All @@ -8,9 +8,18 @@ type PoolWorker<TOut> = Worker & {
[kTaskInfo]: null | { resolve: (value: TOut) => void, reject: (reason?: any) => void };
};

function availableParallelism(): number {
// TODO: Use os.availableParallelism directly when dropping support for Node.js < 19.4.0
if (`availableParallelism` in os)
// @ts-expect-error - No types yet
return os.availableParallelism();

return Math.max(1, os.cpus().length);
}

export class WorkerPool<TIn, TOut> {
private workers: Array<PoolWorker<TOut>> = [];
private limit = PLimit(Math.max(1, cpus().length));
private limit = PLimit(availableParallelism());
private cleanupInterval: ReturnType<typeof setInterval>;

constructor(private source: string) {
Expand Down