This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
/
BaseBuilder.js
316 lines (280 loc) · 9.07 KB
/
BaseBuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* @flow
*/
import { Project, ProjectUtils, Versions } from 'xdl';
import chalk from 'chalk';
import fp from 'lodash/fp';
import simpleSpinner from '@expo/simple-spinner';
import log from '../../log';
import { action as publishAction } from '../publish';
import BuildError from './BuildError';
const sleep = ms => new Promise(res => setTimeout(res, ms));
const secondsToMilliseconds = seconds => seconds * 1000;
type BuilderOptions = {
wait: boolean,
clearCredentials: boolean,
type?: string,
releaseChannel: string,
publish: boolean,
teamId?: string,
distP12Path?: string,
pushP12Path?: string,
provisioningProfilePath?: string,
};
export default class BaseBuilder {
projectDir: string = '';
options: BuilderOptions = {
wait: true,
clearCredentials: false,
releaseChannel: 'default',
publish: false,
};
run: () => Promise<void>;
constructor(projectDir: string, options: BuilderOptions) {
this.projectDir = projectDir;
this.options = options;
}
async command(options) {
try {
await this._checkProjectConfig();
await this.run(options);
} catch (e) {
if (!(e instanceof BuildError)) {
throw e;
} else {
log.error(e.message);
process.exit(1);
}
}
}
async _checkProjectConfig(): Promise<void> {
let { exp } = await ProjectUtils.readConfigJsonAsync(this.projectDir);
if (exp.isDetached) {
log.error(`\`exp build\` is not supported for detached projects.`);
process.exit(1);
}
}
async checkStatus({
platform = 'all',
current = true,
publicUrl,
sdkVersion,
}: { platform: string, current: boolean, publicUrl?: string } = {}): Promise<void> {
await this._checkProjectConfig();
log('Checking if current build exists...\n');
const buildStatus = await Project.buildAsync(this.projectDir, {
mode: 'status',
platform,
current,
...(publicUrl ? { publicUrl } : {}),
sdkVersion,
});
if (buildStatus.err) {
throw new Error('Error getting current build status for this project.');
}
if (!(buildStatus.jobs && buildStatus.jobs.length)) {
if (current && buildStatus.userHasBuiltAppBefore) {
log.warn(`Did you know that Expo provides over-the-air updates?
Please see the docs (${chalk.underline(
'https://docs.expo.io/versions/latest/guides/configuring-ota-updates'
)}) and check if you can use them instead of building your app binaries again.`);
}
log('No currently active or previous builds for this project.');
return;
}
log.raw();
log('=================');
log(' Builds Statuses ');
log('=================\n');
buildStatus.jobs.forEach((job, i) => {
let platform, packageExtension;
if (job.platform === 'ios') {
platform = 'iOS';
packageExtension = 'IPA';
} else {
platform = 'Android';
packageExtension = 'APK';
}
log(`### ${i} | ${platform} | ${constructBuildLogsUrl(job.id)} ###`);
let status;
switch (job.status) {
case 'pending':
case 'sent-to-queue':
status = `Build waiting in queue...
You can check the queue length at ${chalk.underline(constructTurtleStatusUrl())}`;
break;
case 'started':
status = 'Build started...';
break;
case 'in-progress':
status = 'Build in progress...';
break;
case 'finished':
status = 'Build finished.';
break;
case 'errored':
status = 'There was an error with this build.';
if (job.id) {
status += `
When requesting support, please provide this build ID:
${job.id}
`;
}
break;
default:
status = '';
break;
}
log(status);
if (job.status === 'finished') {
if (job.artifacts) {
log(`${packageExtension}: ${job.artifacts.url}`);
} else {
log(`Problem getting ${packageExtension} URL. Please try to build again.`);
}
}
log();
});
throw new BuildError('Cannot start new build, as there is a build in progress.');
}
async ensureReleaseExists(platform: string) {
if (this.options.hardcodeRevisionId) {
// Used for sandbox build
return [this.options.hardcodeRevisionId];
}
if (this.options.publish) {
const { ids, url, err } = await publishAction(this.projectDir, {
...this.options,
platform,
});
if (err) {
throw new BuildError(`No url was returned from publish. Please try again.\n${err}`);
} else if (!url || url === '') {
throw new BuildError('No url was returned from publish. Please try again.');
}
return ids;
} else {
log('Looking for releases...');
const release = await Project.getLatestReleaseAsync(this.projectDir, {
releaseChannel: this.options.releaseChannel,
platform,
});
if (!release) {
throw new BuildError('No releases found. Please create one using `exp publish` first.');
}
log(
`Using existing release on channel "${release.channel}":\n publicationId: ${
release.publicationId
}\n publishedTime: ${release.publishedTime}`
);
return [release.publicationId];
}
}
async wait(buildId, { timeout = 1200, interval = 60, publicUrl } = {}) {
let time = new Date().getTime();
log(`Waiting for build to complete. You can press Ctrl+C to exit.`);
await sleep(secondsToMilliseconds(interval));
const endTime = time + secondsToMilliseconds(timeout);
while (time <= endTime) {
const res = await Project.buildAsync(this.projectDir, {
current: false,
mode: 'status',
...(publicUrl ? { publicUrl } : {}),
});
const job = fp.compose(
fp.head,
fp.filter(job => buildId && job.id === buildId),
fp.getOr([], 'jobs')
)(res);
switch (job.status) {
case 'finished':
return job;
case 'pending':
case 'sent-to-queue':
case 'started':
case 'in-progress':
break;
case 'errored':
throw new BuildError(`Standalone build failed!`);
default:
throw new BuildError(`Unknown status: ${job.status} - aborting!`);
}
time = new Date().getTime();
await sleep(secondsToMilliseconds(interval));
}
throw new BuildError(
'Timeout reached! Project is taking longer than expected to finish building, aborting wait...'
);
}
async build(
expIds: Array<string>,
platform: string,
extraArgs: { publicUrl?: string, bundleIdentifier?: string } = {}
) {
log('Building...');
let opts = {
mode: 'create',
expIds,
platform,
releaseChannel: this.options.releaseChannel,
...(extraArgs.publicUrl ? { publicUrl: extraArgs.publicUrl } : {}),
};
if (platform === 'ios') {
opts = {
...opts,
type: this.options.type,
bundleIdentifier: extraArgs.bundleIdentifier,
};
}
// call out to build api here with url
const { id: buildId } = await Project.buildAsync(this.projectDir, opts);
log('Build started, it may take a few minutes to complete.');
if (buildId) {
log(`You can check the queue length at\n ${chalk.underline(constructTurtleStatusUrl())}\n`);
}
if (buildId) {
log(`You can monitor the build at\n\n ${chalk.underline(constructBuildLogsUrl(buildId))}\n`);
}
if (this.options.wait) {
simpleSpinner.start();
const waitOpts = extraArgs.publicUrl ? { publicUrl: extraArgs.publicUrl } : {};
const completedJob = await this.wait(buildId, waitOpts);
simpleSpinner.stop();
const artifactUrl = completedJob.artifactId
? constructArtifactUrl(completedJob.artifactId)
: completedJob.artifacts.url;
log(`${chalk.green('Successfully built standalone app:')} ${chalk.underline(artifactUrl)}`);
} else {
log('Alternatively, run `exp build:status` to monitor it from the command line.');
}
}
async checkIfSdkIsSupported(sdkVersion: string, platform: string) {
const isSupported = await Versions.canTurtleBuildSdkVersion(sdkVersion, platform);
if (!isSupported) {
const storeName = platform === 'ios' ? 'Apple App Store' : 'Google Play Store';
log.error(
chalk.red(
`Unsupported SDK version: our app builders don't have support for ${sdkVersion} version yet. Submitting the app to the ${storeName} may result in an unexpected behaviour`
)
);
}
}
}
function constructBuildLogsUrl(buildId: string): string {
return `${getExpoDomainUrl()}/builds/${buildId}`;
}
function constructTurtleStatusUrl(): string {
return `${getExpoDomainUrl()}/turtle-status`;
}
function constructArtifactUrl(artifactId: string): string {
return `${getExpoDomainUrl()}/artifacts/${artifactId}`;
}
function getExpoDomainUrl() {
if (process.env.EXPO_STAGING) {
return `https://staging.expo.io`;
} else if (process.env.EXPO_LOCAL) {
return `http://expo.test`;
} else {
return `https://expo.io`;
}
}