Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error messaging in apps:sdkconfig #4549

Merged
merged 3 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@

## Not-so-breaking

- Fix missing Connection header in RTDB emulator REST streaming API (https://github.com/firebase/firebase-tools-ui/issues/3329).
- Fixes missing Connection header in RTDB emulator REST streaming API (https://github.com/firebase/firebase-tools-ui/issues/3329).
- Fixes error messaging when working with apps in interactive/non-interactive modes (#4007).
- Removes unused `dotenv` dependency.
143 changes: 74 additions & 69 deletions src/commands/apps-sdkconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ import { FirebaseError } from "../error";
import { requireAuth } from "../requireAuth";
import { logger } from "../logger";
import { promptOnce } from "../prompt";
import { Options } from "../options";

async function selectAppInteractively(
apps: AppMetadata[],
appPlatform: AppPlatform
): Promise<AppMetadata> {
if (apps.length === 0) {
function checkForApps(apps: AppMetadata[], appPlatform: AppPlatform): void {
if (!apps.length) {
throw new FirebaseError(
`There are no ${appPlatform === AppPlatform.ANY ? "" : appPlatform + " "}apps ` +
"associated with this Firebase project"
);
}
}

async function selectAppInteractively(
apps: AppMetadata[],
appPlatform: AppPlatform
): Promise<AppMetadata> {
checkForApps(apps, appPlatform);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const choices = apps.map((app: any) => {
Expand Down Expand Up @@ -55,78 +60,78 @@ module.exports = new Command("apps:sdkconfig [platform] [appId]")
)
.option("-o, --out [file]", "(optional) write config output to a file")
.before(requireAuth)
.action(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async (platform = "", appId = "", options: any): Promise<AppConfigurationData> => {
let appPlatform = getAppPlatform(platform);

if (!appId) {
let projectId = needProjectId(options);
if (options.nonInteractive && !projectId) {
throw new FirebaseError("Must supply app and project ids in non-interactive mode.");
} else if (!projectId) {
const result = await getOrPromptProject(options);
projectId = result.projectId;
}
.action(async (platform = "", appId = "", options: Options): Promise<AppConfigurationData> => {
let appPlatform = getAppPlatform(platform);

const apps = await listFirebaseApps(projectId, appPlatform);
// if there's only one app, we don't need to prompt interactively
if (apps.length === 1) {
appId = apps[0].appId;
appPlatform = apps[0].platform;
} else if (options.nonInteractive) {
throw new FirebaseError(
`Project ${projectId} has multiple apps, must specify an app id.`
);
} else {
const appMetadata: AppMetadata = await selectAppInteractively(apps, appPlatform);
appId = appMetadata.appId;
appPlatform = appMetadata.platform;
}
if (!appId) {
let projectId = needProjectId(options);
if (options.nonInteractive && !projectId) {
throw new FirebaseError("Must supply app and project ids in non-interactive mode.");
} else if (!projectId) {
const result = await getOrPromptProject(options);
projectId = result.projectId;
}

let configData;
const spinner = ora(
`Downloading configuration data of your Firebase ${appPlatform} app`
).start();
try {
configData = await getAppConfig(appId, appPlatform);
} catch (err: any) {
spinner.fail();
throw err;
const apps = await listFirebaseApps(projectId, appPlatform);
// Fail out early if there's no apps.
checkForApps(apps, appPlatform);
// if there's only one app, we don't need to prompt interactively
if (apps.length === 1) {
// If there's only one, use it.
appId = apps[0].appId;
appPlatform = apps[0].platform;
} else if (options.nonInteractive) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this behave when there are 0 apps? Do we show the correct error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above, on L77, it checks for apps and will throw an error if there are no apps.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp, just saw that you had a screenshot of this case. LGTM

// If there's > 1 and we're non-interactive, fail.
throw new FirebaseError(`Project ${projectId} has multiple apps, must specify an app id.`);
} else {
// > 1, ask what the user wants.
const appMetadata: AppMetadata = await selectAppInteractively(apps, appPlatform);
appId = appMetadata.appId;
appPlatform = appMetadata.platform;
}
spinner.succeed();
}

const fileInfo = getAppConfigFile(configData, appPlatform);
if (appPlatform === AppPlatform.WEB) {
fileInfo.sdkConfig = configData;
}
let configData;
const spinner = ora(
`Downloading configuration data of your Firebase ${appPlatform} app`
).start();
try {
configData = await getAppConfig(appId, appPlatform);
} catch (err: any) {
spinner.fail();
throw err;
}
spinner.succeed();

if (options.out === undefined) {
logger.info(fileInfo.fileContents);
return fileInfo;
}
const fileInfo = getAppConfigFile(configData, appPlatform);
if (appPlatform === AppPlatform.WEB) {
fileInfo.sdkConfig = configData;
}

const shouldUseDefaultFilename = options.out === true || options.out === "";
const filename = shouldUseDefaultFilename ? configData.fileName : options.out;
if (fs.existsSync(filename)) {
if (options.nonInteractive) {
throw new FirebaseError(`${filename} already exists`);
}
const overwrite = await promptOnce({
type: "confirm",
default: false,
message: `${filename} already exists. Do you want to overwrite?`,
});
if (options.out === undefined) {
logger.info(fileInfo.fileContents);
return fileInfo;
}

if (!overwrite) {
return configData;
}
const shouldUseDefaultFilename = options.out === true || options.out === "";
const filename = shouldUseDefaultFilename ? configData.fileName : options.out;
if (fs.existsSync(filename)) {
if (options.nonInteractive) {
throw new FirebaseError(`${filename} already exists`);
}
const overwrite = await promptOnce({
type: "confirm",
default: false,
message: `${filename} already exists. Do you want to overwrite?`,
});

fs.writeFileSync(filename, fileInfo.fileContents);
logger.info(`App configuration is written in ${filename}`);

return configData;
if (!overwrite) {
return configData;
}
}
);

fs.writeFileSync(filename, fileInfo.fileContents);
logger.info(`App configuration is written in ${filename}`);

return configData;
});