import * as android from '../lib/android' ; import * as cocoapods from '../lib/cocoapods' ; import * as deeplinking from '../lib/deeplinking' ; import * as env from '../lib/env' ; import * as fastlane from '../lib/fastlane' ; import * as fs from '../lib/fs' ; import * as ios from '../lib/ios' ; import * as link from '../lib/link' ; import * as helpers from '../helpers' ; import * as modules from '../lib/modules' ; import * as path from '../lib/path' ; import * as rename from '../lib/rename' ; import * as web from '../lib/web' ; import * as os from '../lib/os' ; import { Config , NPMPackageConfig } from '../types' ; export interface BuilderArgs { option : ( a : string , b : { alias : string ; default : string ; } ) => void ; } export interface HandlerArgs { platform : string ; env : string ; onlyDefault : boolean ; } const TEMPLATE_ANDROID_PACKAGE = 'com.brandingbrand.reactnative.and.flagship' ; export const command = 'init [platform]' ; export const describe = 'initialize FLAGSHIP for [platform]' ; export function builder ( yargs ) { yargs . option ( 'env' , { alias : 'e' , default : 'prod' } ) ; } export function handler ( argv ) { const platform = argv . platform ; const doAndroid = ! platform || platform === 'android' || platform === 'native' ; const doIOS = ( ! platform || platform === 'ios' || platform === 'native' ) && ! os . win ; const doWeb = ! platform || platform === 'web' ; if ( ! doAndroid && ! doIOS && ! doWeb ) { helpers . logError ( `unrecognized platform ${ platform }` ) ; return process . exit ( 1 ) ; } helpers . logInfo ( `Flagship ${ platform } init` ) ; const projectPackageJSON = require ( path . project . resolve ( 'package.json' ) ) ; const configuration = initEnvironment ( argv . env , projectPackageJSON , argv . onlyDefault ) ; if ( doAndroid ) { initAndroid ( projectPackageJSON , configuration , projectPackageJSON . version , argv . env ) ; } if ( doIOS ) { initIOS ( projectPackageJSON , configuration , projectPackageJSON . version , argv . env ) ; } if ( doWeb ) { initWeb ( projectPackageJSON , configuration , argv . env ) ; } link . link ( projectPackageJSON . flagship && projectPackageJSON . flagship . forceLink ) . then ( ( ) => { if ( doAndroid ) { modules . android ( projectPackageJSON , configuration , 'postLink' ) ; } if ( doIOS ) { modules . ios ( projectPackageJSON , configuration , 'postLink' ) ; cocoapods . install ( ) ; } } ) . catch ( err => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } param { string } environmentIdentifier param { object } packageJSON param { boolean } onlyDefault returns { object } function initEnvironment ( environmentIdentifier , packageJSON , onlyDefault ? ) { const configuration = env . configuration ( environmentIdentifier , packageJSON ) ; env . write ( configuration ) ; env . createEnvIndex ( onlyDefault ? environmentIdentifier : undefined ) ; return configuration ; } param { object } packageJSON param { object } configuration param { string } version param { string } environmentIdentifier function initAndroid ( packageJSON , configuration , version , environmentIdentifier ) { helpers . logInfo ( 'beginning Android initialization' ) ; fs . clone ( 'android' ) ; const androidConfig = android . androidConfigWithDefault ( configuration . android ) ; const pkgId = configuration . bundleIds && configuration . bundleIds . android ? configuration . bundleIds . android . toLowerCase ( ) : `com.brandingbrand.reactnative.and.${ configuration . name . toLowerCase ( ) }` ; rename . source ( 'FLAGSHIP' , configuration . name , 'android' ) ; rename . source ( 'CONFIG_BUNDLE_ID' , pkgId , 'android' ) ; rename . pkgDirectory ( TEMPLATE_ANDROID_PACKAGE , pkgId , path . android . mainPath ( ) , 'java' ) ; rename . pkgDirectory ( TEMPLATE_ANDROID_PACKAGE , pkgId , path . android . debugPath ( ) , 'java' ) ; fastlane . configure ( path . android . fastfilePath ( ) , configuration ) ; android . urlScheme ( configuration ) ; android . urlSchemeHost ( androidConfig ) ; deeplinking . addDeeplinkHosts ( configuration . associatedDomains ) ; android . displayName ( configuration ) ; android . bundleId ( configuration ) ; android . icon ( configuration ) ; android . launchScreen ( configuration ) ; android . version ( version , androidConfig ) ; android . sentryProperties ( configuration ) ; android . setEnvSwitcherInitialEnv ( configuration , environmentIdentifier ) ; android . additionalDependencies ( androidConfig ) ; android . mainApplicationAttributes ( androidConfig ) ; android . mainActivityAttributes ( androidConfig ) ; android . mainApplicationElements ( androidConfig ) ; android . googleMaps ( configuration ) ; android . exceptionDomains ( configuration ) ; if ( ! configuration . disableDevFeature ) { android . addDevMenuFlag ( configuration ) ; } modules . android ( packageJSON , configuration , 'preLink' ) ; helpers . logInfo ( 'finished Android initialization' ) ; } param { object } packageJSON param { object } configuration param { string } version param { string } environmentIdentifier function initIOS ( packageJSON , configuration , version , environmentIdentifier ) { helpers . logInfo ( 'beginning iOS initialization' ) ; fs . clone ( 'ios' ) ; rename . source ( 'FLAGSHIP' , configuration . name , 'ios' ) ; rename . files ( 'FLAGSHIP' , configuration . name , 'ios' ) ; fastlane . configure ( path . ios . fastfilePath ( ) , configuration ) ; ios . urlScheme ( configuration ) ; ios . displayName ( configuration ) ; ios . bundleId ( configuration ) ; ios . icon ( configuration ) ; ios . launchScreen ( configuration ) ; ios . version ( configuration , version ) ; ios . exceptionDomains ( configuration ) ; ios . capabilities ( configuration ) ; ios . targetedDevice ( configuration ) ; ios . entitlements ( configuration ) ; ios . usageDescription ( configuration ) ; ios . backgroundModes ( configuration ) ; ios . sentryProperties ( configuration ) ; ios . iosExtensions ( configuration , version ) ; ios . setEnvSwitcherInitialEnv ( configuration , environmentIdentifier ) ; if ( configuration . ios ) { if ( configuration . ios . pods ) { if ( configuration . ios . pods . sources ) { cocoapods . sources ( configuration . ios . pods . sources ) ; } if ( configuration . ios . pods . newPods ) { cocoapods . add ( configuration . ios . pods . newPods ) ; } } if ( configuration . ios . frameworks ) { ios . frameworks ( configuration ) ; } } if ( ! configuration . disableDevFeature ) { ios . addDevMenuFlag ( configuration ) ; } modules . ios ( packageJSON , configuration , 'preLink' ) ; helpers . logInfo ( 'finished iOS initialization' ) ; } param { object } packageJSON param { object } configuration param { string } environmentIdentifier function initWeb ( packageJSON , configuration , environmentIdentifier ) { helpers . logInfo ( 'beginning Web initialization' ) ; fs . copySync ( path . flagship . resolve ( '../fsweb' ) , path . project . resolve ( 'web' ) , { dereference : true } ) ; fs . writeFileSync ( path . project . resolve ( 'web' , 'config.web.json' ) , JSON . stringify ( { defaultEnvName : environmentIdentifier } ) ) ; web . homepage ( configuration . webPath || '/' ) ; web . title ( configuration . webTitle ) ; web . headerScripts ( configuration . webScriptInjectHeader ) ; web . footerScripts ( configuration . webScriptInjectFooter ) ; web . install ( ) ; web . link ( packageJSON ) ; helpers . logInfo ( 'finished Web initialization' ) ; }