From 8159082baca52f97d7914e56da6b1b81f7a32c79 Mon Sep 17 00:00:00 2001 From: Sasha Date: Mon, 8 Jul 2024 17:46:59 +0200 Subject: [PATCH] [CLI] feat: on-the-fly login during publishing (CDMD-3687) (#1034) --- apps/cli/src/commands/publish.ts | 22 ++++++++++++++++++---- apps/cli/src/commands/unpublish.ts | 23 +++++++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/apps/cli/src/commands/publish.ts b/apps/cli/src/commands/publish.ts index 5cd5b966c..ba702093a 100644 --- a/apps/cli/src/commands/publish.ts +++ b/apps/cli/src/commands/publish.ts @@ -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; @@ -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; diff --git a/apps/cli/src/commands/unpublish.ts b/apps/cli/src/commands/unpublish.ts index 3f466de36..8df337a76 100644 --- a/apps/cli/src/commands/unpublish.ts +++ b/apps/cli/src/commands/unpublish.ts @@ -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; @@ -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);