Skip to content

Commit

Permalink
fix(cli): when error opening a link - show it in console
Browse files Browse the repository at this point in the history
  • Loading branch information
arybitskiy committed Nov 13, 2024
1 parent 4a64b99 commit cae3437
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 42 deletions.
13 changes: 3 additions & 10 deletions apps/cli/src/auth-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { backOff } from "exponential-backoff";
import inquirer from "inquirer";
import open from "open";
import { open } from "./utils/open.js";

import type { GetUserDataResponse } from "@codemod-com/api-types";
import { type Printer, chalk } from "@codemod-com/printer";
Expand Down Expand Up @@ -108,17 +108,10 @@ const routeUserToStudioForPermissions = ({
printer: Printer;
scopes: string[];
}) => {
const success = open(
open(
`${process.env.CODEMOD_HOME_PAGE_URL}?permissions=github&scopes=${scopes.join("&scopes=")}`,
printer,
);

if (!success) {
printer.printOperationMessage({
kind: "error",
message:
"An unexpected error occurred while redirecting to the permissions page. Please submit a GitHub issue (github.com/codemod-com/codemod/issues/new) or report it to us (codemod.com/community).",
});
}
};

export const requestGithubPermissions = async (options: {
Expand Down
12 changes: 2 additions & 10 deletions apps/cli/src/commands/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import open from "open";
import { open } from "../utils/open.js";

import { type Printer, chalk } from "@codemod-com/printer";

Expand All @@ -13,13 +13,5 @@ export const handleFeedbackCommand = async (options: {
chalk.cyan("Redirecting to the feedback page..."),
);

const success = await open(feedbackUrl);

if (!success) {
printer.printOperationMessage({
kind: "error",
message:
"Unexpected error occurred while redirecting to the feedback page.",
});
}
await open(feedbackUrl, printer);
};
12 changes: 2 additions & 10 deletions apps/cli/src/commands/learn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from "node:child_process";
import { dirname, extname } from "node:path";
import open from "open";
import { Project } from "ts-morph";
import { open } from "../utils/open.js";

import { type Printer, chalk } from "@codemod-com/printer";
import {
Expand Down Expand Up @@ -272,13 +272,5 @@ export const handleLearnCliCommand = async (options: {
chalk.cyan("Learning went successful! Opening the Codemod Studio...\n"),
);

const success = open(url);

if (!success) {
printer.printOperationMessage({
kind: "error",
message: "Unexpected error occurred while opening the Codemod Studio.",
});
return;
}
open(url, printer);
};
13 changes: 3 additions & 10 deletions apps/cli/src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { backOff } from "exponential-backoff";
import open from "open";
import { open } from "../utils/open.js";

import { type Printer, chalk } from "@codemod-com/printer";
import {
Expand All @@ -20,17 +20,10 @@ const routeUserToStudioForLogin = (
sessionId: string,
iv: string,
) => {
const success = open(
open(
`${process.env.CODEMOD_HOME_PAGE_URL}?command=${ACCESS_TOKEN_REQUESTED_BY_CLI_KEY}&sessionId=${sessionId}&iv=${iv}`,
printer,
);

if (!success) {
printer.printOperationMessage({
kind: "error",
message:
"An unexpected error occurred while redirecting to the sign-in page. Please submit a GitHub issue (github.com/codemod-com/codemod/issues/new) or report it to us (codemod.com/community).",
});
}
};
export const handleLoginCliCommand = async (options: {
printer: Printer;
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
execPromise,
getCodemodRc,
} from "@codemod-com/utilities";
import open from "open";
import { version as cliVersion } from "#/../package.json";
import { getDiff, getDiffScreen } from "#dryrun-diff.js";
import { fetchCodemod, populateCodemodArgs } from "#fetch-codemod.js";
Expand All @@ -27,6 +26,7 @@ import type { TelemetryEvent } from "#telemetry.js";
import type { NamedFileCommand } from "#types/commands.js";
import { originalStdoutWrite } from "#utils/constants.js";
import { logsPath, writeLogs } from "#utils/logs.js";
import { open } from "../utils/open.js";

const checkFileTreeVersioning = async (target: string) => {
let force = true;
Expand Down Expand Up @@ -102,7 +102,7 @@ export const handleRunCliCommand = async (options: {
const nameOrPath = args._.at(0)?.toString() ?? args.source ?? null;
if (nameOrPath === null) {
if (args.logs) {
return open(logsPath);
return open(logsPath, printer);
}

throw new Error("Codemod to run was not specified!");
Expand Down
13 changes: 13 additions & 0 deletions apps/cli/src/utils/open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Printer } from "@codemod-com/printer";
import openOriginal from "open";

export async function open(url: string, printer: Printer) {
try {
return await openOriginal(url);
} catch (error: any) {
printer.printOperationMessage({
kind: "error",
message: `Please open the following URL in your browser: ${url}`,
});
}
}

0 comments on commit cae3437

Please sign in to comment.