Skip to content

Commit f48e7d6

Browse files
committed
refactor(@angular/cli): support custom temporary directory in package manager abstraction
This change introduces the ability to specify a base temporary directory when creating a `PackageManager` instance. This allows for creating temporary directories within the project structure (e.g., `node_modules/.tmp`), which enables package managers to correctly inherit project-specific configurations like `.npmrc` or `.yarnrc` during operations like `acquireTempPackage`. (cherry picked from commit 5fb3af0)
1 parent 059123f commit f48e7d6

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

packages/angular/cli/src/package-managers/factory.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ export async function createPackageManager(options: {
110110
configuredPackageManager?: PackageManagerName;
111111
logger?: Logger;
112112
dryRun?: boolean;
113+
tempDirectory?: string;
113114
}): Promise<PackageManager> {
114-
const { cwd, configuredPackageManager, logger, dryRun } = options;
115+
const { cwd, configuredPackageManager, logger, dryRun, tempDirectory } = options;
115116
const host = NodeJS_HOST;
116117

117118
const { name, source } = await determinePackageManager(
@@ -127,7 +128,11 @@ export async function createPackageManager(options: {
127128
throw new Error(`Unsupported package manager: "${name}"`);
128129
}
129130

130-
const packageManager = new PackageManager(host, cwd, descriptor, { dryRun, logger });
131+
const packageManager = new PackageManager(host, cwd, descriptor, {
132+
dryRun,
133+
logger,
134+
tempDirectory,
135+
});
131136

132137
// Do not verify if the package manager is installed during a dry run.
133138
if (!dryRun) {

packages/angular/cli/src/package-managers/host.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ export interface Host {
4747

4848
/**
4949
* Creates a new, unique temporary directory.
50+
* @param baseDir The base directory in which to create the temporary directory.
5051
* @returns A promise that resolves to the absolute path of the created directory.
5152
*/
52-
createTempDirectory(): Promise<string>;
53+
createTempDirectory(baseDir?: string): Promise<string>;
5354

5455
/**
5556
* Deletes a directory recursively.
@@ -94,7 +95,7 @@ export const NodeJS_HOST: Host = {
9495
readdir,
9596
readFile: (path: string) => readFile(path, { encoding: 'utf8' }),
9697
writeFile,
97-
createTempDirectory: () => mkdtemp(join(tmpdir(), 'angular-cli-')),
98+
createTempDirectory: (baseDir?: string) => mkdtemp(join(baseDir ?? tmpdir(), 'angular-cli-')),
9899
deleteDirectory: (path: string) => rm(path, { recursive: true, force: true }),
99100
runCommand: async (
100101
command: string,

packages/angular/cli/src/package-managers/package-manager.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export interface PackageManagerOptions {
5959

6060
/** A logger instance for debugging and dry run output. */
6161
logger?: Logger;
62+
63+
/**
64+
* The path to use as the base for temporary directories.
65+
* If not specified, the system's temporary directory will be used.
66+
*/
67+
tempDirectory?: string;
6268
}
6369

6470
/**
@@ -538,7 +544,7 @@ export class PackageManager {
538544
specifier: string,
539545
options: { registry?: string; ignoreScripts?: boolean } = {},
540546
): Promise<{ workingDirectory: string; cleanup: () => Promise<void> }> {
541-
const workingDirectory = await this.host.createTempDirectory();
547+
const workingDirectory = await this.host.createTempDirectory(this.options.tempDirectory);
542548
const cleanup = () => this.host.deleteDirectory(workingDirectory);
543549

544550
// Some package managers, like yarn classic, do not write a package.json when adding a package.

0 commit comments

Comments
 (0)