@@ -28,22 +28,26 @@ import { FirebaseError } from "../../../error";
28
28
import { camelCase , snakeCase , upperFirst } from "lodash" ;
29
29
import { logSuccess , logBullet , promptForDirectory , envOverride , logWarning } from "../../../utils" ;
30
30
import { 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" ;
31
36
32
- export const FDC_APP_FOLDER = "_FDC_APP_FOLDER" ;
33
37
export type SDKInfo = {
34
38
connectorYamlContents : string ;
35
39
connectorInfo : ConnectorInfo ;
36
40
displayIOSWarning : boolean ;
37
41
} ;
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 ) ;
40
44
await actuate ( sdkInfo , config ) ;
41
45
logSuccess (
42
46
`If you'd like to add more generated SDKs to your app your later, run ${ clc . bold ( "firebase init dataconnect:sdk" ) } again` ,
43
47
) ;
44
48
}
45
49
46
- async function askQuestions ( setup : Setup , config : Config ) : Promise < SDKInfo > {
50
+ async function askQuestions ( setup : Setup , config : Config , options : Options ) : Promise < SDKInfo > {
47
51
const serviceCfgs = readFirebaseJson ( config ) ;
48
52
// TODO: This current approach removes comments from YAML files. Consider a different approach that won't.
49
53
const serviceInfos = await Promise . all (
@@ -69,7 +73,21 @@ async function askQuestions(setup: Setup, config: Config): Promise<SDKInfo> {
69
73
70
74
// First, lets check if we are in an app directory
71
75
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
+ }
73
91
if ( targetPlatform === Platform . NONE && ! process . env [ FDC_APP_FOLDER ] ?. length ) {
74
92
// If we aren't in an app directory, ask the user where their app is, and try to autodetect from there.
75
93
appDir = await promptForDirectory ( {
@@ -114,14 +132,22 @@ async function askQuestions(setup: Setup, config: Config): Promise<SDKInfo> {
114
132
( framework ) => ! newConnectorYaml ! . generate ?. javascriptSdk ! [ framework ] ,
115
133
) ;
116
134
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
+
125
151
for ( const framework of additionalFrameworks ) {
126
152
newConnectorYaml ! . generate ! . javascriptSdk ! [ framework ] = true ;
127
153
}
0 commit comments