UNPKG

linear-cmd

Version:

A GitHub CLI-like tool for Linear - manage issues, accounts, and more

91 lines (90 loc) 4.05 kB
import { LinearClient } from '@linear/sdk'; import { Command } from 'commander'; import inquirer from 'inquirer'; import { ConfigManager } from '../../lib/config-manager.js'; import { findAccountForProject, LinearAPIClient } from '../../lib/linear-client.js'; import { logger } from '../../lib/logger.js'; import {} from '../../schemas/definitions/project.js'; import { CommandNames, SubCommandNames } from '../../schemas/definitions.js'; import { createSubCommandFromSchema } from '../../schemas/utils.js'; export function createDeleteProjectCommand() { return createSubCommandFromSchema(CommandNames.PROJECT, SubCommandNames.PROJECT_DELETE, async (idOrUrl, options) => { const configManager = new ConfigManager(); try { const linearClient = new LinearAPIClient(); // Find the account that has access to this project let accountName; let client; if (options.account) { const account = configManager.getAccount(options.account); if (!account) { logger.error(`Account '${options.account}' not found`); logger.dim('Run `linear account list` to see available accounts'); process.exit(1); } client = new LinearClient({ apiKey: account.api_key }); accountName = account.name; } else { const result = await findAccountForProject(configManager, idOrUrl); if (!result) { logger.error('Could not find an account with access to this project'); logger.dim('Use --account flag to specify which account to use'); logger.dim('Run `linear account list` to see available accounts'); process.exit(1); } client = result.client; accountName = result.account.name; } // Get project details logger.loading('Fetching project details...'); const { projectId: projectIdOrSlug } = linearClient.parseProjectUrl(idOrUrl); // Try to get project by ID first, if fails, try by slugId let project; try { project = await client.project(projectIdOrSlug); } catch { // If ID lookup fails, try searching by slugId const projects = await client.projects({ filter: { slugId: { eq: projectIdOrSlug } } }); if (projects.nodes.length === 0) { throw new Error(`Project not found with ID or slug: ${projectIdOrSlug}`); } project = projects.nodes[0]; } const projectName = project.name; const projectId = project.id; // Confirm deletion unless --yes flag is used if (!options.yes) { const answer = await inquirer.prompt([ { type: 'confirm', name: 'confirm', message: `Are you sure you want to delete project "${projectName}"?`, default: false } ]); if (!answer.confirm) { logger.info('Project deletion cancelled'); return; } } // Delete the project logger.loading(`Deleting project from account: ${accountName}...`); const deleteResult = await client.deleteProject(projectId); const success = deleteResult.success; if (success) { logger.success(`Project "${projectName}" deleted successfully!`); } else { logger.error('Failed to delete project'); } } catch (error) { logger.error('Error deleting project', error); process.exit(1); } }); }