@@ -30,47 +30,17 @@ steps:
3030 - ${{ if ne(parameters.customNPMRegistry, '') }} :
3131 # When using a private/custom registry, configure npm to read auth/config from a temp user config
3232 # instead of relying on a checked-in project .npmrc.
33- - pwsh : |
34- $path = "${{ parameters.npmrcPath }}"
35-
36- if (Test-Path -LiteralPath $path -PathType Container) {
37- throw "npmrcPath points to a directory (expected a file): $path"
38- }
39-
40- $parent = Split-Path -Parent $path
41- if ($parent -and -not (Test-Path -LiteralPath $parent)) {
42- New-Item -ItemType Directory -Path $parent -Force | Out-Null
43- }
44-
45- if (-not (Test-Path -LiteralPath $path -PathType Leaf)) {
46- New-Item -ItemType File -Path $path -Force | Out-Null
47- }
48-
49- Write-Host "##vso[task.setvariable variable=NPM_CONFIG_USERCONFIG]$path"
50- Write-Host "##vso[task.setvariable variable=NPM_CONFIG_REGISTRY]${{ parameters.customNPMRegistry }}"
33+ - pwsh : >
34+ $(Build.SourcesDirectory)/build/scripts/ensure-npm-userconfig.ps1
35+ -Path "${{ parameters.npmrcPath }}"
36+ -Registry "${{ parameters.customNPMRegistry }}"
5137 displayName: 📦 Setup NPM User Config
5238
5339 # Configure npm/yarn to use the custom registry and ensure auth headers are sent.
54- - pwsh : |
55- $path = "${{ parameters.npmrcPath }}"
56- $registry = "${{ parameters.customNPMRegistry }}"
57-
58- $env:NPM_CONFIG_USERCONFIG = $path
59- $env:NPM_CONFIG_REGISTRY = $registry
60-
61- npm config set registry "$registry"
62-
63- # npm >v7 deprecated the `always-auth` config option, refs npm/cli@72a7eeb
64- # following is a workaround for yarn-like clients to send authorization header for GET
65- # requests to the registry.
66- "always-auth=true" | Out-File -FilePath $path -Append -Encoding utf8
67-
68- $yarn = Get-Command yarn -ErrorAction SilentlyContinue
69- if ($null -ne $yarn) {
70- yarn config set registry "$registry"
71- } else {
72- Write-Host "yarn not found; skipping yarn registry configuration"
73- }
40+ - pwsh : >
41+ $(Build.SourcesDirectory)/build/scripts/setup-npm-and-yarn.ps1
42+ -NpmrcPath "${{ parameters.npmrcPath }}"
43+ -Registry "${{ parameters.customNPMRegistry }}"
7444 displayName: 📦 Setup NPM & Yarn
7545
7646 # Populate the temp .npmrc with auth for the configured registry.
@@ -79,58 +49,17 @@ steps:
7949 workingFile : ${{ parameters.npmrcPath }}
8050 displayName : 📦 Setup NPM Authentication
8151
52+ # Ensure the registry always sends auth headers (npmAuthenticate may overwrite the file).
53+ - pwsh : >
54+ $(Build.SourcesDirectory)/build/scripts/finalize-npm-config.ps1
55+ -Path "${{ parameters.npmrcPath }}"
56+ displayName: 📦 Finalize NPM config
57+
8258 # Some lockfiles contain hardcoded references to public registries. Rewrite them so installs
8359 # and `npx` resolve from the custom registry consistently.
84- - pwsh : |
85- $registry = "${{ parameters.customNPMRegistry }}"
86- $env:NPM_CONFIG_REGISTRY = $registry
87- $scriptPath = Join-Path "$(Agent.TempDirectory)" 'setup-npm-registry.js'
88-
89- $lines = @(
90- "const fs = require('fs').promises;",
91- "const path = require('path');",
92- "",
93- "async function* getLockFiles(dir) {",
94- " const files = await fs.readdir(dir);",
95- "",
96- " for (const file of files) {",
97- " const fullPath = path.join(dir, file);",
98- " const stat = await fs.stat(fullPath);",
99- "",
100- " if (stat.isDirectory()) {",
101- " if (file === 'node_modules' || file === '.git') {",
102- " continue;",
103- " }",
104- " yield* getLockFiles(fullPath);",
105- " } else if (file === 'yarn.lock' || file === 'package-lock.json') {",
106- " yield fullPath;",
107- " }",
108- " }",
109- "}",
110- "",
111- "async function rewrite(file, registry) {",
112- " let contents = await fs.readFile(file, 'utf8');",
113- " const re = new RegExp('https://registry\\\\.[^.]+\\\\.(com|org)/', 'g');",
114- " contents = contents.replace(re, registry);",
115- " await fs.writeFile(file, contents);",
116- "}",
117- "",
118- "async function main() {",
119- " const root = process.cwd();",
120- " const registry = process.env.NPM_CONFIG_REGISTRY;",
121- " if (!registry) { throw new Error('NPM_CONFIG_REGISTRY is not set'); }",
122- "",
123- " for await (const file of getLockFiles(root)) {",
124- " await rewrite(file, registry);",
125- " console.log('Updated node registry:', file);",
126- " }",
127- "}",
128- "",
129- "main();"
130- )
131-
132- Set-Content -LiteralPath $scriptPath -Value ($lines -join "`n") -Encoding utf8
133- node $scriptPath
60+ - pwsh : >
61+ $(Build.SourcesDirectory)/build/scripts/setup-npm-registry.ps1
62+ -Registry "${{ parameters.customNPMRegistry }}"
13463 displayName: 📦 Setup NPM Registry
13564
13665 - script : npm config get registry
0 commit comments