@@ -32,13 +32,37 @@ function jsonParse(input: string) {
32
32
}
33
33
}
34
34
35
- function getCorrectRegistry ( packageJson ?: PackageJSON ) : string {
35
+ interface RegistryInfo {
36
+ scope ?: string ;
37
+ registry : string ;
38
+ }
39
+
40
+ export function getCorrectRegistry ( packageJson ?: PackageJSON ) : RegistryInfo {
41
+ const packageName = packageJson ?. name ;
42
+
43
+ if ( packageName ?. startsWith ( "@" ) ) {
44
+ const scope = packageName . split ( "/" ) [ 0 ] ;
45
+ const scopedRegistry =
46
+ packageJson ! . publishConfig ?. [ `${ scope } :registry` ] ||
47
+ process . env [ `npm_config_${ scope } :registry` ] ;
48
+ if ( scopedRegistry ) {
49
+ return {
50
+ scope,
51
+ registry : scopedRegistry ,
52
+ } ;
53
+ }
54
+ }
55
+
36
56
const registry =
37
- packageJson ?. publishConfig ?. registry ?? process . env . npm_config_registry ;
57
+ packageJson ?. publishConfig ?. registry || process . env . npm_config_registry ;
38
58
39
- return ! registry || registry === "https://registry.yarnpkg.com"
40
- ? "https://registry.npmjs.org"
41
- : registry ;
59
+ return {
60
+ scope : undefined ,
61
+ registry :
62
+ ! registry || registry === "https://registry.yarnpkg.com"
63
+ ? "https://registry.npmjs.org"
64
+ : registry ,
65
+ } ;
42
66
}
43
67
44
68
async function getPublishTool (
@@ -64,10 +88,11 @@ async function getPublishTool(
64
88
}
65
89
66
90
export async function getTokenIsRequired ( ) {
91
+ const { scope, registry } = getCorrectRegistry ( ) ;
67
92
// Due to a super annoying issue in yarn, we have to manually override this env variable
68
93
// See: https://github.com/yarnpkg/yarn/issues/2935#issuecomment-355292633
69
94
const envOverride = {
70
- npm_config_registry : getCorrectRegistry ( ) ,
95
+ [ scope ? `npm_config_ ${ scope } :registry` : " npm_config_registry" ] : registry ,
71
96
} ;
72
97
let result = await spawn ( "npm" , [ "profile" , "get" , "--json" ] , {
73
98
env : Object . assign ( { } , process . env , envOverride ) ,
@@ -90,6 +115,8 @@ export function getPackageInfo(packageJson: PackageJSON) {
90
115
return npmRequestLimit ( async ( ) => {
91
116
info ( `npm info ${ packageJson . name } ` ) ;
92
117
118
+ const { scope, registry } = getCorrectRegistry ( packageJson ) ;
119
+
93
120
// Due to a couple of issues with yarnpkg, we also want to override the npm registry when doing
94
121
// npm info.
95
122
// Issues: We sometimes get back cached responses, i.e old data about packages which causes
@@ -99,8 +126,7 @@ export function getPackageInfo(packageJson: PackageJSON) {
99
126
let result = await spawn ( "npm" , [
100
127
"info" ,
101
128
packageJson . name ,
102
- "--registry" ,
103
- getCorrectRegistry ( packageJson ) ,
129
+ `--${ scope ? `${ scope } :` : "" } registry=${ registry } ` ,
104
130
"--json" ,
105
131
] ) ;
106
132
@@ -161,7 +187,7 @@ export let getOtpCode = async (twoFactorState: TwoFactorState) => {
161
187
// we have this so that we can do try a publish again after a publish without
162
188
// the call being wrapped in the npm request limit and causing the publishes to potentially never run
163
189
async function internalPublish (
164
- pkgName : string ,
190
+ packageJson : PackageJSON ,
165
191
opts : PublishOptions ,
166
192
twoFactorState : TwoFactorState
167
193
) : Promise < { published : boolean } > {
@@ -177,11 +203,14 @@ async function internalPublish(
177
203
publishFlags . push ( "--no-git-checks" ) ;
178
204
}
179
205
206
+ const { scope, registry } = getCorrectRegistry ( packageJson ) ;
207
+
180
208
// Due to a super annoying issue in yarn, we have to manually override this env variable
181
209
// See: https://github.com/yarnpkg/yarn/issues/2935#issuecomment-355292633
182
210
const envOverride = {
183
- npm_config_registry : getCorrectRegistry ( ) ,
211
+ [ scope ? `npm_config_ ${ scope } :registry` : " npm_config_registry" ] : registry ,
184
212
} ;
213
+
185
214
let { code, stdout, stderr } =
186
215
publishTool . name === "pnpm"
187
216
? await spawn ( "pnpm" , [ "publish" , "--json" , ...publishFlags ] , {
@@ -219,10 +248,10 @@ async function internalPublish(
219
248
}
220
249
// just in case this isn't already true
221
250
twoFactorState . isRequired = Promise . resolve ( true ) ;
222
- return internalPublish ( pkgName , opts , twoFactorState ) ;
251
+ return internalPublish ( packageJson , opts , twoFactorState ) ;
223
252
}
224
253
error (
225
- `an error occurred while publishing ${ pkgName } : ${ json . error . code } ` ,
254
+ `an error occurred while publishing ${ packageJson . name } : ${ json . error . code } ` ,
226
255
json . error . summary ,
227
256
json . error . detail ? "\n" + json . error . detail : ""
228
257
) ;
@@ -235,13 +264,13 @@ async function internalPublish(
235
264
}
236
265
237
266
export function publish (
238
- pkgName : string ,
267
+ packageJson : PackageJSON ,
239
268
opts : PublishOptions ,
240
269
twoFactorState : TwoFactorState
241
270
) : Promise < { published : boolean } > {
242
271
// If there are many packages to be published, it's better to limit the
243
272
// concurrency to avoid unwanted errors, for example from npm.
244
273
return npmRequestLimit ( ( ) =>
245
- npmPublishLimit ( ( ) => internalPublish ( pkgName , opts , twoFactorState ) )
274
+ npmPublishLimit ( ( ) => internalPublish ( packageJson , opts , twoFactorState ) )
246
275
) ;
247
276
}
0 commit comments