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

Added doctor command #8302

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
99 changes: 99 additions & 0 deletions src/commands/doctor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { promisify } from "node:util";
import { Command } from "../command";
import * as childProcess from "node:child_process";
import * as fs from "node:fs/promises";
import { logBullet, logWarning } from "../utils";
interface Dependencies {
[name: string]: {
version: string;
resolved: string;
overridden: boolean;
dependencies: Dependencies;
problems: string[];
invalid?: string;
};
}
interface NpmOutput {
version: string;
name: string;
problems: string[];
dependencies: Dependencies;
}

const exec = promisify(childProcess.exec);
const DESCRIPTION = `Automatically detect potential issues related to your web app's configuration`;
interface StdOutErr {
stdout: string;
}
export const command = new Command("doctor").description(DESCRIPTION).action(doctorAction);
/**
* doctor command action
*/
export async function doctorAction(): Promise<void> {
// Note: This command requires nodejs installation and will not work with firepit users that don't have node installed.
try {
await fs.access("node_modules", fs.constants.F_OK);
} catch {
console.log('Install your dependencies before running "firebase doctor"');
return;
}
try {
await fs.access("package.json", fs.constants.F_OK);
} catch {
console.log('Run "firebase doctor" from a path with a valid package.json');
return;
}

try {
const { stdout } = await exec("npm ls firebase --json");
if (parse(JSON.parse(stdout) as NpmOutput)) {
console.log("No problems found in your configuration.");
}
} catch (e) {
if ("stdout" in (e as Object)) {
const parsedOutput: NpmOutput = JSON.parse((e as StdOutErr)?.stdout);
if (parse(parsedOutput as NpmOutput)) {
console.log("No problems found in your configuration.");
}
}
}
}
function parse(parsedOutput: NpmOutput) {
if (!parsedOutput.dependencies || Object.keys(parsedOutput).length === 0) {
return true;
}
const versionsOfFirebase: string[] = [];
const overallProblems = Object.keys(parsedOutput.dependencies)
.map((key: string) => {
const curDep = parsedOutput.dependencies[key];
if (curDep.dependencies && curDep.dependencies["firebase"]) {
const firebaseDep = curDep.dependencies["firebase"];
if (!versionsOfFirebase.includes(firebaseDep.version)) {
versionsOfFirebase.push(firebaseDep.version);
}
}
if (key === "firebase") {
if (!versionsOfFirebase.includes(curDep.version)) {
versionsOfFirebase.push(curDep.version);
}
}
return {
dep: key,
problems: parsedOutput.dependencies[key].problems,
invalid: parsedOutput.dependencies[key].invalid,
};
})
.filter((res) => res.problems);
let success = true;
if (overallProblems.length > 0) {
logWarning("Issues detected with your firebase app:");
success = false;
}
overallProblems.map((problem) => logBullet(`${problem.dep} is invalid: ${problem.invalid}`));
if (versionsOfFirebase.length > 1) {
logWarning("You have multiple versions of firebase installed:");
versionsOfFirebase.map((version) => logBullet(version));
success = false;
}
return success;
}
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
/**
* Loads all commands for our parser.
*/
export function load(client: any): any {

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

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
function loadCommand(name: string) {

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

View workflow job for this annotation

GitHub Actions / lint (20)

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

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

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

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

View workflow job for this annotation

GitHub Actions / lint (20)

Require statement not part of import statement
cmd.register(client);

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

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .register on an `any` value
const t1 = process.hrtime.bigint();
const diffMS = (t1 - t0) / BigInt(1e6);
if (diffMS > 75) {
Expand Down Expand Up @@ -72,6 +72,7 @@
client.database.settings.set = loadCommand("database-settings-set");
client.database.update = loadCommand("database-update");
client.deploy = loadCommand("deploy");
client.doctor = loadCommand("doctor");
client.emulators = {};
client.emulators.exec = loadCommand("emulators-exec");
client.emulators.export = loadCommand("emulators-export");
Expand Down
Loading