-
Notifications
You must be signed in to change notification settings - Fork 976
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at auto generating sdk configs (#7833)
* First pass at auto generating sdk configs * Fixed formatting issues * Removed extra command * Deleted unnecessary files * Fixed more linting' * Removed test assertion * Fixed formatting * Updated erros * Misc * Updated platforms list * Undid last changes * Addressed comments * Fixed client test * Driveby type fixing * missed a spot * Fixed test * Fix issue where if a user passes in an empty 'out' parameter, the CLI crashes * Added intelligent sensing where app should be * Fixed formatting * Fixed lint * Fixed app dir * Misc * Wrote tests * Reverted apps sdkconfig changes * Fixed formatting * Small changes * Revert shrinkwrap changes * Updated test:management script * Fixed apps-sdkconfig boolean check * Fixed more boolean * Fixed formatting * Added changelog * Added new options * Removed unused var * Added experimental flag * Moved apps:init behind a flag * Added apps:init command * Removed unnecessary experiments * Addressed comments --------- Co-authored-by: Joe Hanley <joehanley@google.com>
- Loading branch information
Showing
12 changed files
with
732 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import * as fs from "fs-extra"; | ||
import * as path from "path"; | ||
|
||
import { Command } from "../command"; | ||
import { | ||
AppConfig, | ||
AppPlatform, | ||
getAppConfigFile, | ||
getAppPlatform, | ||
getPlatform, | ||
getSdkConfig, | ||
getSdkOutputPath, | ||
sdkInit, | ||
writeConfigToFile, | ||
} from "../management/apps"; | ||
import { requireAuth } from "../requireAuth"; | ||
import { logger } from "../logger"; | ||
import { Options } from "../options"; | ||
import { needProjectId } from "../projectUtils"; | ||
import { Platform } from "../dataconnect/types"; | ||
import { assertEnabled } from "../experiments"; | ||
|
||
export interface AppsInitOptions extends Options { | ||
out?: string | boolean; | ||
} | ||
|
||
function logUse(platform: AppPlatform, filePath: string) { | ||
switch (platform) { | ||
case AppPlatform.WEB: | ||
logger.info(` | ||
How to use your JS SDK Config: | ||
ES Module: | ||
import { initializeApp } from 'firebase/app'; | ||
import json from './${filePath || "firebase-sdk-config.json"}'; | ||
initializeApp(json); // or copy and paste the config directly from the json file here | ||
// CommonJS Module: | ||
const { initializeApp } = require('firebase/app'); | ||
const json = require('./firebase-js-config.json'); | ||
initializeApp(json); // or copy and paste the config directly from the json file here`); | ||
break; | ||
case AppPlatform.ANDROID: | ||
logger.info( | ||
`Visit https://firebase.google.com/docs/android/setup#add-config-file | ||
for information on editing your gradle file and adding Firebase SDKs to your app. | ||
If you're using Unity or C++, visit https://firebase.google.com/docs/cpp/setup?platform=android#add-config-file | ||
for information about adding your config file to your project.`, | ||
); | ||
break; | ||
case AppPlatform.IOS: | ||
logger.info( | ||
`Visit https://firebase.google.com/docs/ios/setup#add-config-file | ||
for information on adding the config file to your targets and adding Firebase SDKs to your app. | ||
If you're using Unity or C++, visit https://firebase.google.com/docs/cpp/setup?platform=ios#add-config-file | ||
for information about adding your config file to your project.`, | ||
); | ||
break; | ||
} | ||
} | ||
|
||
function toAppPlatform(str: string) { | ||
switch (str.toUpperCase()) { | ||
case Platform.ANDROID: | ||
return Platform.ANDROID as unknown as AppPlatform.ANDROID; | ||
case Platform.IOS: | ||
return Platform.IOS as unknown as AppPlatform.IOS; | ||
case Platform.WEB: | ||
return Platform.WEB as unknown as AppPlatform.WEB; | ||
} | ||
throw new Error(`Platform ${str} is not compatible with apps:configure`); | ||
} | ||
|
||
export const command = new Command("apps:init [platform] [appId]") | ||
.description("Automatically download and create config of a Firebase app. ") | ||
.before(requireAuth) | ||
.option("-o, --out [file]", "(optional) write config output to a file") | ||
.action(async (platform = "", appId = "", options: AppsInitOptions): Promise<AppConfig> => { | ||
assertEnabled("appsinit", "autoconfigure an app"); | ||
if (typeof options.out === "boolean") { | ||
throw new Error("Please specify a file path to output to."); | ||
} | ||
const config = options.config; | ||
const appDir = process.cwd(); | ||
// auto-detect the platform | ||
const detectedPlatform = platform ? toAppPlatform(platform) : await getPlatform(appDir, config); | ||
|
||
let sdkConfig: AppConfig | undefined; | ||
while (sdkConfig === undefined) { | ||
try { | ||
sdkConfig = await getSdkConfig(options, getAppPlatform(detectedPlatform), appId); | ||
} catch (e) { | ||
if ((e as Error).message.includes("associated with this Firebase project")) { | ||
const projectId = needProjectId(options); | ||
await sdkInit(platform, { ...options, project: projectId }); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
let outputPath = options.out; | ||
|
||
const fileInfo = getAppConfigFile(sdkConfig, detectedPlatform); | ||
let relativePath = ""; | ||
outputPath = outputPath || (await getSdkOutputPath(appDir, detectedPlatform, options)); | ||
const outputDir = path.dirname(outputPath); | ||
fs.mkdirpSync(outputDir); | ||
relativePath = path.relative(appDir, outputPath); | ||
const written = await writeConfigToFile( | ||
outputPath, | ||
options.nonInteractive, | ||
fileInfo.fileContents, | ||
); | ||
|
||
if (written) { | ||
logger.info(`App configuration is written in ${relativePath}`); | ||
} | ||
logUse(detectedPlatform, relativePath); | ||
|
||
return sdkConfig; | ||
}); |
Oops, something went wrong.