@@ -3,9 +3,9 @@ import { spawnSync } from "node:child_process";
3
3
import * as fs from "node:fs" ;
4
4
import * as util from "node:util" ;
5
5
6
- const ADO_PUBLISH_PIPELINE = ".ado/templates/npm-publish-steps.yml" ;
7
6
const NX_CONFIG_FILE = "nx.json" ;
8
7
8
+ const NPM_DEFEAULT_REGISTRY = "https://registry.npmjs.org/"
9
9
const NPM_TAG_NEXT = "next" ;
10
10
const NPM_TAG_NIGHTLY = "nightly" ;
11
11
const RNMACOS_LATEST = "react-native-macos@latest" ;
@@ -21,8 +21,18 @@ const RNMACOS_NEXT = "react-native-macos@next";
21
21
* };
22
22
* };
23
23
* }} NxConfig;
24
- * @typedef {{ tag?: string; update?: boolean; verbose?: boolean; } } Options;
25
- * @typedef {{ npmTag: string; prerelease?: string; isNewTag?: boolean; } } TagInfo;
24
+ * @typedef {{
25
+ * "mock-branch"?: string;
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,17 +133,39 @@ 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.
149
+ * @param {Options } options
96
150
* @returns {string }
97
151
*/
98
- function getCurrentBranch ( ) {
152
+ function getCurrentBranch ( options ) {
153
+ const adoTargetBranchName = getTargetBranch ( ) ;
154
+ if ( adoTargetBranchName ) {
155
+ return adoTargetBranchName ;
156
+ }
157
+
99
158
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
100
159
const adoSourceBranchName = process . env [ "BUILD_SOURCEBRANCHNAME" ] ;
101
160
if ( adoSourceBranchName ) {
102
161
return adoSourceBranchName . replace ( / ^ r e f s \/ h e a d s \/ / , "" ) ;
103
162
}
104
163
164
+ const { "mock-branch" : mockBranch } = options ;
165
+ if ( mockBranch ) {
166
+ return mockBranch ;
167
+ }
168
+
105
169
// Depending on how the repo was cloned, HEAD may not exist. We only use this
106
170
// method as fallback.
107
171
const { stdout } = spawnSync ( "git" , [ "rev-parse" , "--abbrev-ref" , "HEAD" ] ) ;
@@ -177,31 +241,15 @@ function getTagForStableBranch(branch, { tag }, log) {
177
241
return { npmTag : NPM_TAG_NEXT , prerelease : "rc" } ;
178
242
}
179
243
180
- /**
181
- * @param {string } file
182
- * @param {string } tag
183
- * @returns {void }
184
- */
185
- function verifyPublishPipeline ( file , tag ) {
186
- const data = fs . readFileSync ( file , { encoding : "utf-8" } ) ;
187
- const m = data . match ( / p u b l i s h T a g : ' ( l a t e s t | n e x t | n i g h t l y | v \d + \. \d + - s t a b l e ) ' / ) ;
188
- if ( ! m ) {
189
- throw new Error ( `${ file } : Could not find npm publish tag` ) ;
190
- }
191
-
192
- if ( m [ 1 ] !== tag ) {
193
- throw new Error ( `${ file } : 'publishTag' must be set to '${ tag } '` ) ;
194
- }
195
- }
196
-
197
244
/**
198
245
* Verifies the configuration and enables publishing on CI.
199
246
* @param {NxConfig } config
200
247
* @param {string } currentBranch
201
248
* @param {TagInfo } tag
249
+ * @param {Options } options
202
250
* @returns {asserts config is NxConfig["release"] }
203
251
*/
204
- function enablePublishing ( config , currentBranch , { npmTag : tag , prerelease, isNewTag } ) {
252
+ function enablePublishing ( config , currentBranch , { npmTag : tag , prerelease, isNewTag } , options ) {
205
253
/** @type {string[] } */
206
254
const errors = [ ] ;
207
255
@@ -244,7 +292,7 @@ function enablePublishing(config, currentBranch, { npmTag: tag, prerelease, isNe
244
292
generatorOptions . fallbackCurrentVersionResolver = "disk" ;
245
293
}
246
294
} else if ( typeof generatorOptions . fallbackCurrentVersionResolver === "string" ) {
247
- errors . push ( "'release.version.generatorOptions.fallbackCurrentVersionResolver' must be unset " ) ;
295
+ errors . push ( "'release.version.generatorOptions.fallbackCurrentVersionResolver' must be removed " ) ;
248
296
generatorOptions . fallbackCurrentVersionResolver = undefined ;
249
297
}
250
298
@@ -253,16 +301,24 @@ function enablePublishing(config, currentBranch, { npmTag: tag, prerelease, isNe
253
301
throw new Error ( "Nx Release is not correctly configured for the current branch" ) ;
254
302
}
255
303
256
- verifyPublishPipeline ( ADO_PUBLISH_PIPELINE , tag ) ;
257
- enablePublishingOnAzurePipelines ( ) ;
304
+ if ( options [ "skip-auth" ] ) {
305
+ info ( "Skipped npm auth validation" ) ;
306
+ } else {
307
+ verifyNpmAuth ( ) ;
308
+ }
309
+
310
+ // Don't enable publishing in PRs
311
+ if ( ! getTargetBranch ( ) ) {
312
+ enablePublishingOnAzurePipelines ( ) ;
313
+ }
258
314
}
259
315
260
316
/**
261
317
* @param {Options } options
262
318
* @returns {number }
263
319
*/
264
320
function main ( options ) {
265
- const branch = getCurrentBranch ( ) ;
321
+ const branch = getCurrentBranch ( options ) ;
266
322
if ( ! branch ) {
267
323
error ( "Could not get current branch" ) ;
268
324
return 1 ;
@@ -273,10 +329,11 @@ function main(options) {
273
329
const config = loadNxConfig ( NX_CONFIG_FILE ) ;
274
330
try {
275
331
if ( isMainBranch ( branch ) ) {
276
- enablePublishing ( config , branch , { npmTag : NPM_TAG_NIGHTLY , prerelease : NPM_TAG_NIGHTLY } ) ;
332
+ const info = { npmTag : NPM_TAG_NIGHTLY , prerelease : NPM_TAG_NIGHTLY } ;
333
+ enablePublishing ( config , branch , info , options ) ;
277
334
} else if ( isStableBranch ( branch ) ) {
278
335
const tag = getTagForStableBranch ( branch , options , logger ) ;
279
- enablePublishing ( config , branch , tag ) ;
336
+ enablePublishing ( config , branch , tag , options ) ;
280
337
}
281
338
} catch ( e ) {
282
339
if ( options . update ) {
@@ -296,6 +353,13 @@ function main(options) {
296
353
const { values } = util . parseArgs ( {
297
354
args : process . argv . slice ( 2 ) ,
298
355
options : {
356
+ "mock-branch" : {
357
+ type : "string" ,
358
+ } ,
359
+ "skip-auth" : {
360
+ type : "boolean" ,
361
+ default : false ,
362
+ } ,
299
363
tag : {
300
364
type : "string" ,
301
365
default : NPM_TAG_NEXT ,
0 commit comments