@@ -33,6 +33,7 @@ import * as os from "os";
3333import * as path from "path" ;
3434import { Configuration } from "../configuration" ;
3535import { BuildOnPushSubscription } from "../typings/types" ;
36+ import * as pRetry from "p-retry" ;
3637
3738const Matchers = [
3839 {
@@ -261,68 +262,12 @@ const NpmInstallStep: NpmStep = {
261262 } ,
262263} ;
263264
264- const NodeVersionStep : NpmStep = {
265- name : "version" ,
266- runWhen : async ( ctx , params ) => {
267- const pj = await fs . readJson ( params . project . path ( "package.json" ) ) ;
268- return ! pj . scripts ?. version ;
269- } ,
270- run : async ( ctx , params ) => {
271- const push = ctx . data . Push [ 0 ] ;
272- const pj = await fs . readJson ( params . project . path ( "package.json" ) ) ;
273-
274- let pjVersion = pj . version ;
275- if ( ! pjVersion || pjVersion . length === 0 ) {
276- pjVersion = "0.0.0" ;
277- }
278-
279- const version = await github . nextTag (
280- params . project . id ,
281- "prerelease" ,
282- pjVersion ,
283- ) ;
284- const result = await params . project . spawn (
285- "npm" ,
286- [ "version" , "--no-git-tag-version" , version ] ,
287- {
288- env : {
289- ...process . env ,
290- PATH : `${ params . path } :${ process . env . PATH } ` ,
291- } ,
292- } ,
293- ) ;
294- if ( result . status !== 0 ) {
295- await params . check . update ( {
296- conclusion : "failure" ,
297- body : "`npm version` failed" ,
298- } ) ;
299- return status . failure (
300- `\`npm version\` failed on [${ push . repo . owner } /${
301- push . repo . name
302- } /${ push . after . sha . slice ( 0 , 7 ) } ](${ push . after . url } )`,
303- ) ;
304- }
305- return status . success ( ) ;
306- } ,
307- } ;
308-
309- function gitBranchToNpmVersion ( branchName : string ) : string {
310- // prettier-ignore
311- return branchName . replace ( / \/ / g, "-" ) . replace ( / _ / g, "-" ) . replace ( / @ / g, "" ) ;
312- }
313-
314265const NpmScriptsStep : NpmStep = {
315266 name : "npm run" ,
316267 run : async ( ctx , params ) => {
317268 const push = ctx . data . Push [ 0 ] ;
318269 const cfg = ctx . configuration ?. [ 0 ] ?. parameters ;
319- let scripts = cfg . scripts ;
320-
321- // Test if the project overwrites the version step
322- const pj = await fs . readJson ( params . project . path ( "package.json" ) ) ;
323- if ( pj . scripts . version ) {
324- scripts = [ "version" , ...scripts ] ;
325- }
270+ const scripts = cfg . scripts ;
326271
327272 // Run scripts
328273 for ( const script of scripts ) {
@@ -442,6 +387,84 @@ function mapSeverity(severity: string): "notice" | "warning" | "failure" {
442387 }
443388}
444389
390+ const NpmVersionStep : NpmStep = {
391+ name : "version" ,
392+ runWhen : async ctx => ctx . configuration ?. [ 0 ] ?. parameters . publish ,
393+ run : async ( ctx , params ) => {
394+ const push = ctx . data . Push [ 0 ] ;
395+ let pj = await fs . readJson ( params . project . path ( "package.json" ) ) ;
396+
397+ let pjVersion = pj . version ;
398+ if ( ! pjVersion || pjVersion . length === 0 ) {
399+ pjVersion = "0.0.0" ;
400+ }
401+
402+ let version ;
403+ if ( ! pj . scripts ?. version ) {
404+ version = await pRetry (
405+ async ( ) => {
406+ const tag = await github . nextTag (
407+ params . project . id ,
408+ "prerelease" ,
409+ pjVersion ,
410+ ) ;
411+ await params . project . exec ( "git" , [
412+ "tag" ,
413+ "-m" ,
414+ `Version ${ tag } ` ,
415+ tag ,
416+ ] ) ;
417+ await params . project . exec ( "git" , [ "push" , "origin" , tag ] ) ;
418+ return tag ;
419+ } ,
420+ {
421+ retries : 5 ,
422+ maxTimeout : 2500 ,
423+ minTimeout : 1000 ,
424+ randomize : true ,
425+ } ,
426+ ) ;
427+ } else {
428+ await params . project . spawn ( "npm" , [ "run" , "version" ] , {
429+ env : {
430+ ...process . env ,
431+ PATH : `${ params . path } :${ process . env . PATH } ` ,
432+ } ,
433+ } ) ;
434+ pj = await fs . readJson ( params . project . path ( "package.json" ) ) ;
435+ version = pj . version ;
436+ }
437+
438+ const result = await params . project . spawn (
439+ "npm" ,
440+ [ "version" , "--no-git-tag-version" , version ] ,
441+ {
442+ env : {
443+ ...process . env ,
444+ PATH : `${ params . path } :${ process . env . PATH } ` ,
445+ } ,
446+ } ,
447+ ) ;
448+ if ( result . status !== 0 ) {
449+ await params . check . update ( {
450+ conclusion : "failure" ,
451+ body : "`npm version` failed" ,
452+ } ) ;
453+ return status . failure (
454+ `\`npm version\` failed on [${ push . repo . owner } /${
455+ push . repo . name
456+ } /${ push . after . sha . slice ( 0 , 7 ) } ](${ push . after . url } )`,
457+ ) ;
458+ }
459+ return status . success ( ) ;
460+ } ,
461+ } ;
462+
463+ function gitBranchToNpmVersion ( branchName : string ) : string {
464+ // prettier-ignore
465+ return branchName . replace ( / \/ / g, "-" ) . replace ( / _ / g, "-" ) . replace ( / @ / g, "" ) ;
466+ }
467+
445468const NpmPublishStep : NpmStep = {
446469 name : "npm publish" ,
447470 runWhen : async ctx => ctx . configuration ?. [ 0 ] ?. parameters . publish ,
@@ -592,24 +615,6 @@ function gitBranchToNpmTag(branchName: string): string {
592615 return `branch-${ gitBranchToNpmVersion ( branchName ) } ` ;
593616}
594617
595- const GitTagStep : NpmStep = {
596- name : "git tag" ,
597- runWhen : async ctx => {
598- return ctx . configuration ?. [ 0 ] ?. parameters ?. gitTag ;
599- } ,
600- run : async ( ctx , params ) => {
601- const pj = await fs . readJson ( params . project . path ( "package.json" ) ) ;
602- await params . project . spawn ( "git" , [
603- "tag" ,
604- "-m" ,
605- `Version ${ pj . version } ` ,
606- pj . version ,
607- ] ) ;
608- await params . project . spawn ( "git" , [ "push" , "origin" , pj . version ] ) ;
609- return status . success ( ) ;
610- } ,
611- } ;
612-
613618export const handler : EventHandler <
614619 BuildOnPushSubscription ,
615620 Configuration
@@ -624,9 +629,8 @@ export const handler: EventHandler<
624629 SetupNodeStep ,
625630 NpmInstallStep ,
626631 NpmScriptsStep ,
627- NodeVersionStep ,
632+ NpmVersionStep ,
628633 NpmPublishStep ,
629- GitTagStep ,
630634 ] ,
631635 parameters : { body : [ ] } ,
632636 } ) ;
0 commit comments