| 
 | 1 | +var fs = require('fs');  | 
 | 2 | +var prompt = require('prompt');  | 
 | 3 | + | 
 | 4 | +// Default settings for using ios and android with Firebase  | 
 | 5 | +var usingiOS = false, usingAndroid = false;  | 
 | 6 | + | 
 | 7 | +// The directories where the Podfile and include.gradle are stored  | 
 | 8 | +var directories = {  | 
 | 9 | +    ios: './platforms/ios',  | 
 | 10 | +    android: './platforms/android'  | 
 | 11 | +}  | 
 | 12 | + | 
 | 13 | +console.log('NativeScript Firebase Plugin Installation');  | 
 | 14 | +prompt.start();  | 
 | 15 | +askiOSPrompt();  | 
 | 16 | + | 
 | 17 | +/**  | 
 | 18 | + * Prompt the user if they are integrating Firebase with iOS  | 
 | 19 | + */  | 
 | 20 | +function askiOSPrompt() {  | 
 | 21 | +    prompt.get({  | 
 | 22 | +        name: 'using_ios',  | 
 | 23 | +        description: 'Are you using iOS (y/n)',  | 
 | 24 | +        default: 'y',  | 
 | 25 | +    }, function (err, result) {  | 
 | 26 | +        if (err) {  | 
 | 27 | +            return console.log(err);  | 
 | 28 | +        }  | 
 | 29 | +        if (isSelected(result.using_ios)) {  | 
 | 30 | +            usingiOS = true;  | 
 | 31 | +        }  | 
 | 32 | +        askAndroidPrompt();  | 
 | 33 | +    });  | 
 | 34 | +}  | 
 | 35 | + | 
 | 36 | +/**  | 
 | 37 | + * Prompt the user if they are integrating Firebase with Android  | 
 | 38 | + */  | 
 | 39 | +function askAndroidPrompt() {  | 
 | 40 | +    prompt.get({  | 
 | 41 | +        name: 'using_android',  | 
 | 42 | +        description: 'Are you using Android (y/n)',  | 
 | 43 | +        default: 'y',  | 
 | 44 | +    }, function (err, result) {  | 
 | 45 | +        if (err) {  | 
 | 46 | +            return console.log(err);  | 
 | 47 | +        }  | 
 | 48 | +        if (isSelected(result.using_android)) {  | 
 | 49 | +            usingAndroid = true;  | 
 | 50 | +        }  | 
 | 51 | +        if(usingiOS || usingAndroid) {  | 
 | 52 | +            promptQuestions();  | 
 | 53 | +        }  | 
 | 54 | +    });  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | +/**  | 
 | 58 | + * Prompt the user through the configurable firebase add-on services  | 
 | 59 | + */  | 
 | 60 | +function promptQuestions() {  | 
 | 61 | +    prompt.get([{  | 
 | 62 | +        name: 'remote_config',  | 
 | 63 | +        description: 'Are you using Firebase RemoteConfig (y/n)',  | 
 | 64 | +        default: 'n',  | 
 | 65 | +    }, {  | 
 | 66 | +        name: 'messaging',  | 
 | 67 | +        description: 'Are you using Firebase Messaging (y/n)',  | 
 | 68 | +        default: 'n'  | 
 | 69 | +    }, {  | 
 | 70 | +        name: 'storage',  | 
 | 71 | +        description: 'Are you using Firebase Storage (y/n)',  | 
 | 72 | +        default: 'n'  | 
 | 73 | +    }, {  | 
 | 74 | +        name: 'facebook_auth',  | 
 | 75 | +        description: 'Are you using Firebase Facebook Authentication (y/n)',  | 
 | 76 | +        default: 'n'  | 
 | 77 | +    }, {  | 
 | 78 | +        name: 'google_auth',  | 
 | 79 | +        description: 'Are you using Firebase Google Authentication (y/n)',  | 
 | 80 | +        default: 'n'  | 
 | 81 | +    }], function (err, result) {  | 
 | 82 | +        if (err) {  | 
 | 83 | +            return console.log(err);  | 
 | 84 | +        }  | 
 | 85 | +        if(usingiOS) {  | 
 | 86 | +            writePodFile(result);  | 
 | 87 | +        }  | 
 | 88 | +        if(usingAndroid) {  | 
 | 89 | +            writeGradleFile(result);  | 
 | 90 | +        }  | 
 | 91 | +        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 postinstall`.');  | 
 | 92 | +    });  | 
 | 93 | +}  | 
 | 94 | + | 
 | 95 | +/**  | 
 | 96 | + * Create the iOS PodFile for installing the Firebase iOS dependencies and service dependencies  | 
 | 97 | + *  | 
 | 98 | + * @param {any} result The answers to the micro-service prompts  | 
 | 99 | + */  | 
 | 100 | +function writePodFile(result) {  | 
 | 101 | +    if(!fs.existsSync(directories.ios)) {  | 
 | 102 | +        fs.mkdirSync(directories.ios);  | 
 | 103 | +    }  | 
 | 104 | +    fs.writeFile(directories.ios + '/Podfile',  | 
 | 105 | +    `pod 'Firebase', '~> 3.3.0'  | 
 | 106 | +    pod 'Firebase/Database'  | 
 | 107 | +    pod 'Firebase/Auth'  | 
 | 108 | +    pod 'Firebase/Crash'  | 
 | 109 | +
  | 
 | 110 | +    # Uncomment if you want to enable Remote Config  | 
 | 111 | +    ` + (isSelected(result.remote_config) ? `` : `#`) + `pod 'Firebase/RemoteConfig'  | 
 | 112 | +
  | 
 | 113 | +    # Uncomment if you want to enable FCM (Firebase Cloud Messaging)  | 
 | 114 | +    ` + (isSelected(result.messaging) ? `` : `#`) + `pod 'Firebase/Messaging'  | 
 | 115 | +
  | 
 | 116 | +    # Uncomment if you want to enable Firebase Storage  | 
 | 117 | +    ` + (isSelected(result.storage) ? `` : `#`) + `pod 'Firebase/Storage'  | 
 | 118 | +
  | 
 | 119 | +    # Uncomment if you want to enable Facebook Authentication  | 
 | 120 | +    ` + (isSelected(result.facebook_auth) ? `` : `#`) + `pod 'FBSDKCoreKit'  | 
 | 121 | +    ` + (isSelected(result.facebook_auth) ? `` : `#`) + `pod 'FBSDKLoginKit'  | 
 | 122 | +
  | 
 | 123 | +    # Uncomment if you want to enable Google Authentication  | 
 | 124 | +    ` + (isSelected(result.google_auth) ? `` : `#`) + `pod 'GoogleSignIn'`, function(err) {  | 
 | 125 | +        if(err) {  | 
 | 126 | +            return console.log(err);  | 
 | 127 | +        }  | 
 | 128 | +        console.log('Successfully created iOS (Pod) file.');  | 
 | 129 | +    });  | 
 | 130 | +}  | 
 | 131 | + | 
 | 132 | +/**  | 
 | 133 | + * Create the Android Gradle for installing the Firebase Android dependencies and service dependencies  | 
 | 134 | + *  | 
 | 135 | + * @param {any} result The answers to the micro-service prompts  | 
 | 136 | + */  | 
 | 137 | +function writeGradleFile(result) {  | 
 | 138 | +     if(!fs.existsSync(directories.android)) {  | 
 | 139 | +        fs.mkdirSync(directories.android);  | 
 | 140 | +    }  | 
 | 141 | +    fs.writeFile(directories.android + '/include.gradle',  | 
 | 142 | +    `  | 
 | 143 | +    android {  | 
 | 144 | +        productFlavors {  | 
 | 145 | +            "fireb" {  | 
 | 146 | +                dimension "fireb"  | 
 | 147 | +            }  | 
 | 148 | +        }  | 
 | 149 | +    }  | 
 | 150 | +
  | 
 | 151 | +    repositories {  | 
 | 152 | +        jcenter()  | 
 | 153 | +        mavenCentral()  | 
 | 154 | +    }  | 
 | 155 | +
  | 
 | 156 | +    dependencies {  | 
 | 157 | +        // make sure you have these versions by updating your local Android SDK's (Android Support repo and Google repo)  | 
 | 158 | +        compile "com.google.firebase:firebase-core:9.4.0"  | 
 | 159 | +        compile "com.google.firebase:firebase-database:9.4.0"  | 
 | 160 | +        compile "com.google.firebase:firebase-auth:9.4.0"  | 
 | 161 | +        compile "com.google.firebase:firebase-crash:9.4.0"  | 
 | 162 | +
  | 
 | 163 | +        // for reading google-services.json and configuration  | 
 | 164 | +        compile "com.google.android.gms:play-services-base:9.4.0"  | 
 | 165 | +
  | 
 | 166 | +        // Uncomment if you want to use 'Remote Config'  | 
 | 167 | +        ` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:9.4.0"  | 
 | 168 | +
  | 
 | 169 | +        // Uncomment if you want FCM (Firebase Cloud Messaging)  | 
 | 170 | +        ` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:9.4.0"  | 
 | 171 | +
  | 
 | 172 | +        // Uncomment if you want Google Cloud Storage  | 
 | 173 | +        ` + (isSelected(result.storage) ? `` : `//`) + ` compile 'com.google.firebase:firebase-storage:9.4.0'  | 
 | 174 | +
  | 
 | 175 | +        // Uncomment if you need Facebook Authentication  | 
 | 176 | +        ` + (isSelected(result.facebook_auth) ? `` : `//`) + ` compile "com.facebook.android:facebook-android-sdk:4.+"  | 
 | 177 | +
  | 
 | 178 | +        // Uncomment if you need Google Sign-In Authentication  | 
 | 179 | +        ` + (isSelected(result.google_auth) ? `` : `//`) + ` compile "com.google.android.gms:play-services-auth:9.4.0"  | 
 | 180 | +
  | 
 | 181 | +    }  | 
 | 182 | +    `, function(err) {  | 
 | 183 | +        if(err) {  | 
 | 184 | +            return console.log(err);  | 
 | 185 | +        }  | 
 | 186 | +        console.log('Successfully created Android (include.gradle) file.');  | 
 | 187 | +    });  | 
 | 188 | +}  | 
 | 189 | + | 
 | 190 | +/**  | 
 | 191 | + * Determines if the answer validates as selected  | 
 | 192 | + *  | 
 | 193 | + * @param {any} value The user input for a prompt  | 
 | 194 | + * @returns {true} The answer is yes, {false} The answer is no  | 
 | 195 | + */  | 
 | 196 | +function isSelected(value) {  | 
 | 197 | +    return (value && value.toLowerCase() === 'y')  | 
 | 198 | +}  | 
0 commit comments