@@ -79,6 +79,7 @@ async function packageExists(packageName: string) {
7979 'view' ,
8080 packageName ,
8181 'version' ,
82+ '--prefer-online' ,
8283 '--registry' ,
8384 options . registry ,
8485 ] )
@@ -95,6 +96,7 @@ async function packageVersionExists(packageName: string, version: string) {
9596 'view' ,
9697 `${ packageName } @${ version } ` ,
9798 'version' ,
99+ '--prefer-online' ,
98100 '--registry' ,
99101 options . registry ,
100102 ] )
@@ -273,7 +275,22 @@ async function publishPackage(pkg: PackageInfo) {
273275
274276 if ( options . provenance ) args . push ( '--provenance' )
275277
276- await npm ( args )
278+ try {
279+ await npm ( args )
280+ return true
281+ } catch ( error ) {
282+ if ( isAlreadyPublished ( error ) ) return false
283+ throw error
284+ }
285+ }
286+
287+ // npm view can lag behind a fresh publish, so a rerun may still try to
288+ // publish a version that is already live. That is a skip, not a failure.
289+ function isAlreadyPublished ( error : unknown ) {
290+ if ( ! isExecError ( error ) ) return false
291+ return error . stderr . includes (
292+ 'cannot publish over the previously published versions' ,
293+ )
277294}
278295
279296async function main ( ) {
@@ -317,12 +334,18 @@ async function main() {
317334 return
318335 }
319336
337+ let publishedCount = 0
320338 for ( const pkg of missingPackages ) {
321- console . log ( `publishing ${ pkg . packageJson . name } @${ pkg . packageJson . version } ` )
322- await publishPackage ( pkg )
339+ const id = `${ pkg . packageJson . name } @${ pkg . packageJson . version } `
340+ console . log ( `publishing ${ id } ` )
341+ if ( await publishPackage ( pkg ) ) {
342+ publishedCount += 1
343+ continue
344+ }
345+ console . log ( `skip already published ${ id } ` )
323346 }
324347
325- console . log ( `Published ${ missingPackages . length } new package(s).` )
348+ console . log ( `Published ${ publishedCount } new package(s).` )
326349}
327350
328351main ( ) . catch ( ( error ) => {
0 commit comments