Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 14 additions & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as clc from "colorette";
import { markedTerminal } from "marked-terminal";
import { marked } from "marked";
marked.use(markedTerminal() as any);

Check warning on line 5 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 5 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `MarkedExtension`

import { CommanderStatic } from "commander";
import * as fs from "node:fs";
Expand All @@ -18,8 +18,8 @@
import { enableExperimentsFromCliEnvVariable } from "../experiments";
import { fetchMOTD } from "../fetchMOTD";

export function cli(pkg: any) {

Check warning on line 21 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 21 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment

Check warning on line 21 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
const updateNotifier = updateNotifierPkg({ pkg });

Check warning on line 22 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

const args = process.argv.slice(2);
let cmd: CommanderStatic;
Expand All @@ -34,7 +34,7 @@

logger.debug("-".repeat(70));
logger.debug("Command: ", process.argv.join(" "));
logger.debug("CLI Version: ", pkg.version);

Check warning on line 37 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .version on an `any` value
logger.debug("Platform: ", process.platform);
logger.debug("Node Version: ", process.version);
logger.debug("Time: ", new Date().toString());
Expand All @@ -54,7 +54,7 @@
}

if (code > 0 && process.stdout.isTTY) {
const lastError = configstore.get("lastError") || 0;

Check warning on line 57 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const timestamp = Date.now();
if (lastError > timestamp - 120000) {
let help;
Expand Down Expand Up @@ -84,7 +84,7 @@
const updateMessage =
`Update available ${clc.gray("{currentVersion}")} → ${clc.green("{latestVersion}")}\n` +
`To update to the latest version using ${installMethod}, run\n${clc.cyan(updateCommand)}\n` +
`For other CLI management options, visit the ${marked(

Check warning on line 87 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "string | Promise<string>" of template literal expression
"[CLI documentation](https://firebase.google.com/docs/cli#update-cli)",
)}`;
// `defer: true` would interfere with commands that perform tasks (emulators etc.)
Expand All @@ -109,7 +109,20 @@

if (!handlePreviewToggles(args)) {
// determine if there are any arguments. if not, display help
if (!args.length) {
if (!args.length || args[0] === "help") {
const seen = new Set();
const loadAll = (obj: any) => {

Check warning on line 114 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
if (seen.has(obj)) return;
seen.add(obj);
for (const [key, value] of Object.entries(obj)) {
if (typeof value === "function" && (value as any).load) {
(value as any).load();
} else if (typeof value === "object" && value !== null && !Array.isArray(value) && key !== "cli") {

Check failure on line 120 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `typeof·value·===·"object"·&&·value·!==·null·&&·!Array.isArray(value)·&&·key·!==·"cli"` with `⏎············typeof·value·===·"object"·&&⏎············value·!==·null·&&⏎············!Array.isArray(value)·&&⏎············key·!==·"cli"⏎··········`

Check failure on line 120 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `typeof·value·===·"object"·&&·value·!==·null·&&·!Array.isArray(value)·&&·key·!==·"cli"` with `⏎············typeof·value·===·"object"·&&⏎············value·!==·null·&&⏎············!Array.isArray(value)·&&⏎············key·!==·"cli"⏎··········`

Check failure on line 120 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / unit (22)

Replace `typeof·value·===·"object"·&&·value·!==·null·&&·!Array.isArray(value)·&&·key·!==·"cli"` with `⏎············typeof·value·===·"object"·&&⏎············value·!==·null·&&⏎············!Array.isArray(value)·&&⏎············key·!==·"cli"⏎··········`

Check failure on line 120 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `typeof·value·===·"object"·&&·value·!==·null·&&·!Array.isArray(value)·&&·key·!==·"cli"` with `⏎············typeof·value·===·"object"·&&⏎············value·!==·null·&&⏎············!Array.isArray(value)·&&⏎············key·!==·"cli"⏎··········`
loadAll(value);

Check failure on line 121 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Delete `·`

Check failure on line 121 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Delete `·`

Check failure on line 121 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / unit (22)

Delete `·`

Check failure on line 121 in src/bin/cli.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Delete `·`
}
}
};
loadAll(client);
client.cli.help();
} else {
cmd = client.cli.parse(process.argv);
Expand Down
34 changes: 25 additions & 9 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import * as experiments from "../experiments";
import { Command } from "../command";

Check failure on line 2 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'Command' is defined but never used

Check failure on line 2 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'Command' is defined but never used

Check failure on line 2 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (22)

'Command' is defined but never used

Check failure on line 2 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'Command' is defined but never used

/**
* Loads all commands for our parser.
*/
export function load(client: any): any {
function loadCommand(name: string) {
const t0 = process.hrtime.bigint();

Check failure on line 9 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (24)

't0' is assigned a value but never used

Check failure on line 9 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (24)

't0' is assigned a value but never used

Check failure on line 9 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (22)

't0' is assigned a value but never used

Check failure on line 9 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

't0' is assigned a value but never used
const { command: cmd } = require(`./${name}`);
cmd.register(client);
const t1 = process.hrtime.bigint();
const diffMS = (t1 - t0) / BigInt(1e6);
if (diffMS > 75) {
// NOTE: logger.debug doesn't work since it's not loaded yet. Comment out below to debug.
// console.error(`Loading ${name} took ${diffMS}ms`);
}

return cmd.runner();
const load = () => {
const { command: cmd } = require(`./${name}`);
cmd.register(client);
return cmd.runner();
};

const runner = async (...args: any[]) => {
const run = load();
return run(...args);
};

// Store the load function on the runner so we can trigger it without running.
(runner as any).load = () => {
const tStart = process.hrtime.bigint();
require(`./${name}`).command.register(client);
const tEnd = process.hrtime.bigint();
const diff = (tEnd - tStart) / BigInt(1e6);
if (diff > 75) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to keep this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably not, cleaned up

// console.error(`Loading ${name} took ${diff}ms`);

Check failure on line 29 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Delete `·`

Check failure on line 29 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Delete `·`

Check failure on line 29 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / unit (22)

Delete `·`

Check failure on line 29 in src/commands/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Delete `·`
}
};

return runner;
}

const t0 = process.hrtime.bigint();
Expand Down
25 changes: 24 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,32 @@ const RENAMED_COMMANDS: Record<string, string> = {

// Default handler, this is called when no other command action matches.
program.action((_, args) => {
const cmd = args[0];
const keys = cmd.split(":");
let obj = client;
let hit = true;
for (const key of keys) {
if (!obj || typeof obj !== "object") {
hit = false;
break;
}
const nextKey = Object.keys(obj).find((k) => k.toLowerCase() === key.toLowerCase());
if (!nextKey) {
hit = false;
break;
}
obj = (obj as any)[nextKey];
}

if (hit && typeof obj === "function" && (obj as any).load) {
(obj as any).load();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(client.cli as any).parse(process.argv);
return;
}

useConsoleLoggers();

const cmd = args[0];
logger.error(clc.bold(clc.red("Error:")), clc.bold(cmd), "is not a Firebase command");

if (RENAMED_COMMANDS[cmd]) {
Expand Down
Loading