10
10
'use strict' ;
11
11
12
12
/**
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.
14
14
* It is supposed to run in CI environment, not on a developer's machine.
15
15
*
16
16
* To make it easier for developers it uses some logic to identify with which
49
49
* 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)
50
50
*/
51
51
52
- /*eslint-disable no-undef */
53
- require ( 'shelljs/global' ) ;
52
+ const { exec, echo, exit, test} = require ( 'shelljs' ) ;
54
53
const yargs = require ( 'yargs' ) ;
54
+ const { parseVersion} = require ( './version-utils' ) ;
55
55
56
- let argv = yargs
56
+ const buildTag = process . env . CIRCLE_TAG ;
57
+ const otp = process . env . NPM_CONFIG_OTP ;
58
+
59
+ const argv = yargs
57
60
. option ( 'n' , {
58
61
alias : 'nightly' ,
59
62
type : 'boolean' ,
@@ -64,75 +67,52 @@ let argv = yargs
64
67
type : 'boolean' ,
65
68
default : false ,
66
69
} ) . argv ;
67
-
68
70
const nightlyBuild = argv . nightly ;
69
71
const 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
91
72
92
73
// 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' , {
98
75
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 ) ;
115
95
exit ( 1 ) ;
116
96
}
117
97
118
98
let 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
+ }
119
112
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 ) {
136
116
if (
137
117
exec (
138
118
`node scripts/bump-oss-version.js --nightly --to-version ${ releaseVersion } ` ,
@@ -141,14 +121,6 @@ if (buildFromMain) {
141
121
echo ( 'Failed to bump version number' ) ;
142
122
exit ( 1 ) ;
143
123
}
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 ] ;
152
124
}
153
125
154
126
// -------- Generating Android Artifacts with JavaDoc
@@ -183,12 +155,12 @@ if (dryRunBuild) {
183
155
exit ( 0 ) ;
184
156
}
185
157
186
- // if version contains -rc, tag as prerelease
158
+ // Set the right tag for nightly and prerelease builds
187
159
const tagFlag = nightlyBuild
188
160
? '--tag nightly'
189
- : releaseVersion . indexOf ( '-rc' ) === - 1
190
- ? ''
191
- : '--tag next ' ;
161
+ : prerelease != null
162
+ ? '--tag next '
163
+ : '' ;
192
164
193
165
// use otp from envvars if available
194
166
const otpFlag = otp ? `--otp ${ otp } ` : '' ;
@@ -200,4 +172,3 @@ if (exec(`npm publish ${tagFlag} ${otpFlag}`).code) {
200
172
echo ( `Published to npm ${ releaseVersion } ` ) ;
201
173
exit ( 0 ) ;
202
174
}
203
- /*eslint-enable no-undef */
0 commit comments