|
1 | 1 | import { execFileSync } from 'child_process'; |
2 | 2 | import { createHash } from 'crypto'; |
3 | | -import { existsSync, mkdirSync, writeFileSync } from 'fs'; |
4 | | -import { join, resolve } from 'path'; |
| 3 | +import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'fs'; |
| 4 | +import { dirname, join, resolve } from 'path'; |
5 | 5 |
|
6 | 6 | import type { CdsDependencyCombination } from './types'; |
7 | 7 | import { CdsDependencyGraph, CdsProject } from '../cds/parser/types'; |
@@ -48,6 +48,61 @@ function addDependencyVersionWarning( |
48 | 48 | } |
49 | 49 | } |
50 | 50 |
|
| 51 | +/** |
| 52 | + * Find the nearest `.npmrc` file by searching the given directory and its |
| 53 | + * ancestors up to (and including) the filesystem root. npm itself walks the |
| 54 | + * directory tree when looking for project-level `.npmrc` files, so we mirror |
| 55 | + * that behaviour here. |
| 56 | + * |
| 57 | + * @param startDir The directory from which to start the upward search. |
| 58 | + * @returns The absolute path to the nearest `.npmrc`, or `undefined` if none is found. |
| 59 | + */ |
| 60 | +export function findNearestNpmrc(startDir: string): string | undefined { |
| 61 | + let current = resolve(startDir); |
| 62 | + |
| 63 | + // Walk up the directory tree until we find an .npmrc or reach the root |
| 64 | + |
| 65 | + while (true) { |
| 66 | + const candidate = join(current, '.npmrc'); |
| 67 | + if (existsSync(candidate)) { |
| 68 | + return candidate; |
| 69 | + } |
| 70 | + const parent = dirname(current); |
| 71 | + if (parent === current) { |
| 72 | + // Reached filesystem root without finding .npmrc |
| 73 | + return undefined; |
| 74 | + } |
| 75 | + current = parent; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Copy the project's `.npmrc` file (if any) into the cache directory so that |
| 81 | + * `npm install` inside the cache respects custom registry configuration such |
| 82 | + * as scoped registries (`@sap:registry=...`), authentication tokens, and |
| 83 | + * `strict-ssl` settings. |
| 84 | + * |
| 85 | + * @param cacheDir The cache directory where dependencies will be installed. |
| 86 | + * @param projectDir Absolute path to the project directory whose `.npmrc` should be used. |
| 87 | + */ |
| 88 | +export function copyNpmrcToCache(cacheDir: string, projectDir: string): void { |
| 89 | + const npmrcPath = findNearestNpmrc(projectDir); |
| 90 | + if (!npmrcPath) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const dest = join(cacheDir, '.npmrc'); |
| 95 | + try { |
| 96 | + copyFileSync(npmrcPath, dest); |
| 97 | + cdsExtractorLog('info', `Copied .npmrc from '${npmrcPath}' to cache directory '${cacheDir}'`); |
| 98 | + } catch (err) { |
| 99 | + cdsExtractorLog( |
| 100 | + 'warn', |
| 101 | + `Failed to copy .npmrc to cache directory: ${err instanceof Error ? err.message : String(err)}`, |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
| 105 | + |
51 | 106 | /** |
52 | 107 | * Install dependencies for CDS projects using a robust cache strategy with fallback logic |
53 | 108 | * @param dependencyGraph The dependency graph of the project |
@@ -206,6 +261,14 @@ export function cacheInstallDependencies( |
206 | 261 | } |
207 | 262 | } |
208 | 263 |
|
| 264 | + // Ensure the cache directory has an .npmrc that reflects the projects' registry configuration |
| 265 | + const npmrcProjectDir = Array.from(dependencyGraph.projects.values()) |
| 266 | + .map(project => project.projectDir) |
| 267 | + .find(projectDir => projectDir && existsSync(join(sourceRoot, projectDir, '.npmrc'))); |
| 268 | + if (npmrcProjectDir) { |
| 269 | + copyNpmrcToCache(cacheDir, join(sourceRoot, npmrcProjectDir)); |
| 270 | + } |
| 271 | + |
209 | 272 | // Try to install dependencies in the cache directory |
210 | 273 | // Get the first project package.json path for diagnostic purposes |
211 | 274 | const samplePackageJsonPath = Array.from(dependencyGraph.projects.values()).find( |
|
0 commit comments