1
+ // Used to publish this fork of react-native
2
+ // Publish it as an attached tar asset to the GitHub release for general consumption, since we can't publish this to the npmjs npm feed
3
+
4
+ const fs = require ( "fs" ) ;
5
+ const path = require ( "path" ) ;
6
+ const execSync = require ( "child_process" ) . execSync ;
7
+
8
+ function exec ( command ) {
9
+ try {
10
+ console . log ( `Running command: ${ command } ` ) ;
11
+ return execSync ( command , {
12
+ stdio : "inherit"
13
+ } ) ;
14
+ } catch ( err ) {
15
+ process . exitCode = 1 ;
16
+ console . log ( `Failure running: ${ command } ` ) ;
17
+ throw err ;
18
+ }
19
+ }
20
+
21
+ function doPublish ( ) {
22
+ const publishBranchName = process . env . publishBranchName ;
23
+
24
+ const tempPublishBranch = `publish-${ Date . now ( ) } ` ;
25
+
26
+ const pkgJsonPath = path . resolve ( __dirname , "../package.json" ) ;
27
+ let pkgJson = JSON . parse ( fs . readFileSync ( pkgJsonPath , "utf8" ) ) ;
28
+
29
+ let releaseVersion = pkgJson . version ;
30
+
31
+ const versionGroups = / ( .* - m i c r o s o f t \. ) ( [ 0 - 9 ] * ) / . exec ( releaseVersion ) ;
32
+ if ( versionGroups ) {
33
+ releaseVersion = versionGroups [ 1 ] + ( parseInt ( versionGroups [ 2 ] ) + 1 ) ;
34
+ } else {
35
+ if ( releaseVersion . indexOf ( "-" ) === - 1 ) {
36
+ releaseVersion = releaseVersion + "-microsoft.0" ;
37
+ } else {
38
+ console . log ( "Invalid version to publish" ) ;
39
+ exit ( 1 ) ;
40
+ }
41
+ }
42
+
43
+ pkgJson . version = releaseVersion ;
44
+ fs . writeFileSync ( pkgJsonPath , JSON . stringify ( pkgJson , null , 2 ) ) ;
45
+ console . log ( `Updating package.json to version ${ releaseVersion } ` ) ;
46
+
47
+ exec ( `git checkout -b ${ tempPublishBranch } ` ) ;
48
+
49
+ exec ( `git add ${ pkgJsonPath } ` ) ;
50
+ exec ( `git commit -m "Applying package update to ${ releaseVersion } ` ) ;
51
+ exec ( `git tag v${ releaseVersion } ` ) ;
52
+ exec ( `git push origin HEAD:${ tempPublishBranch } --follow-tags --verbose` ) ;
53
+ exec ( `git push origin tag v${ releaseVersion } ` ) ;
54
+
55
+ // -------- Generating Android Artifacts with JavaDoc
56
+ exec ( "gradlew installArchives" ) ;
57
+
58
+ // undo uncommenting javadoc setting
59
+ exec ( "git checkout ReactAndroid/gradle.properties" ) ;
60
+
61
+ // Configure npm to publish to internal feed
62
+ const npmrcPath = path . resolve ( __dirname , "../.npmrc" ) ;
63
+ const npmrcContents = `registry=https:${
64
+ process . env . publishnpmfeed
65
+ } /registry/\nalways-auth=true`;
66
+ console . log ( `Creating ${ npmrcPath } for publishing:` ) ;
67
+ console . log ( npmrcContents ) ;
68
+ fs . writeFileSync ( npmrcPath , npmrcContents ) ;
69
+
70
+ exec ( `npm publish` ) ;
71
+ exec ( `del ${ npmrcPath } ` ) ;
72
+
73
+ // Push tar to GitHub releases
74
+ exec ( `npm pack` ) ;
75
+
76
+ const npmTarPath = path . resolve (
77
+ __dirname ,
78
+ `../react-native-${ releaseVersion } .tgz`
79
+ ) ;
80
+ const assetUpdateUrl = `https://uploads.github.com/repos/Microsoft/react-native/releases/{id}/assets?name=react-native-${ releaseVersion } .tgz` ;
81
+ const authHeader =
82
+ "Basic " + new Buffer ( ":" + process . env . githubToken ) . toString ( "base64" ) ;
83
+ const userAgent = "Microsoft-React-Native-Release-Agent" ;
84
+
85
+ let uploadReleaseAssetUrl = "" ;
86
+ exec ( "npm install request@^2.69.0 --no-save" ) ;
87
+
88
+ const request = require ( "request" ) ;
89
+
90
+ const uploadTarBallToRelease = function ( ) {
91
+ request . post (
92
+ {
93
+ url : uploadReleaseAssetUrl ,
94
+ headers : {
95
+ "User-Agent" : userAgent ,
96
+ Authorization : authHeader ,
97
+ "Content-Type" : "application/octet-stream"
98
+ } ,
99
+ formData : {
100
+ file : fs . createReadStream ( npmTarPath )
101
+ }
102
+ } ,
103
+ function ( err , res , body ) {
104
+ if ( err ) {
105
+ console . error ( err ) ;
106
+ process . exitCode = 1 ;
107
+ throw err ;
108
+ }
109
+
110
+ var formattedResponse = JSON . parse ( body ) ;
111
+
112
+ if ( formattedResponse . errors ) {
113
+ process . exitCode = 1 ;
114
+ console . error ( formattedResponse . errors ) ;
115
+ throw formattedResponse . errors ;
116
+ }
117
+
118
+ exec ( `del ${ npmTarPath } ` ) ;
119
+ exec ( `git checkout ${ publishBranchName } ` ) ;
120
+ exec ( `git pull origin ${ publishBranchName } ` ) ;
121
+ exec ( `git merge ${ tempPublishBranch } --no-edit` ) ;
122
+ exec (
123
+ `git push origin HEAD:${ publishBranchName } --follow-tags --verbose`
124
+ ) ;
125
+ exec ( `git branch -d ${ tempPublishBranch } ` ) ;
126
+ exec ( `git push origin --delete -d ${ tempPublishBranch } ` ) ;
127
+ }
128
+ ) ;
129
+ } ;
130
+
131
+ const createReleaseRequestBody = {
132
+ tag_name : `v${ releaseVersion } ` ,
133
+ target_commitish : tempPublishBranch ,
134
+ name : `v${ releaseVersion } ` ,
135
+ body : `v${ releaseVersion } ` ,
136
+ draft : false ,
137
+ prerelease : true
138
+ } ;
139
+ console . log ( 'createReleaseRequestBody: ' + JSON . stringify ( createReleaseRequestBody , null , 2 ) ) ;
140
+
141
+ request . post (
142
+ {
143
+ url : "https://github.com/api/repos/Microsoft/react-native/releases" ,
144
+ headers : {
145
+ "User-Agent" : userAgent ,
146
+ Authorization : authHeader
147
+ } ,
148
+ json : true ,
149
+ body : createReleaseRequestBody
150
+ } ,
151
+ function ( err , res , body ) {
152
+ if ( err ) {
153
+ console . log ( err ) ;
154
+ throw new Error ( "Error fetching release id." ) ;
155
+ }
156
+
157
+ console . log ( "Created GitHub Release: " + JSON . stringify ( body , null , 2 ) ) ;
158
+ uploadReleaseAssetUrl = assetUpdateUrl . replace ( / { i d } / , body . id ) ;
159
+ uploadTarBallToRelease ( ) ;
160
+ }
161
+ ) ;
162
+ }
163
+
164
+ doPublish ( ) ;
0 commit comments