Skip to content

Commit

Permalink
Moving more code into shared (#571)
Browse files Browse the repository at this point in the history
Continuation of [PRO-720](https://linear.app/replay/issue/PRO-720); follows up to #562

Moving much of the `replayio` utility-type code into the new `shared` package so that we can use those Node APIs for querying the list of recording and uploading or deleting them.

Most of this was rote but I did make a couple of changes worth calling out explicitly:
- I changed the value returned by `fetchAuthIdsFromGraphQL` from `{ userId: string | null; workspaceId: string | null }` to `{ id: string; type: "user" | "workspace" }` and updated the callers; this was easier to work with (from a types perspective)
  - Grafana still gets `userId` _or_ `workspaceId` though, just not both
- `Logger` is now eagerly instantiated; the `initialize()` method will accept a name and version and pass them along to the Grafana instance. Logging before initialization (or in Jest tests) will be local-only.
  - Note that a version parameter is also supported now (so we can associate Grafana logs with specific releases)
  - Both name and version should probably be imported and passed directly from `package.json` (which is what I am currently doing for Puppeteer and replayio)
  - Note that I've replaced the previous "replayio" name prefix with whatever the passed package name is
- Lots of `debug(...args)` logging has been changed to `logger.debug("...", {...args})`
- I've moved `mixpanel` and `launch-darkly` code over to `shared` too, even though (for now) `replayio` is the only thing using this stuff
- I also tidied up the `getRecordings` method to not create so many throw-away arrays; (I didn't just do this for performance, but TypeScript was complaining about the return array containing recordings _and_ `undefined` with the previous code structure)

## Next steps
- Remove the `test-utils` dependency on `@replayio/replay` (and delete `@replayio/replay`)
- Update `test-utils` to be a peer dependency so that it will get inlined as well (to resolve ambiguous logger ownership)
- Audit `logger.debug` entries and decide which ones should be `info`, `log`, or even `error`
- Address any added `TODO [PRO-720]` comments
  • Loading branch information
bvaughn authored Jun 28, 2024
1 parent 6ce3100 commit 33656ea
Show file tree
Hide file tree
Showing 175 changed files with 1,178 additions and 951 deletions.
23 changes: 11 additions & 12 deletions packages/cypress/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/// <reference types="cypress" />

import { RecordingEntry } from "@replay-cli/shared/recording/types";
import { getRuntimePath } from "@replay-cli/shared/runtime/getRuntimePath";
import { initMetadataFile, warn } from "@replayio/test-utils";
import { RecordingEntry, initMetadataFile, warn } from "@replayio/test-utils";
import chalk from "chalk";
import dbg from "debug";
import path from "path";
Expand All @@ -16,6 +15,16 @@ import { createServer } from "./server";
import type { StepEvent } from "./support";

export type { PluginOptions } from "./reporter";
export {
getMetadataFilePath,
onAfterRun,
onAfterSpec,
onBeforeBrowserLaunch,
onBeforeRun,
onBeforeSpec,
plugin,
cypressOnWrapper as wrapOn,
};

const debug = dbg("replay:cypress:plugin");
const debugTask = debug.extend("task");
Expand Down Expand Up @@ -342,13 +351,3 @@ export function getCypressReporter() {
}

export default plugin;
export {
getMetadataFilePath,
onAfterRun,
onAfterSpec,
onBeforeBrowserLaunch,
onBeforeRun,
onBeforeSpec,
plugin,
cypressOnWrapper as wrapOn,
};
2 changes: 1 addition & 1 deletion packages/cypress/src/junit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RecordingEntry } from "@replay-cli/shared/recording/types";
import { warn } from "@replayio/test-utils";
import { RecordingEntry } from "@replayio/test-utils";
import dbg from "debug";
import fs, { readFileSync, writeFileSync } from "fs";
import path from "path";
Expand Down
1 change: 1 addition & 0 deletions packages/playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@replay-cli/pkg-build": "workspace:^",
"@replay-cli/shared": "workspace:^",
"@replay-cli/tsconfig": "workspace:^",
"@replayio/test-utils": "workspace:^",
"@types/debug": "^4.1.8",
"@types/node": "^20.11.27",
"@types/stack-utils": "^2.0.3",
Expand Down
7 changes: 3 additions & 4 deletions packages/puppeteer/src/install.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { initLogger, logger } from "@replay-cli/shared/logger";
import { installLatestRuntimeRelease } from "@replay-cli/shared/runtime/installLatestRuntimeRelease";
import { name, version } from "../package.json";

async function install() {
initLogger("puppeteer");
export default async function install() {
initLogger(name, version);
try {
await installLatestRuntimeRelease();
} finally {
await logger.close();
}
}

export default install;
3 changes: 2 additions & 1 deletion packages/puppeteer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "@replay-cli/tsconfig/base.json",
"compilerOptions": {
"lib": ["dom"]
"lib": ["dom"],
"resolveJsonModule": true
},
"include": ["src/**/*.ts"],
"references": [
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"homepage": "https://github.com/replayio/replay-cli/blob/main/packages/replay/README.md",
"dependencies": {
"@replay-cli/shared": "workspace:^",
"@replayio/sourcemap-upload": "workspace:^",
"@types/semver": "^7.5.6",
"commander": "^12.0.0",
Expand All @@ -55,7 +56,6 @@
},
"devDependencies": {
"@replay-cli/pkg-build": "workspace:^",
"@replay-cli/shared": "workspace:^",
"@replay-cli/tsconfig": "workspace:^",
"@types/debug": "^4.1.7",
"@types/jest": "^28.1.5",
Expand Down
2 changes: 2 additions & 0 deletions packages/replayio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"table": "^6.8.2",
"undici": "^5.28.4",
"uuid": "^8.3.2",
"winston": "^3.13.0",
"winston-loki": "^6.1.2",
"ws": "^7.5.0"
},
"devDependencies": {
Expand Down
9 changes: 8 additions & 1 deletion packages/replayio/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { setUserAgent } from "@replay-cli/shared/userAgent";
import { name, version } from "../package.json";
import { finalizeCommander } from "./utils/commander/finalizeCommander";
import { exitProcess } from "./utils/exitProcess";

// Commands self-register with "commander"
import "./commands/info";
Expand All @@ -12,6 +14,11 @@ import "./commands/remove";
import "./commands/update";
import "./commands/upload";
import "./commands/upload-source-maps";
import { initLogger } from "@replay-cli/shared/logger";

initLogger(name, version);

setUserAgent(`${name}/${version}`);

finalizeCommander();

Expand Down
6 changes: 3 additions & 3 deletions packages/replayio/src/commands/info.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { parseBuildId } from "@replay-cli/shared/runtime/parseBuildId";
import { highlight } from "@replay-cli/shared/theme";
import { name as packageName, version as packageVersion } from "../../package.json";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";
import { getCurrentRuntimeMetadata } from "../utils/initialization/getCurrentRuntimeMetadata";
import { parseBuildId } from "../utils/installation/parseBuildId";
import { highlight } from "../utils/theme";

registerCommand("info", {
checkForNpmUpdate: false,
Expand Down
6 changes: 3 additions & 3 deletions packages/replayio/src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { getRecordings } from "@replay-cli/shared/recording/getRecordings";
import { printRecordings } from "@replay-cli/shared/recording/printRecordings";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";
import { getRecordings } from "../utils/recordings/getRecordings";
import { printRecordings } from "../utils/recordings/printRecordings";

registerCommand("list")
.description("List all local recordings")
Expand Down
4 changes: 2 additions & 2 deletions packages/replayio/src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { registerCommand } from "../utils/commander/registerCommand";
import { checkAuthentication } from "../utils/initialization/checkAuthentication";
import { promptForAuthentication } from "../utils/initialization/promptForAuthentication";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";

registerCommand("login").description("Log into your Replay account (or register)").action(login);

Expand Down
8 changes: 4 additions & 4 deletions packages/replayio/src/commands/logout.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getAccessToken } from "../utils/authentication/getAccessToken";
import { logoutIfAuthenticated } from "../utils/authentication/logoutIfAuthenticated";
import { getAccessToken } from "@replay-cli/shared/authentication/getAccessToken";
import { logoutIfAuthenticated } from "@replay-cli/shared/authentication/logoutIfAuthenticated";
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { highlight } from "@replay-cli/shared/theme";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";
import { highlight } from "../utils/theme";

registerCommand("logout").description("Log out of your Replay account").action(logout);

Expand Down
2 changes: 1 addition & 1 deletion packages/replayio/src/commands/open.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { killBrowserIfRunning } from "../utils/browser/killBrowserIfRunning";
import { launchBrowser } from "../utils/browser/launchBrowser";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";

registerCommand("open", { checkForRuntimeUpdate: true, requireAuthentication: true })
.argument("[url]", `URL to open (default: "about:blank")`)
Expand Down
20 changes: 10 additions & 10 deletions packages/replayio/src/commands/record.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { ProcessError } from "@replay-cli/shared/ProcessError";
import { trackEvent } from "@replay-cli/shared/mixpanel/trackEvent";
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { canUpload } from "@replay-cli/shared/recording/canUpload";
import { getRecordings } from "@replay-cli/shared/recording/getRecordings";
import { printRecordings } from "@replay-cli/shared/recording/printRecordings";
import { selectRecordings } from "@replay-cli/shared/recording/selectRecordings";
import { LocalRecording } from "@replay-cli/shared/recording/types";
import { uploadRecordings } from "@replay-cli/shared/recording/upload/uploadRecordings";
import { dim, statusFailed } from "@replay-cli/shared/theme";
import debug from "debug";
import { v4 as uuid } from "uuid";
import { ProcessError } from "../utils/ProcessError";
import { logAsyncOperation } from "../utils/async/logAsyncOperation";
import { killBrowserIfRunning } from "../utils/browser/killBrowserIfRunning";
import { launchBrowser } from "../utils/browser/launchBrowser";
import { reportBrowserCrash } from "../utils/browser/reportBrowserCrash";
import { registerCommand } from "../utils/commander/registerCommand";
import { confirm } from "../utils/confirm";
import { exitProcess } from "../utils/exitProcess";
import { trackEvent } from "../utils/mixpanel/trackEvent";
import { canUpload } from "../utils/recordings/canUpload";
import { getRecordings } from "../utils/recordings/getRecordings";
import { printRecordings } from "../utils/recordings/printRecordings";
import { selectRecordings } from "../utils/recordings/selectRecordings";
import { LocalRecording } from "../utils/recordings/types";
import { uploadRecordings } from "../utils/recordings/upload/uploadRecordings";
import { dim, statusFailed } from "../utils/theme";

registerCommand("record", { checkForRuntimeUpdate: true, requireAuthentication: true })
.argument("[url]", `URL to open (default: "about:blank")`)
Expand Down
16 changes: 8 additions & 8 deletions packages/replayio/src/commands/remove.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { findRecordingsWithShortIds } from "@replay-cli/shared/recording/findRecordingsWithShortIds";
import { getRecordings } from "@replay-cli/shared/recording/getRecordings";
import { printRecordings } from "@replay-cli/shared/recording/printRecordings";
import { removeAllFromDisk, removeFromDisk } from "@replay-cli/shared/recording/removeFromDisk";
import { selectRecordings } from "@replay-cli/shared/recording/selectRecordings";
import { LocalRecording } from "@replay-cli/shared/recording/types";
import { dim } from "@replay-cli/shared/theme";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";
import { findRecordingsWithShortIds } from "../utils/recordings/findRecordingsWithShortIds";
import { getRecordings } from "../utils/recordings/getRecordings";
import { printRecordings } from "../utils/recordings/printRecordings";
import { removeAllFromDisk, removeFromDisk } from "../utils/recordings/removeFromDisk";
import { selectRecordings } from "../utils/recordings/selectRecordings";
import { LocalRecording } from "../utils/recordings/types";
import { dim } from "../utils/theme";

registerCommand("remove")
.argument("[ids...]", `Recording ids ${dim("(comma-separated)")}`, value => value.split(","))
Expand Down
4 changes: 2 additions & 2 deletions packages/replayio/src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { statusSuccess } from "@replay-cli/shared/theme";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";
import { checkForNpmUpdate } from "../utils/initialization/checkForNpmUpdate";
import { checkForRuntimeUpdate } from "../utils/initialization/checkForRuntimeUpdate";
import { promptForNpmUpdate } from "../utils/initialization/promptForNpmUpdate";
import { installLatestRelease } from "../utils/installation/installLatestRelease";
import { statusSuccess } from "../utils/theme";

registerCommand("update", {
checkForRuntimeUpdate: false,
Expand Down
8 changes: 4 additions & 4 deletions packages/replayio/src/commands/upload-source-maps.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getAccessToken } from "@replay-cli/shared/authentication/getAccessToken";
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { dim } from "@replay-cli/shared/theme";
import { uploadSourceMaps as uploadSourceMapsExternal } from "@replayio/sourcemap-upload";
import { replayApiServer } from "../config";
import { getAccessToken } from "../utils/authentication/getAccessToken";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";
import { dim } from "../utils/theme";
import { logPromise } from "../utils/async/logPromise";
import { registerCommand } from "../utils/commander/registerCommand";

registerCommand("upload-source-maps <paths...>", { requireAuthentication: true })
.description("Upload source-maps for a Workspace")
Expand Down
17 changes: 8 additions & 9 deletions packages/replayio/src/commands/upload.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";
import { canUpload } from "../utils/recordings/canUpload";
import { findRecordingsWithShortIds } from "../utils/recordings/findRecordingsWithShortIds";
import { getRecordings } from "../utils/recordings/getRecordings";
import { printRecordings } from "../utils/recordings/printRecordings";
import { selectRecordings } from "../utils/recordings/selectRecordings";
import { LocalRecording } from "../utils/recordings/types";
import { uploadRecordings } from "../utils/recordings/upload/uploadRecordings";
import { dim } from "../utils/theme";
import { findRecordingsWithShortIds } from "@replay-cli/shared/recording/findRecordingsWithShortIds";
import { getRecordings } from "@replay-cli/shared/recording/getRecordings";
import { printRecordings } from "@replay-cli/shared/recording/printRecordings";
import { selectRecordings } from "@replay-cli/shared/recording/selectRecordings";
import { LocalRecording } from "@replay-cli/shared/recording/types";
import { uploadRecordings } from "@replay-cli/shared/recording/upload/uploadRecordings";
import { dim } from "@replay-cli/shared/theme";

registerCommand("upload", { requireAuthentication: true })
.argument("[ids...]", `Recording ids ${dim("(comma-separated)")}`, value => value.split(","))
Expand Down
1 change: 1 addition & 0 deletions packages/replayio/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO [PRO-720] Remove these in favor of values exported by "shared"
export const replayApiServer = process.env.REPLAY_API_SERVER || "https://api.replay.io";
export const replayAppHost = process.env.REPLAY_APP_SERVER || "https://app.replay.io";
export const replayWsServer =
Expand Down
6 changes: 3 additions & 3 deletions packages/replayio/src/utils/async/logAsyncOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
STATUS_RESOLVED,
Status,
} from "@replay-cli/shared/async/createDeferred";
import { statusFailed, statusPending, statusSuccess } from "@replay-cli/shared/theme";
import { dots } from "cli-spinners";
import { logUpdate } from "../../../../shared/src/logUpdate";
import { disableAnimatedLog } from "../../config";
import { logUpdate } from "../logUpdate";
import { statusFailed, statusPending, statusSuccess } from "../theme";

export type LogProgressOptions = { delayBeforeLoggingMs?: number };

Expand All @@ -21,7 +21,7 @@ export function logAsyncOperation(
let displayedMessage = initialMessage;

const print = () => {
let prefix: string;
let prefix: string = "";
switch (status) {
case STATUS_PENDING:
if (!disableAnimatedLog && delayBeforeLoggingMs > 0 && Date.now() < logAfter) {
Expand Down
3 changes: 0 additions & 3 deletions packages/replayio/src/utils/authentication/debug.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/replayio/src/utils/browser/debug.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/replayio/src/utils/browser/getRunningProcess.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger } from "@replay-cli/shared/logger";
import { highlight } from "@replay-cli/shared/theme";
import findProcess from "find-process";
import { highlight } from "../theme";
import { debug } from "./debug";
import { getBrowserPath } from "./getBrowserPath";

export async function getRunningProcess() {
Expand All @@ -10,7 +10,7 @@ export async function getRunningProcess() {
if (processes.length > 0) {
const match = processes[0];

debug(`Browser process already running at ${highlight(match.pid)}`);
logger.debug(`Browser process already running at ${highlight(match.pid)}`);

return match;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/replayio/src/utils/browser/killBrowserIfRunning.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { exitProcess } from "@replay-cli/shared/process/exitProcess";
import { killProcess } from "@replay-cli/shared/process/killProcess";
import { confirm } from "../confirm";
import { exitProcess } from "../exitProcess";
import { killProcess } from "../killProcess";
import { getRunningProcess } from "./getRunningProcess";

export async function killBrowserIfRunning() {
Expand Down
21 changes: 8 additions & 13 deletions packages/replayio/src/utils/browser/launchBrowser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getReplayPath } from "@replay-cli/shared/getReplayPath";
import { logger } from "@replay-cli/shared/logger";
import { dim, stderrPrefix, stdoutPrefix } from "@replay-cli/shared/theme";
import { ensureDirSync, existsSync } from "fs-extra";
import { join } from "path";
import { getReplayPath } from "@replay-cli/shared/getReplayPath";
import { spawnProcess } from "../../../../shared/src/spawnProcess";
import { runtimeMetadata, runtimePath } from "../installation/config";
import { prompt } from "../prompt/prompt";
import { spawnProcess } from "../spawnProcess";
import { dim, stderrPrefix, stdoutPrefix } from "../theme";
import { debug } from "./debug";
import { getBrowserPath } from "./getBrowserPath";

export async function launchBrowser(
Expand Down Expand Up @@ -39,16 +39,11 @@ export async function launchBrowser(
};

if (!existsSync(browserExecutablePath)) {
debug(`Replay browser not found at: ${browserExecutablePath}`);
logger.debug(`Replay browser not found at: ${browserExecutablePath}`);
throw new Error(`Replay browser not found at: ${browserExecutablePath}`);
}

debug(
`Launching browser: ${browserExecutablePath} with args:\n`,
args.join("\n"),
"\n",
processOptions
);
logger.debug("Launching browser", { args, browserExecutablePath, processOptions });

// Wait until the user quits the browser process OR
// until the user presses a key to continue (in which case, we will kill the process)
Expand Down Expand Up @@ -80,10 +75,10 @@ export async function launchBrowser(
}
},
printStderr: (text: string) => {
debug(stderrPrefix("stderr"), text);
logger.debug(`${stderrPrefix("stderr")} ${text}`);
},
printStdout: (text: string) => {
debug(stdoutPrefix("stdout"), text);
logger.debug(`${stdoutPrefix("stdout")} ${text}`);
},
});

Expand Down
Loading

0 comments on commit 33656ea

Please sign in to comment.