Skip to content
Open
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
88 changes: 57 additions & 31 deletions packages/angular/cli/src/package-managers/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export class PackageManager {
readonly #initializationError?: Error;
#dependencyCache: Map<string, InstalledPackage> | null = null;
#version: string | undefined;
#activeTasks = 0;
readonly #pendingTasks: (() => void)[] = [];
readonly #maxConcurrent = 5;

/**
* Creates a new `PackageManager` instance.
Expand Down Expand Up @@ -159,49 +162,72 @@ export class PackageManager {
* @param options Options for the child process.
* @returns A promise that resolves with the standard output and standard error of the command.
*/
async #runWithThrottle<T>(action: () => Promise<T>): Promise<T> {
if (this.#activeTasks >= this.#maxConcurrent) {
await new Promise<void>((resolve) => {
this.#pendingTasks.push(resolve);
});
} else {
this.#activeTasks++;
}

try {
return await action();
} finally {
const next = this.#pendingTasks.shift();
if (next) {
next();
} else {
this.#activeTasks--;
}
}
}
Comment thread
clydin marked this conversation as resolved.

async #run(
args: readonly string[],
options: { timeout?: number; registry?: string; cwd?: string } = {},
): Promise<{ stdout: string; stderr: string }> {
this.ensureInstalled();
return this.#runWithThrottle(async () => {
this.ensureInstalled();

const { registry, cwd, ...runOptions } = options;
const finalArgs = [...args];
let finalEnv: Record<string, string> | undefined;

if (registry) {
const registryOptions = this.descriptor.getRegistryOptions?.(registry);
if (!registryOptions) {
throw new Error(
`The configured package manager, '${this.descriptor.binary}', does not support a custom registry.`,
);
}

const { registry, cwd, ...runOptions } = options;
const finalArgs = [...args];
let finalEnv: Record<string, string> | undefined;
if (registryOptions.args) {
finalArgs.push(...registryOptions.args);
}
if (registryOptions.env) {
finalEnv = registryOptions.env;
}
}

if (registry) {
const registryOptions = this.descriptor.getRegistryOptions?.(registry);
if (!registryOptions) {
throw new Error(
`The configured package manager, '${this.descriptor.binary}', does not support a custom registry.`,
const executionDirectory = cwd ?? this.cwd;
if (this.options.dryRun) {
this.options.logger?.info(
`[DRY RUN] Would execute in [${executionDirectory}]: ${this.descriptor.binary} ${finalArgs.join(' ')}`,
);
}

if (registryOptions.args) {
finalArgs.push(...registryOptions.args);
}
if (registryOptions.env) {
finalEnv = registryOptions.env;
return { stdout: '', stderr: '' };
}
}

const executionDirectory = cwd ?? this.cwd;
if (this.options.dryRun) {
this.options.logger?.info(
`[DRY RUN] Would execute in [${executionDirectory}]: ${this.descriptor.binary} ${finalArgs.join(' ')}`,
);
const commandResult = await this.host.runCommand(this.descriptor.binary, finalArgs, {
...runOptions,
cwd: executionDirectory,
stdio: 'pipe',
env: finalEnv,
});

return { stdout: '', stderr: '' };
}

const commandResult = await this.host.runCommand(this.descriptor.binary, finalArgs, {
...runOptions,
cwd: executionDirectory,
stdio: 'pipe',
env: finalEnv,
return { stdout: commandResult.stdout.trim(), stderr: commandResult.stderr.trim() };
});

return { stdout: commandResult.stdout.trim(), stderr: commandResult.stderr.trim() };
}

/**
Expand Down
Loading