@@ -6,6 +6,7 @@ import * as util from "node:util";
6
6
const ADO_PUBLISH_PIPELINE = ".ado/templates/npm-publish-steps.yml" ;
7
7
const NX_CONFIG_FILE = "nx.json" ;
8
8
9
+ const NPM_DEFEAULT_REGISTRY = "https://registry.npmjs.org/"
9
10
const NPM_TAG_NEXT = "next" ;
10
11
const NPM_TAG_NIGHTLY = "nightly" ;
11
12
const RNMACOS_LATEST = "react-native-macos@latest" ;
@@ -21,8 +22,17 @@ const RNMACOS_NEXT = "react-native-macos@next";
21
22
* };
22
23
* };
23
24
* }} NxConfig;
24
- * @typedef {{ tag?: string; update?: boolean; verbose?: boolean; } } Options;
25
- * @typedef {{ npmTag: string; prerelease?: string; isNewTag?: boolean; } } TagInfo;
25
+ * @typedef {{
26
+ * "skip-auth"?: boolean;
27
+ * tag?: string;
28
+ * update?: boolean;
29
+ * verbose?: boolean;
30
+ * }} Options;
31
+ * @typedef {{
32
+ * npmTag: string;
33
+ * prerelease?: string;
34
+ * isNewTag?: boolean;
35
+ * }} TagInfo;
26
36
*/
27
37
28
38
/**
@@ -80,6 +90,38 @@ function loadNxConfig(configFile) {
80
90
return JSON . parse ( nx ) ;
81
91
}
82
92
93
+ function verifyNpmAuth ( registry = NPM_DEFEAULT_REGISTRY ) {
94
+ const npmErrorRegex = / n p m e r r o r c o d e ( \w + ) / ;
95
+ const spawnOptions = {
96
+ stdio : /** @type {const } */ ( "pipe" ) ,
97
+ shell : true ,
98
+ windowsVerbatimArguments : true ,
99
+ } ;
100
+
101
+ const whoamiArgs = [ "whoami" , "--registry" , registry ] ;
102
+ const whoami = spawnSync ( "npm" , whoamiArgs , spawnOptions ) ;
103
+ if ( whoami . status !== 0 ) {
104
+ const error = whoami . stderr . toString ( ) ;
105
+ const m = error . match ( npmErrorRegex ) ;
106
+ switch ( m && m [ 1 ] ) {
107
+ case "EINVALIDNPMTOKEN" :
108
+ throw new Error ( `Invalid auth token for npm registry: ${ registry } ` ) ;
109
+ case "ENEEDAUTH" :
110
+ throw new Error ( `Missing auth token for npm registry: ${ registry } ` ) ;
111
+ default :
112
+ throw new Error ( error ) ;
113
+ }
114
+ }
115
+
116
+ const tokenArgs = [ "token" , "list" , "--registry" , registry ] ;
117
+ const token = spawnSync ( "npm" , tokenArgs , spawnOptions ) ;
118
+ if ( token . status !== 0 ) {
119
+ const error = token . stderr . toString ( ) ;
120
+ const m = error . match ( npmErrorRegex ) ;
121
+ throw new Error ( m ? `Auth token for '${ registry } ' returned error code ${ m [ 1 ] } ` : error ) ;
122
+ }
123
+ }
124
+
83
125
/**
84
126
* Returns a numerical value for a given version string.
85
127
* @param {string } version
@@ -91,11 +133,27 @@ function versionToNumber(version) {
91
133
}
92
134
93
135
/**
94
- * Returns the currently checked out branch. Note that this function prefers
95
- * predefined CI environment variables over local clone.
136
+ * Returns the target branch name. If not targetting any branches (e.g., when
137
+ * executing this script locally), `undefined` is returned.
138
+ * @returns {string | undefined }
139
+ */
140
+ function getTargetBranch ( ) {
141
+ // https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
142
+ const adoTargetBranchName = process . env [ "SYSTEM_PULLREQUEST_TARGETBRANCH" ] ;
143
+ return adoTargetBranchName ?. replace ( / ^ r e f s \/ h e a d s \/ / , "" ) ;
144
+ }
145
+
146
+ /**
147
+ * Returns the current branch name. In a pull request, the target branch name is
148
+ * returned.
96
149
* @returns {string }
97
150
*/
98
151
function getCurrentBranch ( ) {
152
+ const adoTargetBranchName = getTargetBranch ( ) ;
153
+ if ( adoTargetBranchName ) {
154
+ return adoTargetBranchName ;
155
+ }
156
+
99
157
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
100
158
const adoSourceBranchName = process . env [ "BUILD_SOURCEBRANCHNAME" ] ;
101
159
if ( adoSourceBranchName ) {
@@ -199,9 +257,10 @@ function verifyPublishPipeline(file, tag) {
199
257
* @param {NxConfig } config
200
258
* @param {string } currentBranch
201
259
* @param {TagInfo } tag
260
+ * @param {Options } options
202
261
* @returns {asserts config is NxConfig["release"] }
203
262
*/
204
- function enablePublishing ( config , currentBranch , { npmTag : tag , prerelease, isNewTag } ) {
263
+ function enablePublishing ( config , currentBranch , { npmTag : tag , prerelease, isNewTag } , options ) {
205
264
/** @type {string[] } */
206
265
const errors = [ ] ;
207
266
@@ -253,8 +312,18 @@ function enablePublishing(config, currentBranch, { npmTag: tag, prerelease, isNe
253
312
throw new Error ( "Nx Release is not correctly configured for the current branch" ) ;
254
313
}
255
314
315
+ if ( options [ "skip-auth" ] ) {
316
+ info ( "Skipped npm auth validation" ) ;
317
+ } else {
318
+ verifyNpmAuth ( ) ;
319
+ }
320
+
256
321
verifyPublishPipeline ( ADO_PUBLISH_PIPELINE , tag ) ;
257
- enablePublishingOnAzurePipelines ( ) ;
322
+
323
+ // Don't enable publishing in PRs
324
+ if ( ! getTargetBranch ( ) ) {
325
+ enablePublishingOnAzurePipelines ( ) ;
326
+ }
258
327
}
259
328
260
329
/**
@@ -273,10 +342,11 @@ function main(options) {
273
342
const config = loadNxConfig ( NX_CONFIG_FILE ) ;
274
343
try {
275
344
if ( isMainBranch ( branch ) ) {
276
- enablePublishing ( config , branch , { npmTag : NPM_TAG_NIGHTLY , prerelease : NPM_TAG_NIGHTLY } ) ;
345
+ const info = { npmTag : NPM_TAG_NIGHTLY , prerelease : NPM_TAG_NIGHTLY } ;
346
+ enablePublishing ( config , branch , info , options ) ;
277
347
} else if ( isStableBranch ( branch ) ) {
278
348
const tag = getTagForStableBranch ( branch , options , logger ) ;
279
- enablePublishing ( config , branch , tag ) ;
349
+ enablePublishing ( config , branch , tag , options ) ;
280
350
}
281
351
} catch ( e ) {
282
352
if ( options . update ) {
@@ -296,6 +366,10 @@ function main(options) {
296
366
const { values } = util . parseArgs ( {
297
367
args : process . argv . slice ( 2 ) ,
298
368
options : {
369
+ "skip-auth" : {
370
+ type : "boolean" ,
371
+ default : false ,
372
+ } ,
299
373
tag : {
300
374
type : "string" ,
301
375
default : NPM_TAG_NEXT ,
0 commit comments