UNPKG

4.37 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const child_process_1 = require("child_process");
4const path_1 = require("path");
5const fs_1 = require("fs");
6const request_1 = require("request");
7const appConfig = require(path_1.default.join(process.cwd(), 'env', 'env'));
8const packageJson = require(path_1.default.join(process.cwd(), 'package.json'));
9const github = new (require('github'))({
10 version: '3.0.0',
11 timeout: 5000
12});
13if (!appConfig.codepush) {
14 process.exit(0);
15}
16if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
17 console.log('Codepush building ignored for pull request');
18 process.exit(0);
19}
20if (!process.env.GITHUB_TOKEN) {
21 console.error('GITHUB_TOKEN env var is missing.');
22 process.exit(1);
23}
24if (!process.env.APPCENTER_TOKEN) {
25 console.error('APPCENTER_TOKEN env var is missing.');
26 process.exit(1);
27}
28github.authenticate({
29 type: 'basic',
30 username: process.env.GITHUB_TOKEN,
31 password: 'x-oauth-basic'
32});
33const USER = appConfig.codepush.user;
34const REPO = appConfig.codepush.repo;
35const currentHEADSha = child_process_1.execSync('git rev-parse HEAD').toString();
36if (!USER) {
37 console.error('Repo user not defined for codepush config');
38 process.exit(1);
39}
40if (!REPO) {
41 console.error('Repo key not defined for codepush config');
42 process.exit(1);
43}
44if (!currentHEADSha) {
45 console.error('Unable to get current SHA for repo');
46 process.exit(1);
47}
48packageJson.dependencies['react-native'] = 'meh';
49fs_1.default.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
50if (appConfig.codepush.android) {
51 try {
52 child_process_1.execSync(`appcenter codepush release-react -a ${appConfig.codepush.android.name} \
53-d Latest --development false --description ${currentHEADSha}`, { stdio: [0, 1, 2] });
54 tagDeploymentLabel(appConfig.codepush.android.name, 'AndroidBuild');
55 updateTag('LatestAndroidBuild', currentHEADSha, () => console.log(`Updated latest android build tag`));
56 }
57 catch (e) {
58 console.error('Android codepush deployment failed to build');
59 console.error(e);
60 }
61}
62if (appConfig.codepush.ios) {
63 try {
64 child_process_1.execSync(`appcenter codepush release-react -a ${appConfig.codepush.ios.name} \
65--plist-file ./ios/${appConfig.name}/Info.plist -d Latest --development false \
66--description ${currentHEADSha}`, { stdio: [0, 1, 2] });
67 tagDeploymentLabel(appConfig.codepush.ios.name, 'iOSBuild');
68 updateTag('LatestIOSBuild', currentHEADSha, () => console.log(`Updated latest ios build tag`));
69 }
70 catch (e) {
71 console.error('iOS codepush deployment failed to build');
72 console.error(e);
73 }
74}
75function tagDeploymentLabel(app, tagPrefix) {
76 request_1.default({
77 url: `https://api.appcenter.ms/v0.1/apps/${app}/deployments/Latest`,
78 json: true,
79 headers: {
80 'X-API-Token': process.env.APPCENTER_TOKEN
81 }
82 }, (error, response, body) => {
83 if (!error && body && body.latest_release &&
84 body.latest_release.description === currentHEADSha) {
85 createTag(tagPrefix + body.latest_release.label, currentHEADSha, () => console.log('Successfully tagged deployment label'));
86 }
87 });
88}
89function updateTag(tag, sha, cb) {
90 github.gitdata.getReference({
91 user: USER,
92 repo: REPO,
93 ref: 'tags/' + tag
94 }, (e, d) => {
95 if (e && e.code === 404) {
96 return createTag(tag, sha, cb);
97 }
98 if (d) {
99 github.gitdata.updateReference({
100 user: USER,
101 repo: REPO,
102 ref: 'tags/' + tag,
103 sha,
104 force: true
105 }, (e, d) => {
106 if (e) {
107 console.log('tag update error', tag, sha);
108 console.log(e);
109 process.exit(1);
110 }
111 cb();
112 });
113 }
114 });
115}
116function createTag(tag, sha, cb) {
117 github.gitdata.createReference({
118 user: USER,
119 repo: REPO,
120 ref: 'refs/tags/' + tag,
121 sha
122 }, (e, d) => {
123 if (e) {
124 console.log('tag create error', tag, sha);
125 console.log(e);
126 process.exit(1);
127 }
128 cb();
129 });
130}
131//# sourceMappingURL=codepush.js.map
\No newline at end of file