This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 477
/
index.js
418 lines (381 loc) · 15.5 KB
/
index.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import * as ConfigUtils from '@expo/config';
import { Android, Credentials, Simulator, UserManager, Versions } from '@expo/xdl';
import chalk from 'chalk';
import CliTable from 'cli-table';
import fs from 'fs-extra';
import _ from 'lodash';
import ora from 'ora';
import path from 'path';
import CommandError from '../../CommandError';
import log from '../../log';
import prompt from '../../prompt';
import urlOpts from '../../urlOpts';
import { PLATFORMS } from '../build/constants';
import * as appleApi from '../../appleApi';
import { runAction, travelingFastlane } from '../../appleApi/fastlane';
import { findProjectRootAsync } from '../utils/ProjectUtils';
import * as ClientUpgradeUtils from '../utils/ClientUpgradeUtils';
import { createClientBuildRequest, getExperienceName, isAllowedToBuild } from './clientBuildApi';
import generateBundleIdentifier from './generateBundleIdentifier';
import selectAdhocProvisioningProfile from './selectAdhocProvisioningProfile';
import selectDistributionCert from './selectDistributionCert';
import selectPushKey from './selectPushKey';
import { Updater, clearTags } from './tagger';
const { IOS } = PLATFORMS;
export default program => {
program
.command('client:ios [project-dir]')
.option(
'--apple-id <login>',
'Apple ID username (please also set the Apple ID password as EXPO_APPLE_PASSWORD environment variable).'
)
.description(
'Build a custom version of the Expo client for iOS using your own Apple credentials and install it on your mobile device using Safari.'
)
.asyncActionProjectDir(async (projectDir, options) => {
const disabledServices = {
pushNotifications: {
name: 'Push Notifications',
reason:
'not yet available until API tokens are supported for the Push Notification system',
},
};
// get custom project manifest if it exists
// Note: this is the current developer's project, NOT the Expo client's manifest
const spinner = ora(`Finding custom configuration for the Expo client...`).start();
const appJsonPath = options.config || path.join(projectDir, 'app.json');
const appJsonExists = await ConfigUtils.fileExistsAsync(appJsonPath);
const { exp } = appJsonExists ? await ConfigUtils.readConfigJsonAsync(projectDir) : {};
if (exp) {
spinner.succeed(`Found custom configuration for the Expo client at ${appJsonPath}`);
} else {
spinner.warn(`Unable to find custom configuration for the Expo client`);
}
if (!_.has(exp, 'ios.config.googleMapsApiKey')) {
const disabledReason = exp
? `ios.config.googleMapsApiKey does not exist in configuration file found in ${appJsonPath}`
: 'No custom configuration file could be found. You will need to provide a json file with a valid ios.config.googleMapsApiKey field.';
disabledServices.googleMaps = { name: 'Google Maps', reason: disabledReason };
}
if (_.has(exp, 'ios.googleServicesFile')) {
const contents = await fs.readFile(
path.resolve(projectDir, exp.ios.googleServicesFile),
'base64'
);
exp.ios.googleServicesFile = contents;
}
const authData = await appleApi.authenticate(options);
const user = await UserManager.getCurrentUserAsync();
// check if any builds are in flight
const { isAllowed, errorMessage } = await isAllowedToBuild({
user,
appleTeamId: authData.team.id,
});
if (!isAllowed) {
throw new CommandError(
'CLIENT_BUILD_REQUEST_NOT_ALLOWED',
`New Expo client build request disallowed. Reason: ${errorMessage}`
);
}
const bundleIdentifier = generateBundleIdentifier(authData.team.id);
const experienceName = await getExperienceName({ user, appleTeamId: authData.team.id });
const context = {
...authData,
username: user ? user.username : null,
};
await appleApi.ensureAppExists(
context,
{ bundleIdentifier, experienceName },
{ enablePushNotifications: true }
);
const { devices } = await runAction(travelingFastlane.listDevices, [
'--all-ios-profile-devices',
context.appleId,
context.appleIdPassword,
context.team.id,
]);
const udids = devices.map(device => device.deviceNumber);
const distributionCert = await selectDistributionCert(context);
const pushKey = await selectPushKey(context);
const provisioningProfile = await selectAdhocProvisioningProfile(
context,
udids,
bundleIdentifier,
distributionCert.distCertSerialNumber
);
// push notifications won't work if we dont have any push creds
// we also dont store anonymous creds, so user needs to be logged in
if (pushKey === null || !user) {
const disabledReason =
pushKey === null
? 'you did not upload your push credentials'
: 'we require you to be logged in to store push credentials';
// TODO(quin): remove this when we fix push notifications
// keep the default push notification reason if we haven't implemented API tokens
disabledServices.pushNotifications.reason =
disabledServices.pushNotifications.reason || disabledReason;
}
if (Object.keys(disabledServices).length > 0) {
log.newLine();
log.warn('These services will be disabled in your custom Expo client:');
const table = new CliTable({ head: ['Service', 'Reason'], style: { head: ['cyan'] } });
table.push(
...Object.keys(disabledServices).map(serviceKey => {
const service = disabledServices[serviceKey];
return [service.name, service.reason];
})
);
log(table.toString());
log(
'See https://docs.expo.io/versions/latest/guides/adhoc-builds/#optional-additional-configuration-steps for more details.'
);
}
// if user is logged in, then we should update credentials
const credentialsList = [distributionCert, pushKey, provisioningProfile].filter(i => i);
if (user) {
// store all the credentials that we mark for update
const updateCredentialsFn = async listOfCredentials => {
if (listOfCredentials.length === 0) {
return;
}
const credentials = listOfCredentials.reduce(
(acc, credential) => {
return { ...acc, ...credential };
},
{ teamId: context.team.id }
);
await Credentials.updateCredentialsForPlatform(IOS, credentials, [], {
username: user.username,
experienceName,
bundleIdentifier,
});
};
const CredentialsUpdater = new Updater(updateCredentialsFn);
await CredentialsUpdater.updateAllAsync(credentialsList);
} else {
// clear update tags, we dont store credentials for anonymous users
clearTags(credentialsList);
}
let email;
if (user) {
email = user.email;
} else {
({ email } = await prompt({
name: 'email',
message: 'Please enter an email address to notify, when the build is completed:',
filter: value => value.trim(),
validate: value => (/.+@.+/.test(value) ? true : "That doesn't look like a valid email."),
}));
}
log.newLine();
let addUdid;
if (udids.length === 0) {
log(
'There are no devices registered to your Apple Developer account. Please follow the instructions below to register an iOS device.'
);
addUdid = true;
} else {
log(
'Custom builds of the Expo client can only be installed on devices which have been registered with Apple at build-time.'
);
log('These devices are currently registered on your Apple Developer account:');
const table = new CliTable({ head: ['Name', 'Identifier'], style: { head: ['cyan'] } });
table.push(...devices.map(device => [device.name, device.deviceNumber]));
log(table.toString());
const udidPrompt = await prompt({
name: 'addUdid',
message: 'Would you like to register a new device to use the Expo client with?',
type: 'confirm',
default: true,
});
addUdid = udidPrompt.addUdid;
}
const result = await createClientBuildRequest({
user,
context,
distributionCert,
provisioningProfile,
pushKey,
udids,
addUdid,
email,
bundleIdentifier,
customAppConfig: exp,
});
log.newLine();
if (addUdid) {
urlOpts.printQRCode(result.registrationUrl);
log(
'Open the following link on your iOS device (or scan the QR code) and follow the instructions to install the development profile:'
);
log.newLine();
log(chalk.green(`${result.registrationUrl}`));
log.newLine();
log('Please note that you can only register one iOS device per request.');
log(
"After you register your device, we'll start building your client, and you'll receive an email when it's ready to install."
);
} else {
urlOpts.printQRCode(result.statusUrl);
log('Your custom Expo client is being built! 🛠');
log(
'Open this link on your iOS device (or scan the QR code) to view build logs and install the client:'
);
log.newLine();
log(chalk.green(`${result.statusUrl}`));
}
log.newLine();
}, true);
program
.command('client:install:ios')
.description('Install the Expo client for iOS on the simulator')
.asyncAction(async () => {
const currentSdkConfig = await ClientUpgradeUtils.getExpoSdkConfig(process.cwd());
const currentSdkVersion = currentSdkConfig ? currentSdkConfig.sdkVersion : undefined;
if (!currentSdkVersion) {
log(
'Could not find your Expo project. If you run this from a project, we can help pick the right Expo client version!'
);
}
const sdkVersions = await Versions.sdkVersionsAsync();
const latestSdk = await Versions.newestSdkVersionAsync();
const currentSdk = sdkVersions[currentSdkVersion];
const recommendedClient = currentSdk
? ClientUpgradeUtils.getClient(currentSdk, 'ios')
: undefined;
if (currentSdk && !recommendedClient) {
log(
`You are currently using SDK ${currentSdkVersion}. Unfortunately, we couldn't detect the proper client version for this SDK.`
);
}
if (currentSdk && recommendedClient) {
const recommendedClientVersion = recommendedClient.version || 'version unknown';
const answer = await prompt({
type: 'confirm',
name: 'upgradeToRecommended',
message: `You are currently using SDK ${currentSdkVersion}. Would you like to install client ${recommendedClientVersion} released for this SDK?`,
});
if (answer.upgradeToRecommended) {
await Simulator.upgradeExpoAsync(recommendedClient.url);
log('Done!');
return;
}
} else {
const answer = await prompt({
type: 'confirm',
name: 'upgradeToLatest',
message: 'Do you want to install the latest client?',
});
if (answer.upgradeToLatest) {
await Simulator.upgradeExpoAsync();
log('Done!');
return;
}
}
const availableClients = ClientUpgradeUtils.getAvailableClients({
sdkVersions,
project: currentSdkConfig,
platform: 'ios',
});
if (availableClients.length === 0) {
const answer = await prompt({
type: 'confirm',
name: 'updateToAClient',
message: currentSdk
? `We don't have a compatible client for SDK ${currentSdkVersion}. Do you want to try the latest client?`
: "It looks like we don't have a compatible client. Do you want to try the latest client?",
});
if (answer.updateToAClient) {
await Simulator.upgradeExpoAsync();
log('Done!');
} else {
log('No client to install');
}
return;
}
const targetClient = await ClientUpgradeUtils.askClientToInstall({
currentSdkVersion,
latestSdkVersion: latestSdk.version,
clients: availableClients,
});
if (await Simulator.upgradeExpoAsync(targetClient.clientUrl)) {
log('Done!');
}
}, true);
program
.command('client:install:android')
.description('Install the Expo client for Android on a connected device or emulator')
.asyncAction(async () => {
const currentSdkConfig = await ClientUpgradeUtils.getExpoSdkConfig(process.cwd());
const currentSdkVersion = currentSdkConfig ? currentSdkConfig.sdkVersion : undefined;
if (!currentSdkVersion) {
log(
'Could not find your Expo project. If you run this from a project, we can help pick the right Expo client version!'
);
}
const sdkVersions = await Versions.sdkVersionsAsync();
const latestSdk = await Versions.newestSdkVersionAsync();
const currentSdk = sdkVersions[currentSdkVersion];
const recommendedClient = currentSdk
? ClientUpgradeUtils.getClient(currentSdk, 'android')
: undefined;
if (currentSdk && !recommendedClient) {
log(
`You are currently using SDK ${currentSdkVersion}. Unfortunately, we couldn't detect the proper client version for this SDK.`
);
}
if (currentSdk && recommendedClient) {
const recommendedClientVersion = recommendedClient.version || 'version unknown';
const answer = await prompt({
type: 'confirm',
name: 'upgradeToRecommended',
message: `You are currently using SDK ${currentSdkVersion}. Would you like to install client ${recommendedClientVersion} released for this SDK?`,
});
if (answer.upgradeToRecommended) {
await Android.upgradeExpoAsync(recommendedClient.url);
log('Done!');
return;
}
} else {
const answer = await prompt({
type: 'confirm',
name: 'upgradeToLatest',
message: 'Do you want to install the latest client?',
});
if (answer.upgradeToLatest) {
await Android.upgradeExpoAsync();
log('Done!');
return;
}
}
const availableClients = ClientUpgradeUtils.getAvailableClients({
sdkVersions,
project: currentSdkConfig,
platform: 'android',
});
if (availableClients.length === 0) {
const answer = await prompt({
type: 'confirm',
name: 'updateToAClient',
message: currentSdk
? `We don't have a compatible client for SDK ${currentSdkVersion}. Do you want to try the latest client?`
: "It looks like we don't have a compatible client. Do you want to try the latest client?",
});
if (answer.updateToAClient) {
await Android.upgradeExpoAsync();
log('Done!');
} else {
log('No client to install');
}
return;
}
const targetClient = await ClientUpgradeUtils.askClientToInstall({
currentSdkVersion,
latestSdkVersion: latestSdk.version,
clients: availableClients,
});
if (await Android.upgradeExpoAsync(targetClient.clientUrl)) {
log('Done!');
}
}, true);
};