Skip to content

Commit

Permalink
Upgrade logger to TypeScript (#3138)
Browse files Browse the repository at this point in the history
Rewrote our logger code in TypeScript.

Because we now need access to types in exported in the logger file, we export logger as a named export instead of assigning exports to logger (an anti-pattern anyway).

This change touches nearly every file because of the named import change. To reduce risk these changes have been made robotically:

```bash
git grep -e "require(.*logger" --name-only | xargs sed -i '' -e 's/var logger = require("\(.*logger\)")/const { logger } = require("\1")/g'
git grep -e "require(.*logger" --name-only | xargs sed -i '' -e 's/const logger = require("\(.*logger\)")/const { logger } = require("\1")/g'
git grep -e "import logger = require(.*logger" --name-only | xargs sed -i '' -e 's/logger = require("\(.*logger\)")/{ logger } from "\1"/g'
git grep -e "import \* as logger" --name-only | xargs sed -i '' -e 's/import \* as logger/import { logger }/g'
```

Other changes like `import { warn } from 'logger'` were touched up manually

Additionally, there were configurations in our logger that had been removed from the winston spec several versions ago (Particularly in our emulator code). TypeScirpt caught these errors and I upgraded to the new version of these APIs.
  • Loading branch information
inlined authored Mar 12, 2021
1 parent d14cd4c commit 1633f4f
Show file tree
Hide file tree
Showing 150 changed files with 276 additions and 207 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ The Firebase CLI has a central logger available in `src/logger`. You should
never use `console.log()` when displaying output to the user.

```typescript
import * as logger from "../logger";
import { logger } from "../logger";

logger.info("This text will be displayed to the end user.");
logger.debug("This text will only show up in firebase-debug.log or running with --debug.");
Expand Down
20 changes: 13 additions & 7 deletions scripts/emulator-tests/functionsEmulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import { EmulatedTriggerType } from "../../src/emulator/functionsEmulatorShared"
import { FunctionsEmulator, InvokeRuntimeOpts } from "../../src/emulator/functionsEmulator";
import { RuntimeWorker } from "../../src/emulator/functionsRuntimeWorker";
import { TIMEOUT_LONG, MODULE_ROOT } from "./fixtures";
import * as logger from "../../src/logger";
import { logger } from "../../src/logger";
import * as winston from "winston";
import * as logform from "logform";

if ((process.env.DEBUG || "").toLowerCase().indexOf("spec") >= 0) {
// tslint:disable-next-line:no-var-requires
logger.add(require("winston").transports.Console, {
level: "debug",
showLevel: false,
colorize: true,
});
const dropLogLevels = (info: logform.TransformableInfo) => info.message;
logger.add(
new winston.transports.Console({
level: "debug",
format: logform.format.combine(
logform.format.colorize(),
logform.format.printf(dropLogLevels)
),
})
);
}

const functionsEmulator = new FunctionsEmulator({
Expand Down
2 changes: 1 addition & 1 deletion src/accountImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var clc = require("cli-color");
var _ = require("lodash");

var api = require("./api");
var logger = require("./logger");
const { logger } = require("./logger");
var utils = require("./utils");
var { FirebaseError } = require("./error");

Expand Down
2 changes: 1 addition & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var url = require("url");

var { Constants } = require("./emulator/constants");
var { FirebaseError } = require("./error");
var logger = require("./logger");
const { logger } = require("./logger");
var responseToError = require("./responseToError");
var scopes = require("./scopes");
var utils = require("./utils");
Expand Down
2 changes: 1 addition & 1 deletion src/apiv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import util from "util";

import * as auth from "./auth";
import { FirebaseError } from "./error";
import * as logger from "./logger";
import { logger } from "./logger";
import * as responseToError from "./responseToError";

// Using import would require resolveJsonModule, which seems to break the
Expand Down
2 changes: 1 addition & 1 deletion src/appdistribution/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs-extra";
import { FirebaseError } from "../error";
import * as crypto from "crypto";
import { AppDistributionApp } from "./client";
import * as logger from "../logger";
import { logger } from "../logger";
import * as pathUtil from "path";

export enum DistributionFileType {
Expand Down
2 changes: 1 addition & 1 deletion src/archiveDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const tmp = require("tmp");
const { listFiles } = require("./listFiles");
const { FirebaseError } = require("./error");
const fsAsync = require("./fsAsync");
const logger = require("./logger");
const { logger } = require("./logger");
const utils = require("./utils");

/**
Expand Down
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as util from "util";
import * as api from "./api";
import { configstore } from "./configstore";
import { FirebaseError } from "./error";
import * as logger from "./logger";
import { logger } from "./logger";
import { prompt } from "./prompt";
import * as scopes from "./scopes";

Expand Down
2 changes: 1 addition & 1 deletion src/bin/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const client = require("..");
const errorOut = require("../errorOut").errorOut;
const winston = require("winston");
const { SPLAT } = require("triple-beam");
const logger = require("../logger");
const { logger } = require("../logger");
const fs = require("fs");
const fsutils = require("../fsutils");
const path = require("path");
Expand Down
2 changes: 1 addition & 1 deletion src/checkFirebaseSDKVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as semver from "semver";
import * as spawn from "cross-spawn";

import * as utils from "./utils";
import * as logger from "./logger";
import { logger } from "./logger";

interface NpmListResult {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/apps-android-sha-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Command } from "../command";
import * as getProjectId from "../getProjectId";
import { listAppAndroidSha, AppAndroidShaData } from "../management/apps";
import { requireAuth } from "../requireAuth";
import * as logger from "../logger";
import { logger } from "../logger";
import { promiseWithSpinner } from "../utils";

function logCertificatesList(certificates: AppAndroidShaData[]): void {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/apps-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "../management/apps";
import { prompt, promptOnce, Question } from "../prompt";
import { requireAuth } from "../requireAuth";
import * as logger from "../logger";
import { logger } from "../logger";

const DISPLAY_NAME_QUESTION: Question = {
type: "input",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/apps-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Command } from "../command";
import * as getProjectId from "../getProjectId";
import { AppMetadata, AppPlatform, getAppPlatform, listFirebaseApps } from "../management/apps";
import { requireAuth } from "../requireAuth";
import * as logger from "../logger";
import { logger } from "../logger";

const NOT_SPECIFIED = clc.yellow("[Not specified]");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/apps-sdkconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as getProjectId from "../getProjectId";
import { getOrPromptProject } from "../management/projects";
import { FirebaseError } from "../error";
import { requireAuth } from "../requireAuth";
import * as logger from "../logger";
import { logger } from "../logger";
import { promptOnce } from "../prompt";

async function selectAppInteractively(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/auth-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var os = require("os");
var { Command } = require("../command");
var accountExporter = require("../accountExporter");
var getProjectId = require("../getProjectId");
var logger = require("../logger");
const { logger } = require("../logger");
var { requirePermissions } = require("../requirePermissions");

var MAX_BATCH_SIZE = 1000;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/auth-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var _ = require("lodash");
var { Command } = require("../command");
var accountImporter = require("../accountImporter");
var getProjectId = require("../getProjectId");
var logger = require("../logger");
const { logger } = require("../logger");
var { requirePermissions } = require("../requirePermissions");
var utils = require("../utils");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { populateInstanceDetails } from "../management/database";
import { printNoticeIfEmulated } from "../emulator/commandUtils";
import { realtimeOriginOrEmulatorOrCustomUrl } from "../database/api";
import { requirePermissions } from "../requirePermissions";
import * as logger from "../logger";
import { logger } from "../logger";
import { requireDatabaseInstance } from "../requireDatabaseInstance";
import * as responseToError from "../responseToError";
import * as utils from "../utils";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-instances-create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from "../command";
import logger = require("../logger");
import { logger } from "../logger";
import { requirePermissions } from "../requirePermissions";
import { warnEmulatorNotSupported } from "../emulator/commandUtils";
import { Emulators } from "../emulator/types";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-instances-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Table = require("cli-table");
import * as clc from "cli-color";
import * as ora from "ora";

import logger = require("../logger");
import { logger } from "../logger";
import { requirePermissions } from "../requirePermissions";
import { getProjectNumber } from "../getProjectNumber";
import firedata = require("../gcp/firedata");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { printNoticeIfEmulated } from "../emulator/commandUtils";
import { realtimeOriginOrEmulatorOrCustomUrl } from "../database/api";
import { requirePermissions } from "../requirePermissions";
import { URL } from "url";
import * as logger from "../logger";
import { logger } from "../logger";
import { requireDatabaseInstance } from "../requireDatabaseInstance";
import * as utils from "../utils";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-rules-get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from "../command";
import * as logger from "../logger";
import { logger } from "../logger";
import { requireDatabaseInstance } from "../requireDatabaseInstance";
import { requirePermissions } from "../requirePermissions";
import * as metadata from "../database/metadata";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-rules-list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from "../command";
import * as logger from "../logger";
import { logger } from "../logger";
import { requireDatabaseInstance } from "../requireDatabaseInstance";
import { requirePermissions } from "../requirePermissions";
import * as metadata from "../database/metadata";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-rules-stage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from "../command";
import * as logger from "../logger";
import { logger } from "../logger";
import { requireDatabaseInstance } from "../requireDatabaseInstance";
import { requirePermissions } from "../requirePermissions";
import * as metadata from "../database/metadata";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { promptOnce } from "../prompt";
import { realtimeOriginOrEmulatorOrCustomUrl } from "../database/api";
import { requirePermissions } from "../requirePermissions";
import { URL } from "url";
import * as logger from "../logger";
import { logger } from "../logger";
import { requireDatabaseInstance } from "../requireDatabaseInstance";
import * as utils from "../utils";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/database-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { printNoticeIfEmulated } from "../emulator/commandUtils";
import { promptOnce } from "../prompt";
import { realtimeOriginOrEmulatorOrCustomUrl } from "../database/api";
import { requirePermissions } from "../requirePermissions";
import * as logger from "../logger";
import { logger } from "../logger";
import { requireDatabaseInstance } from "../requireDatabaseInstance";
import * as utils from "../utils";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/emulators-start.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command } from "../command";
import * as controller from "../emulator/controller";
import * as commandUtils from "../emulator/commandUtils";
import * as logger from "../logger";
import { logger } from "../logger";
import { EmulatorRegistry } from "../emulator/registry";
import { Emulators, EMULATORS_SUPPORTED_BY_UI } from "../emulator/types";
import * as clc from "cli-color";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ext-configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { logPrefix } from "../extensions/extensionsHelper";
import * as paramHelper from "../extensions/paramHelper";
import { requirePermissions } from "../requirePermissions";
import * as utils from "../utils";
import * as logger from "../logger";
import { logger } from "../logger";

marked.setOptions({
renderer: new TerminalRenderer(),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ext-dev-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Command } from "../command";
import * as Config from "../config";
import { FirebaseError } from "../error";
import { promptOnce } from "../prompt";
import * as logger from "../logger";
import { logger } from "../logger";
import * as npmDependencies from "../init/features/functions/npm-dependencies";
marked.setOptions({
renderer: new TerminalRenderer(),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ext-dev-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FirebaseError } from "../error";
import * as utils from "../utils";
import * as extensionsUtils from "../extensions/utils";
import { listExtensions } from "../extensions/extensionsApi";
import * as logger from "../logger";
import { logger } from "../logger";
import { requireAuth } from "../requireAuth";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ext-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { resolveRegistryEntry, resolveSourceUrl } from "../extensions/resolveSou
import * as extensionsApi from "../extensions/extensionsApi";
import { ensureExtensionsApiEnabled, logPrefix } from "../extensions/extensionsHelper";
import { isLocalExtension, getLocalExtensionSpec } from "../extensions/localHelper";
import * as logger from "../logger";
import { logger } from "../logger";
import { requirePermissions } from "../requirePermissions";
import * as utils from "../utils";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/ext-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
import { getRandomString } from "../extensions/utils";
import { requirePermissions } from "../requirePermissions";
import * as utils from "../utils";
import * as logger from "../logger";
import { logger } from "../logger";
import { promptOnce } from "../prompt";
import { previews } from "../previews";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/ext-sources-create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { checkMinRequiredVersion } from "../checkMinRequiredVersion";
import { Command } from "../command";
import * as getProjectId from "../getProjectId";
import * as logger from "../logger";
import { logger } from "../logger";
import {
createSourceFromLocation,
ensureExtensionsApiEnabled,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ext-uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { promptOnce } from "../prompt";
import { requirePermissions } from "../requirePermissions";
import * as utils from "../utils";
import * as logger from "../logger";
import { logger } from "../logger";

marked.setOptions({
renderer: new TerminalRenderer(),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as getProjectId from "../getProjectId";
import { logPrefix } from "../extensions/extensionsHelper";
import { listExtensions } from "../extensions/listExtensions";
import { requirePermissions } from "../requirePermissions";
import * as logger from "../logger";
import { logger } from "../logger";
import * as utils from "../utils";
import { CommanderStatic } from "commander";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/firestore-indexes-list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command } from "../command";
import * as clc from "cli-color";
import * as fsi from "../firestore/indexes";
import * as logger from "../logger";
import { logger } from "../logger";
import { requirePermissions } from "../requirePermissions";
import { Emulators } from "../emulator/types";
import { warnEmulatorNotSupported } from "../emulator/commandUtils";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions-config-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var functionsConfigClone = require("../functionsConfigClone");
var getProjectId = require("../getProjectId");
var { requirePermissions } = require("../requirePermissions");
var utils = require("../utils");
var logger = require("../logger");
const { logger } = require("../logger");

module.exports = new Command("functions:config:clone")
.description("clone environment config from another project")
Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions-config-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var _ = require("lodash");
var { Command } = require("../command");
var getProjectId = require("../getProjectId");
var logger = require("../logger");
const { logger } = require("../logger");
var { requirePermissions } = require("../requirePermissions");
var functionsConfig = require("../functionsConfig");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions-config-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var getProjectId = require("../getProjectId");
var { requirePermissions } = require("../requirePermissions");
var runtimeconfig = require("../gcp/runtimeconfig");
var functionsConfig = require("../functionsConfig");
var logger = require("../logger");
const { logger } = require("../logger");

module.exports = new Command("functions:config:legacy")
.description("get legacy functions config variables")
Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions-config-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var clc = require("cli-color");
var { Command } = require("../command");
var getProjectId = require("../getProjectId");
var { requirePermissions } = require("../requirePermissions");
var logger = require("../logger");
const { logger } = require("../logger");
var utils = require("../utils");
var functionsConfig = require("../functionsConfig");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions-config-unset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var clc = require("cli-color");
var { Command } = require("../command");
var functionsConfig = require("../functionsConfig");
var getProjectId = require("../getProjectId");
var logger = require("../logger");
const { logger } = require("../logger");
var { requirePermissions } = require("../requirePermissions");
var utils = require("../utils");
var runtimeconfig = require("../gcp/runtimeconfig");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Command } from "../command";
import { FirebaseError } from "../error";
import * as gcp from "../gcp";
import * as getProjectId from "../getProjectId";
import * as logger from "../logger";
import { logger } from "../logger";
import { requirePermissions } from "../requirePermissions";

module.exports = new Command("functions:log")
Expand Down
2 changes: 1 addition & 1 deletion src/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var { Command } = require("../command");

var clc = require("cli-color");
var logger = require("../logger");
const { logger } = require("../logger");
var utils = require("../utils");

module.exports = new Command("help [command]")
Expand Down
2 changes: 1 addition & 1 deletion src/commands/hosting-channel-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { logLabeledSuccess, datetimeString, logLabeledWarning, consoleUrl } from
import { promptOnce } from "../prompt";
import { requirePermissions } from "../requirePermissions";
import * as getProjectId from "../getProjectId";
import * as logger from "../logger";
import { logger } from "../logger";
import * as requireConfig from "../requireConfig";
import * as marked from "marked";
import { requireHostingSite } from "../requireHostingSite";
Expand Down
Loading

0 comments on commit 1633f4f

Please sign in to comment.