Skip to content

Commit 07bec8b

Browse files
author
REDMOND\acoates
committed
fix
1 parent 6e6c426 commit 07bec8b

File tree

1 file changed

+115
-31
lines changed

1 file changed

+115
-31
lines changed

.ado/publish.js

Lines changed: 115 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
1-
require('shelljs/global');
2-
const fs = require('fs');
3-
const path = require('path');
4-
const execSync = require('child_process').execSync;
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;
57

68
function exec(command) {
79
try {
810
console.log(`Running command: ${command}`);
911
return execSync(command, {
10-
stdio: 'inherit'
11-
});
12-
}
13-
catch(err) {
12+
stdio: "inherit"
13+
});
14+
} catch (err) {
1415
process.exitCode = 1;
1516
console.log(`Failure running: ${command}`);
1617
throw err;
1718
}
1819
}
1920

2021
function doPublish() {
21-
2222
const publishBranchName = process.env.publishBranchName;
2323

2424
const tempPublishBranch = `publish-${Date.now()}`;
2525

26-
const pkgJsonPath = path.resolve(__dirname, '../package.json');
27-
let pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
26+
const pkgJsonPath = path.resolve(__dirname, "../package.json");
27+
let pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
2828

2929
let releaseVersion = pkgJson.version;
3030

3131
const versionGroups = /(.*-microsoft\.)([0-9]*)/.exec(releaseVersion);
3232
if (versionGroups) {
33-
releaseVersion = versionGroups[1] + (parseInt(versionGroups[2])+1);
33+
releaseVersion = versionGroups[1] + (parseInt(versionGroups[2]) + 1);
3434
} else {
35-
if (releaseVersion.indexOf('-') === -1)
36-
{
37-
releaseVersion = releaseVersion + '-microsoft.0';
38-
}
39-
else
40-
{
41-
console.log('Invalid version to publish');
35+
if (releaseVersion.indexOf("-") === -1) {
36+
releaseVersion = releaseVersion + "-microsoft.0";
37+
} else {
38+
console.log("Invalid version to publish");
4239
exit(1);
4340
}
4441
}
@@ -54,27 +51,114 @@ function doPublish() {
5451
exec(`git push origin HEAD:${tempPublishBranch} --follow-tags --verbose`);
5552

5653
// -------- Generating Android Artifacts with JavaDoc
57-
exec('gradlew installArchives');
54+
exec("gradlew installArchives");
5855

5956
// undo uncommenting javadoc setting
60-
exec('git checkout ReactAndroid/gradle.properties');
57+
exec("git checkout ReactAndroid/gradle.properties");
6158

6259
// Configure npm to publish to internal feed
63-
const npmrcPath = path.resolve(__dirname, '../.npmrc');
64-
const npmrcContents = `registry=https:${process.env.publishnpmfeed}/registry/\nalways-auth=true`;
60+
const npmrcPath = path.resolve(__dirname, "../.npmrc");
61+
const npmrcContents = `registry=https:${
62+
process.env.publishnpmfeed
63+
}/registry/\nalways-auth=true`;
6564
console.log(`Creating ${npmrcPath} for publishing`);
6665
fs.writeFileSync(npmrcPath, npmrcContents);
6766

6867
exec(`npm publish`);
6968
exec(`del ${npmrcPath}`);
70-
exec(`git tag v${releaseVersion}`);
71-
exec(`git push origin HEAD:${tempPublishBranch} --follow-tags --verbose`);
72-
exec(`git checkout ${publishBranchName}`);
73-
exec('git pull origin ${publishBranchName}');
74-
exec('git merge ${publishBranchName} --no-edit');
75-
exec('git push origin HEAD:${publishBranchName} --follow-tags --verbose');
76-
exec('git branch -d ${tempPublishBranch}');
77-
exec('git push origin --delete -d ${tempPublishBranch}');
69+
70+
// Push tar to GitHub releases
71+
exec(`npm pack`);
72+
73+
const npmTarPath = path.resolve(
74+
__dirname,
75+
`../react-native-${releaseVersion}.tgz`
76+
);
77+
const assetUpdateUrl = `https://uploads.github.com/repos/Microsoft/react-native/releases/{id}/assets?name=react-native-${releaseVersion}.tgz`;
78+
const authHeader =
79+
"Basic " + new Buffer(":" + process.env.githubToken).toString("base64");
80+
const userAgent = "Microsoft-React-Native-Release-Agent";
81+
82+
let uploadReleaseAssetUrl = "";
83+
exec("npm install request@^2.69.0 --no-save");
84+
85+
const request = require("request");
86+
87+
const uploadTarBallToRelease = function() {
88+
request.post(
89+
{
90+
url: uploadReleaseAssetUrl,
91+
headers: {
92+
"User-Agent": userAgent,
93+
Authorization: authHeader,
94+
"Content-Type": "application/octet-stream"
95+
},
96+
formData: {
97+
file: fs.createReadStream(npmTarPath)
98+
}
99+
},
100+
function(err, res, body) {
101+
if (err) {
102+
console.error(err);
103+
process.exitCode = 1;
104+
throw err;
105+
}
106+
107+
var formattedResponse = JSON.parse(body);
108+
109+
if (formattedResponse.errors) {
110+
process.exitCode = 1;
111+
console.error(formattedResponse.errors);
112+
throw formattedResponse.errors;
113+
}
114+
115+
exec(`del ${npmTarPath}`);
116+
exec(`git tag v${releaseVersion}`);
117+
exec(
118+
`git push origin HEAD:${tempPublishBranch} --follow-tags --verbose`
119+
);
120+
exec(`git checkout ${publishBranchName}`);
121+
exec(`git pull origin ${publishBranchName}`);
122+
exec(`git merge ${publishBranchName} --no-edit`);
123+
exec(
124+
`git push origin HEAD:${publishBranchName} --follow-tags --verbose`
125+
);
126+
exec(`git branch -d ${tempPublishBranch}`);
127+
exec(`git push origin --delete -d ${tempPublishBranch}`);
128+
}
129+
);
130+
};
131+
132+
request.post(
133+
{
134+
url: "https://api.github.com/repos/Microsoft/react-native/releases",
135+
headers: {
136+
"User-Agent": userAgent,
137+
Authorization: authHeader,
138+
"Content-Type": "application/json"
139+
},
140+
body: JSON.stringify({
141+
tag_name: `v${releaseVersion}`,
142+
target_commitish: publishBranchName,
143+
name: `v${releaseVersion}`,
144+
body: `v${releaseVersion}`,
145+
draft: false,
146+
prerelease: true
147+
})
148+
},
149+
function(err, res, body) {
150+
if (err) {
151+
console.log(err);
152+
throw new Error("Error fetching release id.");
153+
}
154+
155+
console.log("Created GitHub Release: " + JSON.stringify(body, null, 2));
156+
var releaseId = JSON.parse(body).id;
157+
uploadReleaseAssetUrl = assetUpdateUrl.replace(/{id}/, releaseId);
158+
159+
uploadTarBallToRelease();
160+
}
161+
);
78162
}
79163

80164
doPublish();

0 commit comments

Comments
 (0)