1010'use strict' ;
1111
1212/**
13- * This script publishes a new version of react-native to NPM.
13+ * This script prepares a release version of react-native and may publish to NPM.
1414 * It is supposed to run in CI environment, not on a developer's machine.
1515 *
1616 * To make it easier for developers it uses some logic to identify with which
4949 * If tag v0.XY.Z is present on the commit then publish to npm with version 0.XY.Z and no tag (npm will consider it latest)
5050 */
5151
52- /*eslint-disable no-undef */
53- require ( 'shelljs/global' ) ;
52+ const { exec, echo, exit, test} = require ( 'shelljs' ) ;
5453const yargs = require ( 'yargs' ) ;
54+ const { parseVersion} = require ( './version-utils' ) ;
5555
56- let argv = yargs
56+ const buildTag = process . env . CIRCLE_TAG ;
57+ const otp = process . env . NPM_CONFIG_OTP ;
58+
59+ const argv = yargs
5760 . option ( 'n' , {
5861 alias : 'nightly' ,
5962 type : 'boolean' ,
@@ -64,75 +67,52 @@ let argv = yargs
6467 type : 'boolean' ,
6568 default : false ,
6669 } ) . argv ;
67-
6870const nightlyBuild = argv . nightly ;
6971const dryRunBuild = argv . dryRun ;
70- const buildFromMain = nightlyBuild || dryRunBuild ;
71- const buildTag = process . env . CIRCLE_TAG ;
72- const otp = process . env . NPM_CONFIG_OTP ;
73-
74- let branchVersion = 0 ;
75- if ( buildFromMain ) {
76- branchVersion = 0 ;
77- } else {
78- if ( ! buildTag ) {
79- echo ( 'Error: We publish only from git tags' ) ;
80- exit ( 1 ) ;
81- }
82-
83- let match = buildTag . match ( / ^ v ( \d + \. \d + ) \. \d + (?: - .+ ) ? $ / ) ;
84- if ( ! match ) {
85- echo ( 'Error: We publish only from release version git tags' ) ;
86- exit ( 1 ) ;
87- }
88- [ , branchVersion ] = match ;
89- }
90- // 0.33
9172
9273// 34c034298dc9cad5a4553964a5a324450fda0385
93- const currentCommit = exec ( 'git rev-parse HEAD' , { silent : true } ) . stdout . trim ( ) ;
94-
95- // Note: We rely on tagsWithVersion to be alphabetically sorted
96- // [34c034298dc9cad5a4553964a5a324450fda0385, refs/heads/0.33-stable, refs/tags/latest, refs/tags/v0.33.1, refs/tags/v0.34.1-rc]
97- const tagsWithVersion = exec ( `git ls-remote origin | grep ${ currentCommit } ` , {
74+ const currentCommit = exec ( 'git rev-parse HEAD' , {
9875 silent : true ,
99- } )
100- . stdout . split ( / \s / )
101- // ['refs/tags/v0.33.0', 'refs/tags/v0.33.0-rc', 'refs/tags/v0.33.0-rc1', 'refs/tags/v0.33.0-rc2', 'refs/tags/v0.34.0']
102- . filter (
103- version =>
104- ! ! version && version . indexOf ( `refs/tags/v${ branchVersion } ` ) === 0 ,
105- )
106- // ['refs/tags/v0.33.0', 'refs/tags/v0.33.0-rc', 'refs/tags/v0.33.0-rc1', 'refs/tags/v0.33.0-rc2']
107- . filter ( version => version . indexOf ( branchVersion ) !== - 1 )
108- // ['0.33.0', '0.33.0-rc', '0.33.0-rc1', '0.33.0-rc2']
109- . map ( version => version . slice ( 'refs/tags/v' . length ) ) ;
110-
111- if ( ! buildFromMain && tagsWithVersion . length === 0 ) {
112- echo (
113- 'Error: Cannot find version tag in current commit. To deploy to NPM you must add tag v0.XY.Z[-rc] to your commit' ,
114- ) ;
76+ } ) . stdout . trim ( ) ;
77+ const shortCommit = currentCommit . slice ( 0 , 9 ) ;
78+
79+ const rawVersion =
80+ // 0.0.0 triggers issues with cocoapods for codegen when building template project.
81+ dryRunBuild
82+ ? '1000.0.0'
83+ : // For nightly we continue to use 0.0.0 for clarity for npm
84+ nightlyBuild
85+ ? '0.0.0'
86+ : // For pre-release and stable releases, we use the git tag of the version we're releasing (set in bump-oss-version)
87+ buildTag ;
88+
89+ let version ,
90+ prerelease = null ;
91+ try {
92+ ( { version, prerelease} = parseVersion ( rawVersion ) ) ;
93+ } catch ( e ) {
94+ echo ( e . message ) ;
11595 exit ( 1 ) ;
11696}
11797
11898let releaseVersion ;
99+ if ( dryRunBuild ) {
100+ releaseVersion = `${ version } -${ shortCommit } ` ;
101+ } else if ( nightlyBuild ) {
102+ // 2021-09-28T05:38:40.669Z -> 20210928-0538
103+ const dateIdentifier = new Date ( )
104+ . toISOString ( )
105+ . slice ( 0 , - 8 )
106+ . replace ( / [ - : ] / g, '' )
107+ . replace ( / [ T ] / g, '-' ) ;
108+ releaseVersion = `${ version } -${ dateIdentifier } -${ shortCommit } ` ;
109+ } else {
110+ releaseVersion = version ;
111+ }
119112
120- if ( buildFromMain ) {
121- if ( nightlyBuild ) {
122- releaseVersion = '0.0.0-' ;
123- // 2021-09-28T05:38:40.669Z -> 20210928-0538
124- releaseVersion += new Date ( )
125- . toISOString ( )
126- . slice ( 0 , - 8 )
127- . replace ( / [ - : ] / g, '' )
128- . replace ( / [ T ] / g, '-' ) ;
129- } else {
130- // 0.0.0 triggers issues with cocoapods for codegen for building template project.
131- releaseVersion = '1000.0.0' ;
132- }
133-
134- releaseVersion += `-${ currentCommit . slice ( 0 , 9 ) } ` ;
135- // Bump version number in various files (package.json, gradle.properties etc)
113+ // Bump version number in various files (package.json, gradle.properties etc)
114+ // For stable, pre-release releases, we manually call bump-oss-version on release branch
115+ if ( nightlyBuild || dryRunBuild ) {
136116 if (
137117 exec (
138118 `node scripts/bump-oss-version.js --nightly --to-version ${ releaseVersion } ` ,
@@ -141,14 +121,6 @@ if (buildFromMain) {
141121 echo ( 'Failed to bump version number' ) ;
142122 exit ( 1 ) ;
143123 }
144- } else if ( tagsWithVersion [ 0 ] . indexOf ( '-rc' ) === - 1 ) {
145- // if first tag on this commit is non -rc then we are making a stable release
146- // '0.33.0'
147- releaseVersion = tagsWithVersion [ 0 ] ;
148- } else {
149- // otherwise pick last -rc tag, indicates latest rc version due to alpha-sort
150- // '0.33.0-rc2'
151- releaseVersion = tagsWithVersion [ tagsWithVersion . length - 1 ] ;
152124}
153125
154126// -------- Generating Android Artifacts with JavaDoc
@@ -183,12 +155,12 @@ if (dryRunBuild) {
183155 exit ( 0 ) ;
184156}
185157
186- // if version contains -rc, tag as prerelease
158+ // Set the right tag for nightly and prerelease builds
187159const tagFlag = nightlyBuild
188160 ? '--tag nightly'
189- : releaseVersion . indexOf ( '-rc' ) === - 1
190- ? ''
191- : '--tag next ' ;
161+ : prerelease != null
162+ ? '--tag next '
163+ : '' ;
192164
193165// use otp from envvars if available
194166const otpFlag = otp ? `--otp ${ otp } ` : '' ;
@@ -200,4 +172,3 @@ if (exec(`npm publish ${tagFlag} ${otpFlag}`).code) {
200172 echo ( `Published to npm ${ releaseVersion } ` ) ;
201173 exit ( 0 ) ;
202174}
203- /*eslint-enable no-undef */
0 commit comments