@@ -28,22 +28,26 @@ import { FirebaseError } from "../../../error";
2828import { camelCase , snakeCase , upperFirst } from "lodash" ;
2929import { logSuccess , logBullet , promptForDirectory , envOverride , logWarning } from "../../../utils" ;
3030import { getGlobalDefaultAccount } from "../../../auth" ;
31+ import { Options } from "../../../options" ;
32+
33+ export const FDC_APP_FOLDER = "FDC_APP_FOLDER" ;
34+ export const FDC_SDK_FRAMEWORKS_ENV = "FDC_SDK_FRAMEWORKS" ;
35+ export const FDC_SDK_PLATFORM_ENV = "FDC_SDK_PLATFORM" ;
3136
32- export const FDC_APP_FOLDER = "_FDC_APP_FOLDER" ;
3337export type SDKInfo = {
3438 connectorYamlContents : string ;
3539 connectorInfo : ConnectorInfo ;
3640 displayIOSWarning : boolean ;
3741} ;
38- export async function doSetup ( setup : Setup , config : Config ) : Promise < void > {
39- const sdkInfo = await askQuestions ( setup , config ) ;
42+ export async function doSetup ( setup : Setup , config : Config , options : Options ) : Promise < void > {
43+ const sdkInfo = await askQuestions ( setup , config , options ) ;
4044 await actuate ( sdkInfo , config ) ;
4145 logSuccess (
4246 `If you'd like to add more generated SDKs to your app your later, run ${ clc . bold ( "firebase init dataconnect:sdk" ) } again` ,
4347 ) ;
4448}
4549
46- async function askQuestions ( setup : Setup , config : Config ) : Promise < SDKInfo > {
50+ async function askQuestions ( setup : Setup , config : Config , options : Options ) : Promise < SDKInfo > {
4751 const serviceCfgs = readFirebaseJson ( config ) ;
4852 // TODO: This current approach removes comments from YAML files. Consider a different approach that won't.
4953 const serviceInfos = await Promise . all (
@@ -69,7 +73,21 @@ async function askQuestions(setup: Setup, config: Config): Promise<SDKInfo> {
6973
7074 // First, lets check if we are in an app directory
7175 let appDir = process . env [ FDC_APP_FOLDER ] || process . cwd ( ) ;
72- let targetPlatform = await getPlatformFromFolder ( appDir ) ;
76+ let targetPlatform = envOverride (
77+ FDC_SDK_PLATFORM_ENV ,
78+ ( await getPlatformFromFolder ( appDir ) ) || Platform . NONE ,
79+ ) as Platform ;
80+
81+ if ( options . nonInteractive && targetPlatform === Platform . NONE ) {
82+ throw new FirebaseError (
83+ `In non-interactive mode, the target platform and app directory must be specified using environment variables if they cannot be automatically detected.
84+ Please set the ${ FDC_SDK_PLATFORM_ENV } and ${ FDC_APP_FOLDER } environment variables.
85+ For example:
86+ ${ clc . bold (
87+ `${ FDC_SDK_PLATFORM_ENV } =WEB ${ FDC_APP_FOLDER } =app-dir ${ FDC_SDK_FRAMEWORKS_ENV } =react firebase init dataconnect:sdk --non-interactive` ,
88+ ) } `,
89+ ) ;
90+ }
7391 if ( targetPlatform === Platform . NONE && ! process . env [ FDC_APP_FOLDER ] ?. length ) {
7492 // If we aren't in an app directory, ask the user where their app is, and try to autodetect from there.
7593 appDir = await promptForDirectory ( {
@@ -114,14 +132,22 @@ async function askQuestions(setup: Setup, config: Config): Promise<SDKInfo> {
114132 ( framework ) => ! newConnectorYaml ! . generate ?. javascriptSdk ! [ framework ] ,
115133 ) ;
116134 if ( unusedFrameworks . length > 0 ) {
117- const additionalFrameworks = await checkbox < ( typeof SUPPORTED_FRAMEWORKS ) [ number ] > ( {
118- message :
119- "Which frameworks would you like to generate SDKs for in addition to the TypeScript SDK? Press Enter to skip.\n" ,
120- choices : SUPPORTED_FRAMEWORKS . map ( ( frameworkStr ) => ( {
121- value : frameworkStr ,
122- checked : newConnectorYaml ?. generate ?. javascriptSdk ?. [ frameworkStr ] ,
123- } ) ) ,
124- } ) ;
135+ let additionalFrameworks : ( typeof SUPPORTED_FRAMEWORKS ) [ number ] [ ] = [ ] ;
136+ if ( options . nonInteractive ) {
137+ additionalFrameworks = envOverride ( FDC_SDK_FRAMEWORKS_ENV , "" )
138+ . split ( "," )
139+ . filter ( ( f ) => f ) as ( typeof SUPPORTED_FRAMEWORKS ) [ number ] [ ] ;
140+ } else {
141+ additionalFrameworks = await checkbox < ( typeof SUPPORTED_FRAMEWORKS ) [ number ] > ( {
142+ message :
143+ "Which frameworks would you like to generate SDKs for in addition to the TypeScript SDK? Press Enter to skip.\n" ,
144+ choices : SUPPORTED_FRAMEWORKS . map ( ( frameworkStr ) => ( {
145+ value : frameworkStr ,
146+ checked : newConnectorYaml ?. generate ?. javascriptSdk ?. [ frameworkStr ] ,
147+ } ) ) ,
148+ } ) ;
149+ }
150+
125151 for ( const framework of additionalFrameworks ) {
126152 newConnectorYaml ! . generate ! . javascriptSdk ! [ framework ] = true ;
127153 }
0 commit comments