|
15 | 15 |
|
16 | 16 | import { type SpawnOptions, spawn } from 'node:child_process'; |
17 | 17 | import { Stats } from 'node:fs'; |
18 | | -import { mkdtemp, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises'; |
19 | | -import { platform, tmpdir } from 'node:os'; |
| 18 | +import { mkdir, copyFile, mkdtemp, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises'; |
| 19 | +import { platform } from 'node:os'; |
20 | 20 | import { join } from 'node:path'; |
21 | 21 | import { PackageManagerError } from './error'; |
22 | 22 |
|
@@ -88,67 +88,80 @@ export interface Host { |
88 | 88 |
|
89 | 89 | /** |
90 | 90 | * A concrete implementation of the `Host` interface that uses the Node.js APIs. |
| 91 | + * @param root The root directory of the project. |
| 92 | + * @param cacheDirectory The directory to use for caching. |
| 93 | + * @returns A host that uses the Node.js APIs. |
91 | 94 | */ |
92 | | -export const NodeJS_HOST: Host = { |
93 | | - stat, |
94 | | - readdir, |
95 | | - readFile: (path: string) => readFile(path, { encoding: 'utf8' }), |
96 | | - writeFile, |
97 | | - createTempDirectory: () => mkdtemp(join(tmpdir(), 'angular-cli-')), |
98 | | - deleteDirectory: (path: string) => rm(path, { recursive: true, force: true }), |
99 | | - runCommand: async ( |
100 | | - command: string, |
101 | | - args: readonly string[], |
102 | | - options: { |
103 | | - timeout?: number; |
104 | | - stdio?: 'pipe' | 'ignore'; |
105 | | - cwd?: string; |
106 | | - env?: Record<string, string>; |
107 | | - } = {}, |
108 | | - ): Promise<{ stdout: string; stderr: string }> => { |
109 | | - const signal = options.timeout ? AbortSignal.timeout(options.timeout) : undefined; |
110 | | - const isWin32 = platform() === 'win32'; |
111 | | - |
112 | | - return new Promise((resolve, reject) => { |
113 | | - const spawnOptions = { |
114 | | - shell: isWin32, |
115 | | - stdio: options.stdio ?? 'pipe', |
116 | | - signal, |
117 | | - cwd: options.cwd, |
118 | | - env: { |
119 | | - ...process.env, |
120 | | - ...options.env, |
121 | | - }, |
122 | | - } satisfies SpawnOptions; |
123 | | - const childProcess = isWin32 |
124 | | - ? spawn(`${command} ${args.join(' ')}`, spawnOptions) |
125 | | - : spawn(command, args, spawnOptions); |
126 | | - |
127 | | - let stdout = ''; |
128 | | - childProcess.stdout?.on('data', (data) => (stdout += data.toString())); |
129 | | - |
130 | | - let stderr = ''; |
131 | | - childProcess.stderr?.on('data', (data) => (stderr += data.toString())); |
132 | | - |
133 | | - childProcess.on('close', (code) => { |
134 | | - if (code === 0) { |
135 | | - resolve({ stdout, stderr }); |
136 | | - } else { |
137 | | - const message = `Process exited with code ${code}.`; |
138 | | - reject(new PackageManagerError(message, stdout, stderr, code)); |
139 | | - } |
140 | | - }); |
141 | | - |
142 | | - childProcess.on('error', (err) => { |
143 | | - if (err.name === 'AbortError') { |
144 | | - const message = `Process timed out.`; |
| 95 | +export function createNodeJsHost(root: string, cacheDirectory: string): Host { |
| 96 | + return { |
| 97 | + stat, |
| 98 | + readdir, |
| 99 | + readFile: (path: string) => readFile(path, { encoding: 'utf8' }), |
| 100 | + writeFile, |
| 101 | + createTempDirectory: async () => { |
| 102 | + await mkdir(cacheDirectory, { recursive: true }); |
| 103 | + const tmpDir = await mkdtemp(join(cacheDirectory, 'package-manager-temp-')); |
| 104 | + |
| 105 | + // Copy the .npmrc file to the temp directory if it exists. |
| 106 | + await copyFile(`${root}/.npmrc`, `${tmpDir}/.npmrc`).catch(() => {}); |
| 107 | + |
| 108 | + return tmpDir; |
| 109 | + }, |
| 110 | + deleteDirectory: (path: string) => rm(path, { recursive: true, force: true }), |
| 111 | + runCommand: async ( |
| 112 | + command: string, |
| 113 | + args: readonly string[], |
| 114 | + options: { |
| 115 | + timeout?: number; |
| 116 | + stdio?: 'pipe' | 'ignore'; |
| 117 | + cwd?: string; |
| 118 | + env?: Record<string, string>; |
| 119 | + } = {}, |
| 120 | + ): Promise<{ stdout: string; stderr: string }> => { |
| 121 | + const signal = options.timeout ? AbortSignal.timeout(options.timeout) : undefined; |
| 122 | + const isWin32 = platform() === 'win32'; |
| 123 | + |
| 124 | + return new Promise((resolve, reject) => { |
| 125 | + const spawnOptions = { |
| 126 | + shell: isWin32, |
| 127 | + stdio: options.stdio ?? 'pipe', |
| 128 | + signal, |
| 129 | + cwd: options.cwd, |
| 130 | + env: { |
| 131 | + ...process.env, |
| 132 | + ...options.env, |
| 133 | + }, |
| 134 | + } satisfies SpawnOptions; |
| 135 | + const childProcess = isWin32 |
| 136 | + ? spawn(`${command} ${args.join(' ')}`, spawnOptions) |
| 137 | + : spawn(command, args, spawnOptions); |
| 138 | + |
| 139 | + let stdout = ''; |
| 140 | + childProcess.stdout?.on('data', (data) => (stdout += data.toString())); |
| 141 | + |
| 142 | + let stderr = ''; |
| 143 | + childProcess.stderr?.on('data', (data) => (stderr += data.toString())); |
| 144 | + |
| 145 | + childProcess.on('close', (code) => { |
| 146 | + if (code === 0) { |
| 147 | + resolve({ stdout, stderr }); |
| 148 | + } else { |
| 149 | + const message = `Process exited with code ${code}.`; |
| 150 | + reject(new PackageManagerError(message, stdout, stderr, code)); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + childProcess.on('error', (err) => { |
| 155 | + if (err.name === 'AbortError') { |
| 156 | + const message = `Process timed out.`; |
| 157 | + reject(new PackageManagerError(message, stdout, stderr, null)); |
| 158 | + |
| 159 | + return; |
| 160 | + } |
| 161 | + const message = `Process failed with error: ${err.message}`; |
145 | 162 | reject(new PackageManagerError(message, stdout, stderr, null)); |
146 | | - |
147 | | - return; |
148 | | - } |
149 | | - const message = `Process failed with error: ${err.message}`; |
150 | | - reject(new PackageManagerError(message, stdout, stderr, null)); |
| 163 | + }); |
151 | 164 | }); |
152 | | - }); |
153 | | - }, |
154 | | -}; |
| 165 | + }, |
| 166 | + }; |
| 167 | +} |
0 commit comments