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