update firebase init to accept multiple features#10762
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the firebase init command to accept multiple features from the command line. However, a critical bug was identified where running the command interactively without arguments causes a TypeError because the argument is passed as an empty string instead of an array, causing .filter() to fail. A solution was suggested to normalize the input parameter to handle both strings and arrays.
| export async function initAction(features: string[], options: Options): Promise<void> { | ||
| const invalidFeatures = features.filter((f) => !featureNames.includes(f)); | ||
| if (invalidFeatures.length > 0) { |
There was a problem hiding this comment.
Critical Bug: Interactive firebase init crashes with TypeError
When running firebase init without any arguments (the default interactive mode), the command runner in src/command.ts pads missing positional arguments with an empty string "" to match the expected positional arguments length.
Because of this, features is passed to initAction as "" (a string) instead of an array of strings. Calling .filter() on a string throws a runtime TypeError: features.filter is not a function, completely blocking the interactive initialization flow.
Solution
We can resolve this by accepting string[] | string for the first parameter, and normalizing it to a string[] array at the very beginning of the function. By declaring a local const features, we ensure all subsequent references in the function work seamlessly without further changes.
export async function initAction(rawFeatures: string[] | string, options: Options): Promise<void> {
const features = typeof rawFeatures === "string" ? (rawFeatures ? [rawFeatures] : []) : rawFeatures || [];
const invalidFeatures = features.filter((f) => !featureNames.includes(f));
if (invalidFeatures.length > 0) {There was a problem hiding this comment.
This seems wrong? Did a console log to check if features is an array or an empty string "", and it is an array not a string. With added logs
export async function initAction(features: string[], options: Options): Promise<void> {
console.log(`Array.isArray(features): ${Array.isArray(features)}`)firebase init outputs
$ firebase init
Array.isArray(features): true
######## #### ######## ######## ######## ### ###### ########
## ## ## ## ## ## ## ## ## ## ##
###### ## ######## ###### ######## ######### ###### ######
## ## ## ## ## ## ## ## ## ## ##
## #### ## ## ######## ######## ## ## ###### ########
You're about to initialize a Firebase project in this directory:
| .join("")}`; | ||
|
|
||
| export const command = new Command("init [feature]") | ||
| export const command = new Command("init [features...]") |
There was a problem hiding this comment.
Let's swap this to comma separated values ie firebase init functions,hosting,thing3
There was a problem hiding this comment.
Just to verify, checked our commands and found some that use variadic input firebase appdistribution:testers:add, these seem to expect space separated input.
$ firebase appdistribution:testers:add --help
Usage: firebase appdistribution:testers:add [options] [emails...]
Something like
firebase appdistribution:testers:add fake@fake.com fake2@fake.com --project aalej-6162-2
i Adding 2 testers to project
✔ Testers created successfully
Given the above, I wanted to verify if we still want to do comma separated input firebase init functions,hosting,thing3
There was a problem hiding this comment.
Although, I think we can also make the firebase init command more lenient and support both comma and space separated. So something like below would still work
firebase init feature1,feature2 feature3
joehan
left a comment
There was a problem hiding this comment.
LGTM after the slight args change
|
Note: some work is being done to make the |
Description
Updates
firebase initto accept multiple features. For example,firebase init hosting emulatorsScenarios Tested
firebase init hosting emulators- goes through the hosting + emulators set upfirebase init- verified that interactive feature selection still works`firebase init`
invalid feature
Sample Commands
firebase init hosting emulators