From 78134438c5d688e595b8ac87bd32b9c2f5d5c487 Mon Sep 17 00:00:00 2001 From: vafakaramzadegan Date: Tue, 11 Jun 2024 00:54:04 +0330 Subject: [PATCH] Show integrations using component before deletion Before, the CLI only mentioned that the component was in use by an integration, making it hard to know which integrations were involved. This update improves the deletion message by showing a list of integrations that currently use the component. --- src/commands/components/delete.ts | 78 ++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/src/commands/components/delete.ts b/src/commands/components/delete.ts index 089dd99..3530f92 100644 --- a/src/commands/components/delete.ts +++ b/src/commands/components/delete.ts @@ -1,5 +1,17 @@ -import { Command, Args } from "@oclif/core"; +import { Command, Args, ux } from "@oclif/core"; import { gqlRequest, gql } from "../../graphql.js"; +import { prismaticUrl } from "../../auth.js"; + +interface Integration { + id: string; + name: string; + components: { nodes: { id: string }[] }; +} + +interface FoundIntegrations { + ids: string[]; + names: string[]; +} export default class DeleteCommand extends Command { static description = "Delete a Component"; @@ -15,6 +27,70 @@ export default class DeleteCommand extends Command { args: { component }, } = await this.parse(DeleteCommand); + // Check if the component is being used in any integrations + const { + integrations: { nodes }, + } = await gqlRequest({ + document: gql` + query ListIntegrationsWithComponents { + integrations( + allVersions: true + ) { + nodes { + id + name + components { + nodes { + id + } + } + } + } + } + `, + variables: { + id: component, + }, + }); + + const foundIntegrations = nodes.reduce( + (accumulator: FoundIntegrations, integration: Integration) => { + const { id: integrationId, name: integrationName, components } = integration; + const componentIds = components.nodes.map((component) => component.id); + if (componentIds.includes(component)) { + accumulator.ids.push(integrationId); + accumulator.names.push(integrationName); + } + return accumulator; + }, + { ids: [], names: [] }, + ); + + if (foundIntegrations.ids.length > 0) { + console.log( + "The component is already being used in these integrations. You should delete the component from these integrations first:\n", + ); + const tableData = foundIntegrations.ids.map((id: string, index: number) => ({ + name: foundIntegrations.names[index], + url: `${prismaticUrl}designer/${id}`, + })); + + ux.table( + tableData, + { + name: { + header: "Integration Name", + }, + url: { + header: "Integration URL", + }, + }, + {}, + ); + + return; + } + await gqlRequest({ document: gql` mutation deleteComponent($id: ID!) {