@@ -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" ;
@@ -80,6 +81,38 @@ function loadNxConfig(configFile) {
80
81
return JSON . parse ( nx ) ;
81
82
}
82
83
84
+ function verifyNpmAuth ( registry = NPM_DEFEAULT_REGISTRY ) {
85
+ const npmErrorRegex = / n p m e r r o r c o d e ( \w + ) / ;
86
+ const spawnOptions = {
87
+ stdio : /** @type {const } */ ( "pipe" ) ,
88
+ shell : true ,
89
+ windowsVerbatimArguments : true ,
90
+ } ;
91
+
92
+ const whoamiArgs = [ "whoami" , "--registry" , registry ] ;
93
+ const whoami = spawnSync ( "npm" , whoamiArgs , spawnOptions ) ;
94
+ if ( whoami . status !== 0 ) {
95
+ const error = whoami . stderr . toString ( ) ;
96
+ const m = error . match ( npmErrorRegex ) ;
97
+ switch ( m && m [ 1 ] ) {
98
+ case "EINVALIDNPMTOKEN" :
99
+ throw new Error ( `Invalid auth token for npm registry: ${ registry } ` ) ;
100
+ case "ENEEDAUTH" :
101
+ throw new Error ( `Missing auth token for npm registry: ${ registry } ` ) ;
102
+ default :
103
+ throw new Error ( error ) ;
104
+ }
105
+ }
106
+
107
+ const tokenArgs = [ "token" , "list" , "--registry" , registry ] ;
108
+ const token = spawnSync ( "npm" , tokenArgs , spawnOptions ) ;
109
+ if ( token . status !== 0 ) {
110
+ const error = token . stderr . toString ( ) ;
111
+ const m = error . match ( npmErrorRegex ) ;
112
+ throw new Error ( m ? `Auth token for '${ registry } ' returned error code ${ m [ 1 ] } ` : error ) ;
113
+ }
114
+ }
115
+
83
116
/**
84
117
* Returns a numerical value for a given version string.
85
118
* @param {string } version
@@ -252,6 +285,8 @@ function enablePublishing(config, currentBranch, { npmTag: tag, prerelease, isNe
252
285
throw new Error ( "Nx Release is not correctly configured for the current branch" ) ;
253
286
}
254
287
288
+ verifyNpmAuth ( ) ;
289
+
255
290
verifyPublishPipeline ( ADO_PUBLISH_PIPELINE , tag ) ;
256
291
enablePublishingOnAzurePipelines ( ) ;
257
292
}
0 commit comments