Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions src/commands/functions-kits-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { requireConfig } from "../requireConfig";
import { Command } from "../command";
import { listKitConfigs } from "../functions/kits/config";
import { Options } from "../options";
import { logLabeledBullet } from "../utils";
import { logger } from "../logger";
import * as Table from "cli-table3";
Comment on lines +2 to +7

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Import FirebaseError to handle the case where the command is run outside of a Firebase project directory.

Suggested change
import { Command } from "../command";
import { listKitConfigs } from "../functions/kits/config";
import { Options } from "../options";
import { logLabeledBullet } from "../utils";
import { logger } from "../logger";
import * as Table from "cli-table3";
import { Command } from "../command";
import { listKitConfigs } from "../functions/kits/config";
import { Options } from "../options";
import { logLabeledBullet } from "../utils";
import { logger } from "../logger";
import * as Table from "cli-table3";
import { FirebaseError } from "../error";


export const command = new Command("functions:kits:list")
.description("list all the kits that are installed in your firebase.json")
.before(requireConfig)
.action((options: Options) => {
const firebaseConfig = options.config;
const validatedConfig = firebaseConfig.src;
const kitConfigs = listKitConfigs(validatedConfig);
Comment on lines +12 to +15

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

If the command is run outside of a Firebase project directory, options.config will be undefined. Accessing options.config.src will throw a TypeError. We should explicitly check if options.config is defined and throw a FirebaseError if it is not, adhering to the repository style guide on strict null checks and throwing user-facing errors.

Suggested change
.action((options: Options) => {
const firebaseConfig = options.config;
const validatedConfig = firebaseConfig.src;
const kitConfigs = listKitConfigs(validatedConfig);
.action((options: Options) => {
const firebaseConfig = options.config;
if (!firebaseConfig) {
throw new FirebaseError(
"No active project configuration found. Please run this command from within a Firebase project directory."
);
}
const validatedConfig = firebaseConfig.src;
const kitConfigs = listKitConfigs(validatedConfig);
References
  1. Use strict null checks and handle undefined/null explicitly. Throw FirebaseError for expected, user-facing errors. (link)

if (kitConfigs.length < 1) {
logLabeledBullet("functions", `there are no kits in firebase.json`);
return;
}

const table = new Table({ head: ["Kit", "Instances"], style: { head: ["yellow"] } });
for (const kitConfig of kitConfigs) {
const instanceIds = Object.keys(kitConfig.instances);
table.push([kitConfig.kit, instanceIds.join(", ")]);
}
logger.info(table.toString());
});
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { CLIClient } from "../command";
import * as experiments from "../experiments";

type CommandRunner = ((...args: any[]) => Promise<any>) & { load: () => void };

Check warning on line 4 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type

Check warning on line 4 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unexpected any. Specify a different type

/**
* Loads all commands for our parser.
*/
export function load(client: CLIClient): CLIClient {
function loadCommand(name: string): CommandRunner {
const load = () => {

Check warning on line 11 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Missing return type on function
const { command: cmd } = require(`./${name}`);

Check warning on line 12 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Require statement not part of import statement

Check warning on line 12 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe assignment of an `any` value
cmd.register(client);

Check warning on line 13 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe call of an `any` typed value

Check warning on line 13 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .register on an `any` value
return cmd.runner();

Check warning on line 14 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe call of an `any` typed value

Check warning on line 14 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe member access .runner on an `any` value

Check warning on line 14 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (24)

Unsafe return of an `any` typed value
};

const runner = (async (...args: any[]) => {
Expand Down Expand Up @@ -201,6 +201,7 @@
if (experiments.isEnabled("kits")) {
client.functions.kits = {};
client.functions.kits.install = loadCommand("functions-kits-install");
client.functions.kits.list = loadCommand("functions-kits-list");
}
client.help = loadCommand("help");
client.hosting = {};
Expand Down
10 changes: 10 additions & 0 deletions src/functions/kits/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FirebaseConfig } from "../../firebaseConfig";
import { ValidatedKitSingle, normalizeAndValidate, isKitConfig } from "../projectConfig";

/**
* Extracts only the Kit configs from a parsed Firebase.json.
*/
export function listKitConfigs(config: FirebaseConfig): ValidatedKitSingle[] {
const normalized = normalizeAndValidate(config.functions);
return normalized.filter((s) => isKitConfig(s));
}
Comment on lines +7 to +10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

If firebase.json does not contain a functions section, config.functions will be undefined. Calling normalizeAndValidate(undefined) will throw a FirebaseError stating "No valid functions configuration detected in firebase.json". For a list command, it is better to gracefully return an empty array so the command can print "there are no kits in firebase.json" instead of throwing an error.

Suggested change
export function listKitConfigs(config: FirebaseConfig): ValidatedKitSingle[] {
const normalized = normalizeAndValidate(config.functions);
return normalized.filter((s) => isKitConfig(s));
}
export function listKitConfigs(config: FirebaseConfig): ValidatedKitSingle[] {
if (!config.functions) {
return [];
}
const normalized = normalizeAndValidate(config.functions);
return normalized.filter((s) => isKitConfig(s));
}
References
  1. Use strict null checks and handle undefined/null explicitly. (link)

Loading