/
index.js
executable file
·458 lines (441 loc) · 14.7 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
const Base = require('yeoman-generator');
const randomString = require('randomstring');
require('colors');
const deploymentPlatforms = [
{ name: 'AppCenter', value: 'appcenter' },
{ name: 'AppStore', value: 'appstore' },
{ name: 'HockeyApp (deprecated)', value: 'hockeyapp' },
];
const certificatesTypes = [
{ name: 'Adhoc', value: 'adhoc' },
{ name: 'In House (Enterprise only)', value: 'enterprise' },
];
const cocoaPodsInstallCommands = [
{ commandName: 'sudo', args: ['gem', 'install', 'cocoapods'] },
{ commandName: 'pod', args: ['repo', 'update'], options: { cwd: 'ios' } },
];
const reactNativeLinkAppcenterCommands = [
{ commandName: 'react-native', args: ['link', 'appcenter'] },
{ commandName: 'react-native', args: ['link', 'appcenter-analytics'] },
{ commandName: 'react-native', args: ['link', 'appcenter-crashes'] },
];
const reactNativeLinkCodepushCommands = [
{ commandName: 'react-native', args: ['link', 'react-native-code-push'] },
];
const installAppCenterFastlanePluginCommands = [
{ commandName: 'fastlane', args: ['add_plugin', 'appcenter'] },
];
const installLoadJsonFastlanePluginCommands = [
{ commandName: 'fastlane', args: ['add_plugin', 'load_json'] },
];
const installNokogiriCommands = [
{ commandName: 'bundle', args: ['add', 'nokogiri'] },
];
const installGpgCommands = [{ commandName: 'brew', args: ['install', 'gpg'] }];
class FastlaneEnvGenerator extends Base {
initializing() {
this.composeWith('rn-toolbox:checkversion');
if (!this.config.get('fastlane')) {
this.log.error(
'Running fastlane setup is required to use the environments. You can run it with yo rn-toolbox:fastlane-setup'
.red.bold
);
}
}
prompting() {
const config = this.fs.readJSON(this.destinationPath('package.json'));
return this.prompt([
{
type: 'input',
name: 'projectName',
message:
'Please confirm the react-native project name (as in react-native-init <PROJECT_NAME>)',
default: config.name,
},
{
type: 'input',
name: 'environmentName',
message: 'The name for this new environment (lowercase, no space)',
default: 'myenv',
},
{
type: 'input',
name: 'repoGitBranch',
message:
'The name of your repository Git branch for the environment just set',
default: answers => answers.environmentName,
},
{
type: 'input',
name: 'companyName',
message:
'The name of the company which will be publishing this application (used to generate android Keystore)',
default: 'My Company',
},
{
type: 'list',
name: 'deploymentPlatform',
message: 'Which platform will you use for deployment?',
choices: deploymentPlatforms,
},
{
type: 'input',
name: 'appName',
message: 'The app name for this environment',
default: 'My App',
},
{
type: 'input',
name: 'appId',
message: 'The App Id for this environment',
default: answers =>
`com.${answers.companyName.toLowerCase().replace(' ', '')}.${
answers.projectName
}.${answers.environmentName}`,
},
{
type: 'list',
name: 'certificateType',
message: 'The type of certificate you will be using',
choices: certificatesTypes,
when: answers =>
['hockeyapp', 'appcenter'].includes(answers.deploymentPlatform),
},
{
type: 'input',
name: 'matchGit',
message: 'Your git repo for match',
default: 'git@github.com:mycompany/certificates.git',
},
{
type: 'input',
name: 'matchBranch',
message: 'The branch you want to use for match',
default: 'master',
},
{
type: 'input',
name: 'appleTeamId',
message: 'The developer.apple.com team id for the certificates',
default: 'XXXXXXXXXX',
},
{
type: 'input',
name: 'itunesTeamName',
message: 'The appstoreconnect.apple.com team name',
default: 'MyCompany',
when: answers => answers.deploymentPlatform === 'appstore',
},
{
type: 'input',
name: 'appstoreConnectAppleId',
message:
'An AppstoreConnect Apple Id (good practice: ID should have "developer" access - only allowed to upload builds). Can be entered later in fastlane/env.<environment>',
when: answers => answers.deploymentPlatform === 'appstore',
},
{
type: 'input',
name: 'appleId',
message:
'Your apple id (should be admin on the Apple Developer Portal)',
default: 'dev@mycompany.com',
},
{
type: 'input',
name: 'keystorePassword',
message: 'Your keystore password',
default: randomString.generate(),
validate: input => {
if (input.includes(' ')) return 'No whitespace allowed';
return true;
},
},
{
type: 'input',
name: 'hockeyAppToken',
message: 'A valid HockeyApp token',
when: answers => answers.deploymentPlatform === 'hockeyapp',
},
{
type: 'input',
name: 'androidPlayStoreJsonKeyPath',
message:
'A Google Play JSON Key relative path. Can be entered later in fastlane/env.<environment>',
when: answers => answers.deploymentPlatform === 'appstore',
},
{
type: 'confirm',
name: 'useCodePush',
message: 'Will you deploy with Appcenter CodePush on this environment?',
},
{
type: 'input',
name: 'appCenterUsername',
message: 'A valid App Center Username',
when: answers =>
answers.deploymentPlatform === 'appcenter' || answers.useCodePush,
},
{
type: 'input',
name: 'appCenterToken',
message: 'A valid App Center API token',
when: answers =>
answers.deploymentPlatform === 'appcenter' || answers.useCodePush,
},
{
type: 'input',
name: 'iosAppCenterId',
message:
'The iOS project id on AppCenter for this environment, should be different than Android and not contain spaces',
default: answers =>
`${answers.appName.replace(/ /g, '')}-ios-${answers.environmentName}`,
when: answers =>
answers.deploymentPlatform === 'appcenter' || answers.useCodePush,
},
{
type: 'input',
name: 'androidAppCenterId',
message:
'The Android project id on AppCenter, should be different than iOS and not contain spaces',
default: answers =>
`${answers.appName.replace(/ /g, '')}-android-${
answers.environmentName
}`,
when: answers =>
answers.deploymentPlatform === 'appcenter' || answers.useCodePush,
},
{
type: 'input',
name: 'iosCodePushDeploymentKey',
message:
"Your iOS CodePush deployment key (can be entered later in fastlane/env.<environment>.secret if the App and Deployments don't exist yet in Appcenter)",
when: answers => answers.useCodePush,
},
{
type: 'input',
name: 'androidCodePushDeploymentKey',
message:
"Your Android CodePush deployment key (can be entered later in fastlane/env.<environment>.secret if the App and Deployments don't exist yet in Appcenter)",
when: answers => answers.useCodePush,
},
{
type: 'input',
name: 'iosCodePushDeploymentName',
message:
"Your iOS CodePush deployment name (can be entered later in fastlane/env.<environment> if the App and Deployments don't exist yet in Appcenter)",
when: answers => answers.useCodePush,
},
{
type: 'input',
name: 'androidCodePushDeploymentName',
message:
"Your Android CodePush deployment name (can be entered later in fastlane/env.<environment> if the App and Deployments don't exist yet in Appcenter)",
when: answers => answers.useCodePush,
},
{
type: 'confirm',
name: 'useAppcenterSDK',
message:
'Will you be using Appcenter Analytics and Crash reporting on this environment?',
},
{
type: 'input',
name: 'iosAppcenterAppSecret',
message:
"Your AppCenter app secret for the ios App (can be entered later in fastlane/env.<environment> if the App doesn't exist yet in Appcenter)",
when: answers => answers.useAppcenterSDK,
},
{
type: 'input',
name: 'androidAppcenterAppSecret',
message:
"Your AppCenter app secret for the Android App (can be entered later in fastlane/env.<environment> if the app doesn't exist yet in Appcenter)",
when: answers => answers.useAppcenterSDK,
},
]).then(answers => {
this.answers = answers;
this.answers.lowerCaseProjectName = answers.projectName.toLowerCase();
// Default certificateType to appstore if platform is appstore
if (this.answers.deploymentPlatform === 'appstore') {
this.answers.certificateType = 'appstore';
}
});
}
writing() {
this.fs.copyTpl(
this.templatePath('fastlane/env'),
this.destinationPath(`fastlane/.env.${this.answers.environmentName}`),
this.answers
);
this.fs.copyTpl(
this.templatePath('fastlane/env.secret'),
this.destinationPath(
`fastlane/.env.${this.answers.environmentName}.secret`
),
this.answers
);
this.fs.copyTpl(
this.templatePath('environment/index.js'),
this.destinationPath(
`src/environment/index.${this.answers.environmentName}.js`
),
this.answers
);
}
install() {
// Create Keystore file
this._createKeystore();
// Install AppCenter Fastlane Plugin
if (this.answers.deploymentPlatform === 'appcenter') {
this._runInstallCommandsWithErrorMessages(
installAppCenterFastlanePluginCommands,
'the App Center Fastlane plugin',
'fastlane add_plugin appcenter'
);
}
// Install CodePush npm library
if (this.answers.useCodePush) {
this.yarnInstall(['react-native-code-push'], {
cwd: this.destinationRoot(),
});
this._runInstallCommandsWithErrorMessages(
installNokogiriCommands,
'Nokogiri (xml manipulation)',
'bundle add nokogiri'
);
this.log(
'Appcenter CodePush configuration was added to your project for this environment.'
);
}
// Install App Center npm libraries
if (this.answers.useAppcenterSDK) {
this.yarnInstall(
['appcenter', 'appcenter-analytics', 'appcenter-crashes'],
{ cwd: this.destinationRoot() }
);
this._runInstallCommandsWithErrorMessages(
installLoadJsonFastlanePluginCommands,
'the load_json Fastlane plugin',
'fastlane add_plugin load_json'
);
this.log(
'Appcenter Analytics and Crash reporting configuration were added to your project for this environment.'
);
}
// Install gpg if secrets archive is needed
if (this.answers.useSecretsArchive) {
this._runInstallCommandsWithErrorMessages(
installGpgCommands,
'gpg',
'brew install gpg'
);
this.log('GPG was added in order to encrypt the secrets archive.');
}
}
end() {
this._reactNativeLinkDependencies();
if (this.answers.useCodePush) {
this.log(
'CodePush has been linked but you might need to remove it from PodFile and link the binary manually in Xcode for it to work properly on iOS (see CodePush docs).'
.magenta.bold
);
this.log(
'CodePush config has been added but you still need to wrap your js entry point with the library + config (see CodePush docs).'
.green.bold
);
}
this.log('Environment has been created, please run'.green);
this.log(
`bundle exec fastlane ios setup --env=${this.answers.environmentName}`
.green.bold
);
this.log('to create the provisioning profiles'.green);
}
_reactNativeLinkDependencies() {
// Link AppCenter CodePush
if (this.answers.useCodePush) {
this.log(
'Linking AppCenter CodePush for you...\nPLEASE LEAVE THE KEY FIELDS BLANK.'
.magenta.bold
);
this._runInstallCommandsWithErrorMessages(
reactNativeLinkCodepushCommands,
'AppCenter CodePush',
'react-native link react-native-code-push'
);
}
if (this.answers.useAppcenterSDK) {
this.log('Installing CocoaPods (please provide sudo password)...');
this._runInstallCommandsWithErrorMessages(
cocoaPodsInstallCommands,
'CocoaPods',
'sudo gem install cocoapods && cd ios && pod init && pod repo update.'
);
}
// Link AppCenter SDK
if (this.answers.useAppcenterSDK) {
this.log(
'Linking AppCenter SDK for you...\nPLEASE LEAVE THE "SECRETS" FIELDS BLANK.'
.magenta.bold
);
this._runInstallCommandsWithErrorMessages(
reactNativeLinkAppcenterCommands,
'AppCenter SDK libraries',
'react-native link appcenter && react-native link appcenter-analytics && react-native link appcenter-crashes'
);
}
}
_runInstallCommandsWithErrorMessages(
commands,
library,
manualInstallationCommand
) {
const results = commands.map(command =>
this.spawnCommandSync(
command.commandName,
command.args || [],
command.options || {}
)
);
for (let i = 0; i < results.length; i += 1) {
if (results[i].error) {
this.log(
`Configuration went well but there was an error while installing ${library}. Please install it manually with \`${manualInstallationCommand}\`. Then rerun the generator.`
.red.bold
);
throw results[i].error;
}
break;
}
}
_createKeystore() {
const path = `android/app/${this.answers.lowerCaseProjectName}.${
this.answers.environmentName
}.keystore`;
if (!this.fs.exists(this.destinationPath(path))) {
this.spawnCommandSync('keytool', [
'-genkey',
'-v',
'-dname',
`OU=${this.answers.companyName}`,
'-keystore',
path,
'-alias',
this.answers.lowerCaseProjectName,
'-keyalg',
'RSA',
'-keysize',
'2048',
'-validity',
'10000',
'-storepass',
this.answers.keystorePassword,
'-keypass',
this.answers.keystorePassword,
]);
} else {
this.log('Keystore already exists, skipping...'.yellow);
}
}
}
module.exports = FastlaneEnvGenerator;