[go: nahoru, domu]

Skip to content

Commit

Permalink
Improves "emulators:start" logs (firebase#2219)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeisgoat committed May 14, 2020
1 parent 06580ce commit 66228c0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Adds new API commands that handle list/create/delete operations on the Android SHA certificate hashes `apps:android:sha:list`, `apps:android:sha:create`, and `apps:android:sha:delete`.
- Fixes an issue where the CLI did not assume admin privileges when performing Firestore / RTDB emulator operations.
- Fixes an issue where the functions and hosting emulators would crash when not properly initialized (#2112).
- Improves logs when running `emulators:start`.
- Fixes an issue where `use` would allow an invalid project identifier.
- Fixes an issue where custom options passed to `admin.initializeApp()` in the functions emulator were improperly augmented.
- Changes `firebasemods.*.*` IAM permission checks to `firebaseextensions.*.*`
Expand Down
2 changes: 1 addition & 1 deletion scripts/integration-helpers/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const FIREBASE_PROJECT_ZONE = "us-central1";
const RTDB_FUNCTION_LOG = "========== RTDB FUNCTION ==========";
const FIRESTORE_FUNCTION_LOG = "========== FIRESTORE FUNCTION ==========";
const PUBSUB_FUNCTION_LOG = "========== PUBSUB FUNCTION ==========";
const ALL_EMULATORS_STARTED_LOG = "All emulators started, it is now safe to connect.";
const ALL_EMULATORS_STARTED_LOG = "is now safe to connect";

interface ConnectionInfo {
host: string;
Expand Down
2 changes: 1 addition & 1 deletion scripts/triggers-end-to-end-tests/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FrameworkOptions, TriggerEndToEndTest } from "../integration-helpers/fr

const FIREBASE_PROJECT = process.env.FBTOOLS_TARGET_PROJECT || "";

const ALL_EMULATORS_STARTED_LOG = "All emulators started, it is now safe to connect.";
const ALL_EMULATORS_STARTED_LOG = "is now safe to connect";

/*
* Various delays that are needed because this test spawns
Expand Down
77 changes: 69 additions & 8 deletions src/commands/emulators-start.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Command } from "../command";
import * as controller from "../emulator/controller";
import * as commandUtils from "../emulator/commandUtils";
import * as utils from "../utils";
import { EmulatorLogger } from "../emulator/emulatorLogger";
import { Emulators } from "../emulator/types";
import { FirebaseError } from "../error";
import * as logger from "../logger";
import { EmulatorRegistry } from "../emulator/registry";
import { Emulators, EMULATORS_SUPPORTED_BY_GUI } from "../emulator/types";
import * as clc from "cli-color";

const Table = require("cli-table");

function stylizeLink(url: String) {
return clc.underline(clc.bold(url));
}

module.exports = new Command("emulators:start")
.before(commandUtils.beforeEmulatorCommand)
Expand All @@ -22,12 +28,67 @@ module.exports = new Command("emulators:start")
throw e;
}

EmulatorLogger.forEmulator(Emulators.HUB).logLabeled(
"SUCCESS",
"emulators",
"All emulators started, it is now safe to connect."
const guiInfo = EmulatorRegistry.getInfo(Emulators.GUI);
const guiUrl = `http://${guiInfo?.host}:${guiInfo?.port}`;
const head = ["Emulator", "Host:Port"];

if (guiInfo) {
head.push("View in UI");
}

const successMessageTable = new Table();
successMessageTable.push([
`${clc.green("✔")} All emulators ready! ` +
(guiInfo
? `View status and logs at ${stylizeLink(guiUrl)}`
: `It is now safe to connect your apps.`),
]);

const emulatorsTable = new Table({
head: head,
style: {
head: ["yellow"],
},
});

emulatorsTable.push(
...controller
.filterEmulatorTargets(options)
.map((emulator) => {
const instance = EmulatorRegistry.get(emulator);
const info = EmulatorRegistry.getInfo(emulator);
const emulatorName = emulator.slice(0, 1).toUpperCase() + emulator.slice(1);
const isSupportedByGUI = EMULATORS_SUPPORTED_BY_GUI.includes(emulator);

if (!info) {
return [emulatorName, "Failed to initialize (see above)", "", ""];
}

return [
emulatorName,
`${info?.host}:${info?.port}`,
isSupportedByGUI && guiInfo
? stylizeLink(`${guiUrl}/${emulator}`)
: clc.blackBright("n/a"),
];
})
.map((col) => col.slice(0, head.length))
.filter((v) => v)
);

logger.info(`\n${successMessageTable}
${emulatorsTable}
${clc.blackBright(" Other reserved ports:")} ${EmulatorRegistry.getInfo(Emulators.HUB)?.port}
Issues? Report them at ${stylizeLink(
"https://github.com/firebase/firebase-tools/issues"
)} and attach the *-debug.log files.
`);

// Add this line above once connect page is implemented
// It is now safe to connect your app. Instructions: http://${guiInfo?.host}:${guiInfo?.port}/connect

// Hang until explicitly killed
await killSignalPromise;
});
16 changes: 0 additions & 16 deletions src/emulator/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,6 @@ export async function startAll(options: any, noGui: boolean = false): Promise<vo

const firestoreEmulator = new FirestoreEmulator(args);
await startEmulator(firestoreEmulator);

firestoreLogger.logLabeled(
"BULLET",
"firestore",
`For testing set ${clc.bold(
`${Constants.FIRESTORE_EMULATOR_HOST}=${firestoreAddr.host}:${firestoreAddr.port}`
)}`
);
}

if (shouldStart(options, Emulators.DATABASE)) {
Expand Down Expand Up @@ -389,14 +381,6 @@ export async function startAll(options: any, noGui: boolean = false): Promise<vo

const databaseEmulator = new DatabaseEmulator(args);
await startEmulator(databaseEmulator);

databaseLogger.logLabeled(
"BULLET",
"database",
`For testing set ${clc.bold(
`${Constants.FIREBASE_DATABASE_EMULATOR_HOST}=${databaseAddr.host}:${databaseAddr.port}`
)}`
);
}

if (shouldStart(options, Emulators.HOSTING)) {
Expand Down
6 changes: 3 additions & 3 deletions src/emulator/downloadableEmulators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function getExecPath(name: DownloadableEmulators): string {
return details.binaryPath || details.downloadPath;
}

function _getLogFileName(name: string): string {
export function getLogFileName(name: string): string {
return `${name}-debug.log`;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ async function _runBinary(
): Promise<void> {
return new Promise((resolve) => {
const logger = EmulatorLogger.forEmulator(emulator.name);
emulator.stdout = fs.createWriteStream(_getLogFileName(emulator.name));
emulator.stdout = fs.createWriteStream(getLogFileName(emulator.name));
try {
emulator.instance = childProcess.spawn(command.binary, command.args, {
env: { ...process.env, ...extraEnv },
Expand Down Expand Up @@ -246,7 +246,7 @@ async function _runBinary(
logger.logLabeled(
"BULLET",
emulator.name,
`${description} logging to ${clc.bold(_getLogFileName(emulator.name))}`
`${description} logging to ${clc.bold(getLogFileName(emulator.name))}`
);

emulator.instance.stdout.on("data", (data) => {
Expand Down
6 changes: 0 additions & 6 deletions src/emulator/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ export class EmulatorRegistry {
const info = instance.getInfo();
await controller.waitForPortClosed(info.port, info.host);
this.set(instance.getName(), instance);

EmulatorLogger.forEmulator(instance.getName()).logLabeled(
"SUCCESS",
instance.getName(),
`${description} started at ${clc.bold.underline(`http://${info.host}:${info.port}`)}`
);
}

static async stop(name: Emulators): Promise<void> {
Expand Down

0 comments on commit 66228c0

Please sign in to comment.