Skip to content

Commit

Permalink
Removed extra command
Browse files Browse the repository at this point in the history
  • Loading branch information
maneesht committed Oct 17, 2024
1 parent f740de4 commit 0432624
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 168 deletions.
145 changes: 0 additions & 145 deletions src/commands/apps-sdkconfig-init.ts

This file was deleted.

149 changes: 138 additions & 11 deletions src/commands/apps-sdkconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,38 @@ import { getOrPromptProject } from "../management/projects";
import { FirebaseError } from "../error";
import { requireAuth } from "../requireAuth";
import { logger } from "../logger";
import { promptOnce } from "../prompt";
import { promptForDirectory, promptOnce } from "../prompt";
import { Options } from "../options";

import { getPlatformFromFolder } from "../dataconnect/fileUtils";
import * as path from "path";
import { Platform } from "../dataconnect/types";
import { logBullet, logSuccess } from "../utils";
import { sdkInit } from "./apps-create";
import { readTemplate } from "../templates";
export function getSdkOutputPath(appDir: string, platform: Platform): string {
switch (platform) {
case Platform.ANDROID:
// build.gradle can be in either / or /android/app. We always want to place the google-services.json in /android/app.
// So we check the current directory if it's app, and if so, we'll place it in the current directory, if not, we'll put it in the android/app dir.
// Fallback is just to the current app dir.
if(path.dirname(appDir) !== 'app') {

Check failure on line 33 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `(path.dirname(appDir)·!==·'app'` with `·(path.dirname(appDir)·!==·"app"`

Check failure on line 33 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `(path.dirname(appDir)·!==·'app'` with `·(path.dirname(appDir)·!==·"app"`

Check failure on line 33 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `(path.dirname(appDir)·!==·'app'` with `·(path.dirname(appDir)·!==·"app"`
try {
const fileNames = fs.readdirSync(path.join(appDir, 'app'));

Check failure on line 35 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `'app'` with `"app"`

Check failure on line 35 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'app'` with `"app"`

Check failure on line 35 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'app'` with `"app"`
if(fileNames.includes('build.gradle')) {

Check failure on line 36 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `(fileNames.includes('build.gradle'` with `·(fileNames.includes("build.gradle"`

Check failure on line 36 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `(fileNames.includes('build.gradle'` with `·(fileNames.includes("build.gradle"`

Check failure on line 36 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `(fileNames.includes('build.gradle'` with `·(fileNames.includes("build.gradle"`
appDir = path.join(appDir, 'app');

Check failure on line 37 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `'app'` with `"app"`

Check failure on line 37 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'app'` with `"app"`

Check failure on line 37 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `'app'` with `"app"`
}
} catch {
// Wasn't able to find app dir. Default to outputting to current directory.
}
}
return path.join(appDir, "google-services.json");
case Platform.WEB:
return path.join(appDir, "firebase-js-config.json");
case Platform.IOS:
return path.join(appDir, "GoogleService-Info.plist");
}
throw new Error("Platform " + platform.toString() + " is not supported yet.");
}
export function checkForApps(apps: AppMetadata[], appPlatform: AppPlatform): void {
if (!apps.length) {
throw new FirebaseError(
Expand Down Expand Up @@ -91,7 +120,6 @@ export async function getSdkConfig(
try {
configData = await getAppConfig(appId, appPlatform);
} catch (err: any) {
console.log(err);
spinner.fail();
throw err;
}
Expand Down Expand Up @@ -122,7 +150,29 @@ export async function writeConfigToFile(
// TODO(mtewani): Make the call to get the fileContents a part of one of these util fns.
fs.writeFileSync(filename, fileContents);
}

async function getAutoInitConfigFile(

Check failure on line 153 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'getAutoInitConfigFile' is defined but never used

Check failure on line 153 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

'getAutoInitConfigFile' is defined but never used

Check failure on line 153 in src/commands/apps-sdkconfig.ts

View workflow job for this annotation

GitHub Actions / unit (18)

'getAutoInitConfigFile' is defined but never used
responseBody: any,
platform: AppPlatform,
): Promise<AppConfigurationData[]> {
if (platform === AppPlatform.WEB) {
const [esmTemplate, cjsTemplate] = await Promise.all([
readTemplate("setup/web-auto.esm.js"),
readTemplate("setup/web-auto.cjs.js"),
]);
const REPLACE_STR = "{/*--CONFIG--*/}";
return [
{
fileName: "index.esm.js",
fileContents: esmTemplate.replace(REPLACE_STR, JSON.stringify(responseBody, null, 2)),
},
{
fileName: "index.cjs.js",
fileContents: cjsTemplate.replace(REPLACE_STR, JSON.stringify(responseBody, null, 2)),
},
];
}
throw new FirebaseError("Unexpected app platform");
}
export const command = new Command("apps:sdkconfig [platform] [appId]")
.description(
"print the Google Services config of a Firebase app. " +
Expand All @@ -131,11 +181,88 @@ export const command = new Command("apps:sdkconfig [platform] [appId]")
.option("-o, --out [file]", "(optional) write config output to a file")
.before(requireAuth)
.action(async (platform = "", appId = "", options: Options): Promise<AppConfigurationData> => {
const appPlatform = getAppPlatform(platform);
const configData = await getSdkConfig(options, appPlatform, appId);
const fileInfo = getAppConfigFile(configData, appPlatform);
if (appPlatform === AppPlatform.WEB) {
fileInfo.sdkConfig = configData;
/**
* 1. If the user has already selected where they want to output to, then skip the autodetection
* 2. If the user hasn't already selected where they want to output to, determine what platform they want.
*/
let outputPath: string | undefined = undefined;
if (options.out === undefined) {
// do auto-download
let appDir = process.cwd();
const config = options.config;
if (!platform) {
// Detect what platform based on current user
let targetPlatform = await getPlatformFromFolder(appDir);
if (targetPlatform === Platform.NONE) {
// If we aren't in an app directory, ask the user where their app is, and try to autodetect from there.
appDir = await promptForDirectory({
config,
message: "Where is your app directory?",
});
targetPlatform = await getPlatformFromFolder(appDir);
}
if (targetPlatform === Platform.NONE || targetPlatform === Platform.MULTIPLE) {
if (targetPlatform === Platform.NONE) {
logBullet(`Couldn't automatically detect app your in directory ${appDir}.`);
} else {
logSuccess(`Detected multiple app platforms in directory ${appDir}`);
// Can only setup one platform at a time, just ask the user
}
const platforms = [
{ name: "iOS (Swift)", value: Platform.IOS },
{ name: "Web (JavaScript)", value: Platform.WEB },
{ name: "Android (Kotlin)", value: Platform.ANDROID },
{ name: "Flutter (Dart)", value: Platform.FLUTTER },
];
targetPlatform = await promptOnce({
message: "Which platform do you want to set up a generated SDK for?",
type: "list",
choices: platforms,
});
} else {
logSuccess(`Detected ${targetPlatform} app in directory ${appDir}`);
}
platform = targetPlatform as Platform;
outputPath = getSdkOutputPath(appDir, platform);
}
}
const outputDir = path.dirname(outputPath!);
fs.mkdirpSync(outputDir);
// TODO(mtewani): Map any -> unknown

// TODO(mtewani): Include message for Dart
// TODO(mtewani): Include prompt for optional appId
let sdkConfig: any;
while (sdkConfig === undefined) {
try {
sdkConfig = await getSdkConfig(options, getAppPlatform(platform), appId);
} catch (e) {
if ((e as Error).message.includes("associated with this Firebase project")) {
await sdkInit(platform as unknown as AppPlatform, options);
} else {
throw e;
}
}
}

const fileInfo = getAppConfigFile(sdkConfig, platform as unknown as AppPlatform);
await writeConfigToFile(outputPath!, options.nonInteractive, fileInfo.fileContents);

if (platform === AppPlatform.WEB) {
console.log(`
How to use your JS SDK Config:
ES Module:
import { initializeApp } from 'firebase/app';
import json from './firebase-js-config.json';
initializeApp(json);
// CommonJS Module:
const { initializeApp } from 'firebase/app';
const json = require('./firebase-js-config.json');
initializeApp(json);// instead of initializeApp(config);
`);
if (platform === AppPlatform.WEB) {
fileInfo.sdkConfig = sdkConfig;
}
}

if (options.out === undefined) {
Expand All @@ -145,8 +272,8 @@ export const command = new Command("apps:sdkconfig [platform] [appId]")

const shouldUseDefaultFilename = options.out === true || options.out === "";

const filename = shouldUseDefaultFilename ? configData.fileName : options.out;
const filename = shouldUseDefaultFilename ? sdkConfig.fileName : options.out;
await writeConfigToFile(filename, options.nonInteractive, fileInfo.fileContents);
logger.info(`App configuration is written in ${filename}`);
return configData;
return sdkConfig;
});
1 change: 0 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function load(client: any): any {
client.apps.create = loadCommand("apps-create");
client.apps.list = loadCommand("apps-list");
client.apps.sdkconfig = loadCommand("apps-sdkconfig");
client.apps.sdkconfigInit = loadCommand("apps-sdkconfig-init");
client.apps.android = {};
client.apps.android.sha = {};
client.apps.android.sha.list = loadCommand("apps-android-sha-list");
Expand Down
1 change: 1 addition & 0 deletions src/dataconnect/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export async function pickService(
// case insensitive exact match indicators for supported app platforms
const WEB_INDICATORS = ["package.json", "package-lock.json", "node_modules"];
const IOS_INDICATORS = ["info.plist", "podfile", "package.swift"];
// Note: build.gradle can be nested inside android/ and android/app.
const ANDROID_INDICATORS = ["androidmanifest.xml", "build.gradle", "build.gradle.kts"];
const DART_INDICATORS = ["pubspec.yaml", "pubspec.lock"];

Expand Down
13 changes: 2 additions & 11 deletions src/management/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,11 @@ function getAppConfigResourceString(appId: string, platform: AppPlatform): strin
function parseConfigFromResponse(

Check failure on line 297 in src/management/apps.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `⏎··responseBody:·any,⏎··platform:·AppPlatform,⏎` with `responseBody:·any,·platform:·AppPlatform`

Check failure on line 297 in src/management/apps.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `⏎··responseBody:·any,⏎··platform:·AppPlatform,⏎` with `responseBody:·any,·platform:·AppPlatform`

Check failure on line 297 in src/management/apps.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `⏎··responseBody:·any,⏎··platform:·AppPlatform,⏎` with `responseBody:·any,·platform:·AppPlatform`
responseBody: any,
platform: AppPlatform,
skipTemplate = false,
): AppConfigurationData {
if (platform === AppPlatform.WEB) {
if (skipTemplate) {
return {
fileName: WEB_CONFIG_FILE_NAME,
fileContents: JSON.stringify(responseBody, null, 2),
};
}
const JS_TEMPLATE = readTemplateSync("setup/web.js");
return {
fileName: WEB_CONFIG_FILE_NAME,
fileContents: JS_TEMPLATE.replace("{/*--CONFIG--*/}", JSON.stringify(responseBody, null, 2)),
fileContents: JSON.stringify(responseBody, null, 2),
};
} else if (platform === AppPlatform.ANDROID || platform === AppPlatform.IOS) {
return {
Expand All @@ -331,9 +323,8 @@ function parseConfigFromResponse(
export function getAppConfigFile(

Check failure on line 323 in src/management/apps.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `⏎··config:·any,⏎··platform:·AppPlatform,⏎` with `config:·any,·platform:·AppPlatform`

Check failure on line 323 in src/management/apps.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `⏎··config:·any,⏎··platform:·AppPlatform,⏎` with `config:·any,·platform:·AppPlatform`

Check failure on line 323 in src/management/apps.ts

View workflow job for this annotation

GitHub Actions / unit (18)

Replace `⏎··config:·any,⏎··platform:·AppPlatform,⏎` with `config:·any,·platform:·AppPlatform`
config: any,
platform: AppPlatform,
skipTemplate = false,
): AppConfigurationData {
return parseConfigFromResponse(config, platform, skipTemplate);
return parseConfigFromResponse(config, platform);
}

/**
Expand Down

0 comments on commit 0432624

Please sign in to comment.