This repository was archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathinstaller.js
executable file
·364 lines (321 loc) · 11.5 KB
/
installer.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
var fs = require('fs');
var path = require('path');
var prompt = require('prompt-lite');
// Default settings for using ios and android with Firebase
var usingiOS = false, usingAndroid = false;
// The directories where the Podfile and include.gradle are stored
var directories = {
ios: './platforms/ios',
android: './platforms/android'
};
console.log('NativeScript Firebase Plugin Installation');
var appRoot = "../../";
var pluginConfigFile = "firebase.nativescript.json";
var pluginConfigPath = path.join(appRoot, pluginConfigFile);
var config = {};
function mergeConfig(result) {
for (var key in result) {
config[key] = isSelected(result[key]);
}
}
function saveConfig() {
fs.writeFileSync(pluginConfigPath, JSON.stringify(config, null, 4));
}
function readConfig() {
try {
config = JSON.parse(fs.readFileSync(pluginConfigPath));
} catch(e) {
console.log("Failed reading " + pluginConfigFile);
console.log(e);
config = {};
}
}
if (process.argv.indexOf("config") == -1 && fs.existsSync(pluginConfigPath)) {
readConfig();
console.log("Config file exists (" + pluginConfigFile + ")");
askiOSPromptResult(config);
askAndroidPromptResult(config);
promptQuestionsResult(config);
} else {
console.log("No existing " + pluginConfigFile + " config file found, so let's configure the Firebase plugin!");
prompt.start();
askiOSPrompt();
}
/**
* Prompt the user if they are integrating Firebase with iOS
*/
function askiOSPrompt() {
prompt.get({
name: 'using_ios',
description: 'Are you using iOS (y/n)',
default: 'y'
}, function (err, result) {
if (err) {
return console.log(err);
}
mergeConfig(result);
askiOSPromptResult(result);
askAndroidPrompt();
});
}
function askiOSPromptResult(result) {
if (isSelected(result.using_ios)) {
usingiOS = true;
}
}
/**
* Prompt the user if they are integrating Firebase with Android
*/
function askAndroidPrompt() {
prompt.get({
name: 'using_android',
description: 'Are you using Android (y/n)',
default: 'y'
}, function (err, result) {
if (err) {
return console.log(err);
}
mergeConfig(result);
askAndroidPromptResult(result);
if (usingiOS || usingAndroid) {
promptQuestions();
} else {
askSaveConfigPrompt();
}
});
}
function askAndroidPromptResult(result) {
if (isSelected(result.using_android)) {
usingAndroid = true;
}
}
/**
* Prompt the user through the configurable Firebase add-on services
*/
function promptQuestions() {
prompt.get([{
name: 'remote_config',
description: 'Are you using Firebase RemoteConfig (y/n)',
default: 'n'
}, {
name: 'messaging',
description: 'Are you using Firebase Messaging (y/n)',
default: 'n'
}, {
name: 'crash_reporting',
description: 'Are you using Firebase Crash Reporting (y/n)',
default: 'n'
}, {
name: 'storage',
description: 'Are you using Firebase Storage (y/n)',
default: 'n'
}, {
name: 'facebook_auth',
description: 'Are you using Firebase Facebook Authentication (y/n)',
default: 'n'
}, {
name: 'google_auth',
description: 'Are you using Firebase Google Authentication (y/n)',
default: 'n'
}], function (err, result) {
if (err) {
return console.log(err);
}
mergeConfig(result);
promptQuestionsResult(result);
askSaveConfigPrompt();
});
}
function promptQuestionsResult(result) {
if(usingiOS) {
writePodFile(result);
}
if(usingAndroid) {
writeGradleFile(result);
writeGoogleServiceCopyHook();
writeGoogleServiceGradleHook();
}
console.log('Firebase post install completed. To re-run this script, navigate to the root directory of `nativescript-plugin-firebase` in your `node_modules` folder and run: `npm run config`.');
}
function askSaveConfigPrompt() {
prompt.get({
name: 'save_config',
description: 'Do you want to save the selected configuration. Reinstalling the dependency will reuse the setup from: ' + pluginConfigFile + '. CI will be easier. (y/n)',
default: 'y'
}, function (err, result) {
if (err) {
return console.log(err);
}
if (isSelected(result.save_config)) {
saveConfig();
}
});
}
/**
* Create the iOS PodFile for installing the Firebase iOS dependencies and service dependencies
*
* @param {any} result The answers to the micro-service prompts
*/
function writePodFile(result) {
if(!fs.existsSync(directories.ios)) {
fs.mkdirSync(directories.ios);
}
try {
fs.writeFileSync(directories.ios + '/Podfile',
`pod 'Firebase', '~> 3.11.0'
pod 'Firebase/Database'
pod 'Firebase/Auth'
# Uncomment if you want to enable Remote Config
` + (isSelected(result.remote_config) ? `` : `#`) + `pod 'Firebase/RemoteConfig'
# Uncomment if you want to enable Crash Reporting
` + (isSelected(result.crash_reporting) ? `` : `#`) + `pod 'Firebase/Crash'
# Uncomment if you want to enable FCM (Firebase Cloud Messaging)
` + (isSelected(result.messaging) ? `` : `#`) + `pod 'Firebase/Messaging'
# Uncomment if you want to enable Firebase Storage
` + (isSelected(result.storage) ? `` : `#`) + `pod 'Firebase/Storage'
# Uncomment if you want to enable Facebook Authentication
` + (isSelected(result.facebook_auth) ? `` : `#`) + `pod 'FBSDKCoreKit'
` + (isSelected(result.facebook_auth) ? `` : `#`) + `pod 'FBSDKLoginKit'
# Uncomment if you want to enable Google Authentication
` + (isSelected(result.google_auth) ? `` : `#`) + `pod 'GoogleSignIn'`);
console.log('Successfully created iOS (Pod) file.');
} catch(e) {
console.log('Failed to create iOS (Pod) file.');
console.log(e);
}
}
/**
* Create the Android Gradle for installing the Firebase Android dependencies and service dependencies
*
* @param {any} result The answers to the micro-service prompts
*/
function writeGradleFile(result) {
if(!fs.existsSync(directories.android)) {
fs.mkdirSync(directories.android);
}
try {
fs.writeFileSync(directories.android + '/include.gradle',
`
android {
productFlavors {
"fireb" {
dimension "fireb"
}
}
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
// make sure you have these versions by updating your local Android SDK's (Android Support repo and Google repo)
compile "com.google.firebase:firebase-core:10.0.+"
compile "com.google.firebase:firebase-database:10.0.+"
compile "com.google.firebase:firebase-auth:10.0.+"
// for reading google-services.json and configuration
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : '10.0.+'
compile "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
// Uncomment if you want to use 'Remote Config'
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:10.0.+"
// Uncomment if you want to use 'Crash Reporting'
` + (isSelected(result.crash_reporting) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:10.0.+"
// Uncomment if you want FCM (Firebase Cloud Messaging)
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:10.0.+"
// Uncomment if you want Google Cloud Storage
` + (isSelected(result.storage) ? `` : `//`) + ` compile 'com.google.firebase:firebase-storage:10.0.+'
// Uncomment if you need Facebook Authentication
` + (isSelected(result.facebook_auth) ? `` : `//`) + ` compile "com.facebook.android:facebook-android-sdk:4.+"
// Uncomment if you need Google Sign-In Authentication
` + (isSelected(result.google_auth) ? `` : `//`) + ` compile "com.google.android.gms:play-services-auth:$googlePlayServicesVersion"
}
apply plugin: "com.google.gms.google-services"
`);
console.log('Successfully created Android (include.gradle) file.');
} catch(e) {
console.log('Failed to create Android (include.gradle) file.');
console.log(e);
}
}
/**
* Installs an after-prepare build hook to copy the app/App_Resources/Android/google-services.json to platform/android on build.
*/
function writeGoogleServiceCopyHook() {
console.log("Install google-service.json copy hook.");
try {
var scriptContent =
`
var path = require("path");
var fs = require("fs");
module.exports = function() {
var sourceGoogleJson = path.join(__dirname, "..", "..", "app", "App_Resources", "Android", "google-services.json");
var destinationGoogleJson = path.join(__dirname, "..", "..", "platforms", "android", "google-services.json");
if (fs.existsSync(sourceGoogleJson) && fs.existsSync(path.dirname(destinationGoogleJson))) {
console.log("Copy " + sourceGoogleJson + " to " + destinationGoogleJson + ".");
fs.writeFileSync(destinationGoogleJson, fs.readFileSync(sourceGoogleJson));
}
};
`;
var scriptPath = path.join(appRoot, "hooks", "after-prepare", "firebase-copy-google-services.js");
var afterPrepareDirPath = path.dirname(scriptPath);
var hooksDirPath = path.dirname(afterPrepareDirPath);
if (!fs.existsSync(afterPrepareDirPath)) {
if (!fs.existsSync(hooksDirPath)) {
fs.mkdirSync(hooksDirPath);
}
fs.mkdirSync(afterPrepareDirPath);
}
fs.writeFileSync(scriptPath, scriptContent);
} catch(e) {
console.log("Failed to install google-service.json copy hook.");
console.log(e);
}
}
function writeGoogleServiceGradleHook() {
console.log("Install firebase-build-gradle hook.");
try {
var scriptContent =
`
var path = require("path");
var fs = require("fs");
module.exports = function() {
console.log("Configure firebase");
var buildGradlePath = path.join(__dirname, "..", "..", "platforms", "android", "build.gradle");
if (fs.existsSync(buildGradlePath)) {
var buildGradleContent = fs.readFileSync(buildGradlePath).toString();
if (buildGradleContent.indexOf('classpath "com.google.gms:google-services:3.0.0"') != -1) {
return;
}
var search = -1;
search = buildGradleContent.indexOf("repositories", 0);
if (search == -1) {
return;
}
search = buildGradleContent.indexOf("dependencies", search);
if (search == -1) {
return;
}
search = buildGradleContent.indexOf("}", search);
if (search == -1) {
return;
}
buildGradleContent = buildGradleContent.substr(0, search - 1) + ' classpath "com.google.gms:google-services:3.0.0"\\n ' + buildGradleContent.substr(search - 1);
fs.writeFileSync(buildGradlePath, buildGradleContent);
}
};
`;
var scriptPath = path.join(appRoot, "hooks", "after-prepare", "firebase-build-gradle.js");
fs.writeFileSync(scriptPath, scriptContent);
} catch(e) {
console.log("Failed to install firebase-build-gradle hook.");
console.log(e);
}
}
/**
* Determines if the answer validates as selected
*
* @param {any} value The user input for a prompt
* @returns {boolean} The answer is yes, {false} The answer is no
*/
function isSelected(value) {
return value === true || (typeof value === "string" && value.toLowerCase() === 'y');
}