Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.mocharc.jsonc
.mocharc.jsonc
CHANGELOG.md
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,19 @@ const localInstaller = new LocalInstaller(
{ npmEnv: Object.assign({}, process.env, { envVar: 'envValue' }) },
);
```

##### Max concurrent installs

In some cases it might be useful to control the max concurrent installs. You can do it by passing `options` to `LocalInstaller`'s constructor.

```javascript
const localInstaller = new LocalInstaller(
{
/*1*/ '.': ['../sibling1', '../sibling2'],
/*2*/ '../dependant': ['.'],
},
{ concurrent: 1 },
);
```

The default concurrency is `os.cpus().length`.
12 changes: 10 additions & 2 deletions src/LocalInstaller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from 'events';
import { promises as fs } from 'fs';
import os from 'os';
import path from 'path';
import { helpers } from './helpers.ts';
import type { InstallTarget, PackageJson } from './index.ts';
Expand All @@ -17,7 +18,8 @@ export interface Env {

export interface Options {
npmEnv?: Env;
packageManager: PackageManager | undefined;
packageManager?: PackageManager;
concurrent?: number;
}

export interface ListByPackage {
Expand Down Expand Up @@ -82,7 +84,13 @@ export class LocalInstaller extends EventEmitter {

private async installAll(installTargets: InstallTarget[]): Promise<void> {
this.emit('install_start', this.sourcesByTarget);
await Promise.all(installTargets.map((target) => this.installOne(target)));
// Install targets with max concurrent operations
const batchSize = this.options.concurrent ?? os.cpus().length;
for (let i = 0; i < installTargets.length; i += batchSize) {
const batch = installTargets.slice(i, i + batchSize);
await Promise.all(batch.map((target) => this.installOne(target)));
}

this.emit('install_end');
}

Expand Down
44 changes: 43 additions & 1 deletion test/unit/LocalInstaller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import type { ResultPromise } from 'execa';
import type { Options, Result, ResultPromise } from 'execa';
import { promises as fs } from 'fs';
import os from 'os';
import { resolve } from 'path';
Expand Down Expand Up @@ -267,6 +267,44 @@ describe('LocalInstaller install', () => {
});
});

describe('with concurrent', () => {
beforeEach(() => {
sut = new LocalInstaller(
{ '/a': ['c'], '/b': ['c'] },
{ packageManager: 'npm', concurrent: 1 },
);
stubPackageJson({
'/a': 'a',
'/b': 'b',
c: 'c',
});
helper.rmStub.resolves();
});

it('should install with the specified concurrency', async () => {
const calls: ((res: Result<Options>) => void)[] = [];
helper.execStub.callsFake((file, args) => {
if (file === 'npm' && args?.includes('pack')) {
return Promise.resolve(createExecaResult()) as ResultPromise;
}
return new Promise((res) => {
calls.push(res);
}) as ResultPromise;
});

const onGoingInstall = sut.install();
await tick();
await tick();
expect(calls).to.have.lengthOf(1);
calls[0](createExecaResult());
await tick();
await tick();
expect(calls).to.have.lengthOf(2);
calls[1](createExecaResult());
await onGoingInstall;
});
});

describe('when readFile errors', () => {
it('should propagate the error', () => {
helper.readFileStub.rejects(new Error('file error'));
Expand Down Expand Up @@ -320,3 +358,7 @@ describe('LocalInstaller install', () => {
});
};
});

function tick(): Promise<void> {
return new Promise((res) => process.nextTick(res));
}