Skip to content

Commit

Permalink
[CLI] feat: on-the-fly login during publishing (CDMD-3687) (#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
r4zendev authored Jul 8, 2024
1 parent ea09bd5 commit 8159082
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
22 changes: 18 additions & 4 deletions apps/cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import inquirer from "inquirer";
import { publish } from "../apis.js";
import { getCurrentUserData, rebuildCodemodFallback } from "../utils.js";
import { handleInitCliCommand } from "./init.js";
import { handleLoginCliCommand } from "./login.js";

export const handlePublishCliCommand = async (options: {
printer: PrinterBlueprint;
Expand All @@ -22,12 +23,25 @@ export const handlePublishCliCommand = async (options: {
const { printer } = options;
let { source } = options;

const userData = await getCurrentUserData();
let userData = await getCurrentUserData();

if (userData === null) {
throw new Error(
"To be able to publish to Codemod Registry, please log in first.",
);
const { login } = await inquirer.prompt<{ login: boolean }>({
type: "confirm",
name: "login",
message: "Authentication is required to publish codemods. Proceed?",
});

if (!login) {
return;
}

await handleLoginCliCommand({ printer });
userData = await getCurrentUserData();

if (userData === null) {
throw new Error("Unexpected authentication error occurred.");
}
}

const { token } = userData;
Expand Down
23 changes: 19 additions & 4 deletions apps/cli/src/commands/unpublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
isNeitherNullNorUndefined,
} from "@codemod-com/utilities";
import { AxiosError } from "axios";
import inquirer from "inquirer";
import { unpublish } from "../apis.js";
import { getCurrentUserData } from "../utils.js";
import { handleLoginCliCommand } from "./login.js";

export const handleUnpublishCliCommand = async (options: {
printer: PrinterBlueprint;
Expand All @@ -15,12 +17,25 @@ export const handleUnpublishCliCommand = async (options: {
}) => {
const { printer, name, force } = options;

const userData = await getCurrentUserData();
let userData = await getCurrentUserData();

if (userData === null) {
throw new Error(
"To be able to unpublish your codemods, please log in first.",
);
const { login } = await inquirer.prompt<{ login: boolean }>({
type: "confirm",
name: "login",
message: "Authentication is required to unpublish codemods. Proceed?",
});

if (!login) {
return;
}

await handleLoginCliCommand({ printer });
userData = await getCurrentUserData();

if (userData === null) {
throw new Error("Unexpected authentication error occurred.");
}
}

const { libName: codemodName, version } = extractLibNameAndVersion(name);
Expand Down

0 comments on commit 8159082

Please sign in to comment.