Compare commits

...

5 Commits

Author SHA1 Message Date
Ralph Khreish
344f40c699 fix: implement support for MCP 2025-04-19 10:45:00 +02:00
Ralph Khreish
8840d2fb3b chore: fix formatting issues 2025-04-19 00:09:22 +02:00
Kresna Sucandra
3eca720f36 feat: Enhance remove-task command to handle multiple comma-separated task IDs 2025-04-19 00:08:06 +02:00
Ralph Khreish
d99fa00980 feat: improve task-master init (#248)
* chore: fix weird bug where package.json is not upgrading its version based on current package version

* feat: improve `tm init`
2025-04-17 19:32:30 +02:00
Ralph Khreish
b2ccd60526 feat: add new bin task-master-ai same name as package to allow npx -y task-master-ai to work (#253) 2025-04-17 19:30:30 +02:00
12 changed files with 276 additions and 337 deletions

View File

@@ -0,0 +1,5 @@
---
'task-master-ai': patch
---
Fix remove-task command to handle multiple comma-separated task IDs

View File

@@ -0,0 +1,6 @@
---
'task-master-ai': patch
---
- Fix `task-master init` polluting codebase with new packages inside `package.json` and modifying project `README`
- Now only initializes with cursor rules, windsurf rules, mcp.json, scripts/example_prd.txt, .gitignore modifications, and `README-task-master.md`

View File

@@ -0,0 +1,5 @@
---
'task-master-ai': minor
---
Add `npx task-master-ai` that runs mcp instead of using `task-master-mcp``

View File

@@ -146,7 +146,7 @@ To enable enhanced task management capabilities directly within Cursor using the
4. Configure with the following details: 4. Configure with the following details:
- Name: "Task Master" - Name: "Task Master"
- Type: "Command" - Type: "Command"
- Command: "npx -y task-master-mcp" - Command: "npx -y task-master-ai"
5. Save the settings 5. Save the settings
Once configured, you can interact with Task Master's task management commands directly through Cursor's interface, providing a more integrated experience. Once configured, you can interact with Task Master's task management commands directly through Cursor's interface, providing a more integrated experience.

View File

@@ -20,20 +20,14 @@ A task management system for AI-driven development with Claude, designed to work
MCP (Model Control Protocol) provides the easiest way to get started with Task Master directly in your editor. MCP (Model Control Protocol) provides the easiest way to get started with Task Master directly in your editor.
1. **Install the package** 1. **Add the MCP config to your editor** (Cursor recommended, but it works with other text editors):
```bash
npm i -g task-master-ai
```
2. **Add the MCP config to your editor** (Cursor recommended, but it works with other text editors):
```json ```json
{ {
"mcpServers": { "mcpServers": {
"taskmaster-ai": { "taskmaster-ai": {
"command": "npx", "command": "npx",
"args": ["-y", "task-master-mcp"], "args": ["-y", "task-master-ai"],
"env": { "env": {
"ANTHROPIC_API_KEY": "YOUR_ANTHROPIC_API_KEY_HERE", "ANTHROPIC_API_KEY": "YOUR_ANTHROPIC_API_KEY_HERE",
"PERPLEXITY_API_KEY": "YOUR_PERPLEXITY_API_KEY_HERE", "PERPLEXITY_API_KEY": "YOUR_PERPLEXITY_API_KEY_HERE",

View File

@@ -10,20 +10,14 @@ There are two ways to set up Task Master: using MCP (recommended) or via npm ins
MCP (Model Control Protocol) provides the easiest way to get started with Task Master directly in your editor. MCP (Model Control Protocol) provides the easiest way to get started with Task Master directly in your editor.
1. **Install the package** 1. **Add the MCP config to your editor** (Cursor recommended, but it works with other text editors):
```bash
npm i -g task-master-ai
```
2. **Add the MCP config to your editor** (Cursor recommended, but it works with other text editors):
```json ```json
{ {
"mcpServers": { "mcpServers": {
"taskmaster-ai": { "taskmaster-ai": {
"command": "npx", "command": "npx",
"args": ["-y", "task-master-mcp"], "args": ["-y", "task-master-ai"],
"env": { "env": {
"ANTHROPIC_API_KEY": "YOUR_ANTHROPIC_API_KEY_HERE", "ANTHROPIC_API_KEY": "YOUR_ANTHROPIC_API_KEY_HERE",
"PERPLEXITY_API_KEY": "YOUR_PERPLEXITY_API_KEY_HERE", "PERPLEXITY_API_KEY": "YOUR_PERPLEXITY_API_KEY_HERE",

View File

@@ -3,18 +3,23 @@
* Direct function implementation for removing a task * Direct function implementation for removing a task
*/ */
import { removeTask } from '../../../../scripts/modules/task-manager.js'; import {
removeTask,
taskExists
} from '../../../../scripts/modules/task-manager.js';
import { import {
enableSilentMode, enableSilentMode,
disableSilentMode disableSilentMode,
readJSON
} from '../../../../scripts/modules/utils.js'; } from '../../../../scripts/modules/utils.js';
/** /**
* Direct function wrapper for removeTask with error handling. * Direct function wrapper for removeTask with error handling.
* Supports removing multiple tasks at once with comma-separated IDs.
* *
* @param {Object} args - Command arguments * @param {Object} args - Command arguments
* @param {string} args.tasksJsonPath - Explicit path to the tasks.json file. * @param {string} args.tasksJsonPath - Explicit path to the tasks.json file.
* @param {string} args.id - The ID of the task or subtask to remove. * @param {string} args.id - The ID(s) of the task(s) or subtask(s) to remove (comma-separated for multiple).
* @param {Object} log - Logger object * @param {Object} log - Logger object
* @returns {Promise<Object>} - Remove task result { success: boolean, data?: any, error?: { code: string, message: string }, fromCache: false } * @returns {Promise<Object>} - Remove task result { success: boolean, data?: any, error?: { code: string, message: string }, fromCache: false }
*/ */
@@ -36,8 +41,7 @@ export async function removeTaskDirect(args, log) {
} }
// Validate task ID parameter // Validate task ID parameter
const taskId = id; if (!id) {
if (!taskId) {
log.error('Task ID is required'); log.error('Task ID is required');
return { return {
success: false, success: false,
@@ -49,46 +53,103 @@ export async function removeTaskDirect(args, log) {
}; };
} }
// Skip confirmation in the direct function since it's handled by the client // Split task IDs if comma-separated
log.info(`Removing task with ID: ${taskId} from ${tasksJsonPath}`); const taskIdArray = id.split(',').map((taskId) => taskId.trim());
try { log.info(
// Enable silent mode to prevent console logs from interfering with JSON response `Removing ${taskIdArray.length} task(s) with ID(s): ${taskIdArray.join(', ')} from ${tasksJsonPath}`
enableSilentMode(); );
// Call the core removeTask function using the provided path // Validate all task IDs exist before proceeding
const result = await removeTask(tasksJsonPath, taskId); const data = readJSON(tasksJsonPath);
if (!data || !data.tasks) {
// Restore normal logging
disableSilentMode();
log.info(`Successfully removed task: ${taskId}`);
// Return the result
return {
success: true,
data: {
message: result.message,
taskId: taskId,
tasksPath: tasksJsonPath,
removedTask: result.removedTask
},
fromCache: false
};
} catch (error) {
// Make sure to restore normal logging even if there's an error
disableSilentMode();
log.error(`Error removing task: ${error.message}`);
return { return {
success: false, success: false,
error: { error: {
code: error.code || 'REMOVE_TASK_ERROR', code: 'INVALID_TASKS_FILE',
message: error.message || 'Failed to remove task' message: `No valid tasks found in ${tasksJsonPath}`
}, },
fromCache: false fromCache: false
}; };
} }
const invalidTasks = taskIdArray.filter(
(taskId) => !taskExists(data.tasks, taskId)
);
if (invalidTasks.length > 0) {
return {
success: false,
error: {
code: 'INVALID_TASK_ID',
message: `The following tasks were not found: ${invalidTasks.join(', ')}`
},
fromCache: false
};
}
// Remove tasks one by one
const results = [];
// Enable silent mode to prevent console logs from interfering with JSON response
enableSilentMode();
try {
for (const taskId of taskIdArray) {
try {
const result = await removeTask(tasksJsonPath, taskId);
results.push({
taskId,
success: true,
message: result.message,
removedTask: result.removedTask
});
log.info(`Successfully removed task: ${taskId}`);
} catch (error) {
results.push({
taskId,
success: false,
error: error.message
});
log.error(`Error removing task ${taskId}: ${error.message}`);
}
}
} finally {
// Restore normal logging
disableSilentMode();
}
// Check if all tasks were successfully removed
const successfulRemovals = results.filter((r) => r.success);
const failedRemovals = results.filter((r) => !r.success);
if (successfulRemovals.length === 0) {
// All removals failed
return {
success: false,
error: {
code: 'REMOVE_TASK_ERROR',
message: 'Failed to remove any tasks',
details: failedRemovals
.map((r) => `${r.taskId}: ${r.error}`)
.join('; ')
},
fromCache: false
};
}
// At least some tasks were removed successfully
return {
success: true,
data: {
totalTasks: taskIdArray.length,
successful: successfulRemovals.length,
failed: failedRemovals.length,
results: results,
tasksPath: tasksJsonPath
},
fromCache: false
};
} catch (error) { } catch (error) {
// Ensure silent mode is disabled even if an outer error occurs // Ensure silent mode is disabled even if an outer error occurs
disableSilentMode(); disableSilentMode();

View File

@@ -23,7 +23,9 @@ export function registerRemoveTaskTool(server) {
parameters: z.object({ parameters: z.object({
id: z id: z
.string() .string()
.describe("ID of the task or subtask to remove (e.g., '5' or '5.2')"), .describe(
"ID(s) of the task(s) or subtask(s) to remove (e.g., '5' or '5.2' or '5,6,7')"
),
file: z.string().optional().describe('Absolute path to the tasks file'), file: z.string().optional().describe('Absolute path to the tasks file'),
projectRoot: z projectRoot: z
.string() .string()
@@ -35,7 +37,7 @@ export function registerRemoveTaskTool(server) {
}), }),
execute: async (args, { log, session }) => { execute: async (args, { log, session }) => {
try { try {
log.info(`Removing task with ID: ${args.id}`); log.info(`Removing task(s) with ID(s): ${args.id}`);
// Get project root from args or session // Get project root from args or session
const rootFolder = const rootFolder =

5
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "task-master-ai", "name": "task-master-ai",
"version": "0.10.1", "version": "0.11.1",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "task-master-ai", "name": "task-master-ai",
"version": "0.10.1", "version": "0.11.1",
"license": "MIT WITH Commons-Clause", "license": "MIT WITH Commons-Clause",
"dependencies": { "dependencies": {
"@anthropic-ai/sdk": "^0.39.0", "@anthropic-ai/sdk": "^0.39.0",
@@ -31,6 +31,7 @@
}, },
"bin": { "bin": {
"task-master": "bin/task-master.js", "task-master": "bin/task-master.js",
"task-master-ai": "mcp-server/server.js",
"task-master-mcp": "mcp-server/server.js" "task-master-mcp": "mcp-server/server.js"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -1,12 +1,13 @@
{ {
"name": "task-master-ai", "name": "task-master-ai",
"version": "0.11.0", "version": "0.11.1",
"description": "A task management system for ambitious AI-driven development that doesn't overwhelm and confuse Cursor.", "description": "A task management system for ambitious AI-driven development that doesn't overwhelm and confuse Cursor.",
"main": "index.js", "main": "index.js",
"type": "module", "type": "module",
"bin": { "bin": {
"task-master": "bin/task-master.js", "task-master": "bin/task-master.js",
"task-master-mcp": "mcp-server/server.js" "task-master-mcp": "mcp-server/server.js",
"task-master-ai": "mcp-server/server.js"
}, },
"scripts": { "scripts": {
"test": "node --experimental-vm-modules node_modules/.bin/jest", "test": "node --experimental-vm-modules node_modules/.bin/jest",

View File

@@ -15,7 +15,6 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { execSync } from 'child_process';
import readline from 'readline'; import readline from 'readline';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { dirname } from 'path'; import { dirname } from 'path';
@@ -179,9 +178,6 @@ function copyTemplateFile(templateName, targetPath, replacements = {}) {
// Map template names to their actual source paths // Map template names to their actual source paths
switch (templateName) { switch (templateName) {
case 'dev.js':
sourcePath = path.join(__dirname, 'dev.js');
break;
case 'scripts_README.md': case 'scripts_README.md':
sourcePath = path.join(__dirname, '..', 'assets', 'scripts_README.md'); sourcePath = path.join(__dirname, '..', 'assets', 'scripts_README.md');
break; break;
@@ -297,61 +293,8 @@ function copyTemplateFile(templateName, targetPath, replacements = {}) {
return; return;
} }
// Handle package.json - merge dependencies
if (filename === 'package.json') {
log('info', `${targetPath} already exists, merging dependencies...`);
try {
const existingPackageJson = JSON.parse(
fs.readFileSync(targetPath, 'utf8')
);
const newPackageJson = JSON.parse(content);
// Merge dependencies, preferring existing versions in case of conflicts
existingPackageJson.dependencies = {
...newPackageJson.dependencies,
...existingPackageJson.dependencies
};
// Add our scripts if they don't already exist
existingPackageJson.scripts = {
...existingPackageJson.scripts,
...Object.fromEntries(
Object.entries(newPackageJson.scripts).filter(
([key]) => !existingPackageJson.scripts[key]
)
)
};
// Preserve existing type if present
if (!existingPackageJson.type && newPackageJson.type) {
existingPackageJson.type = newPackageJson.type;
}
fs.writeFileSync(
targetPath,
JSON.stringify(existingPackageJson, null, 2)
);
log(
'success',
`Updated ${targetPath} with required dependencies and scripts`
);
} catch (error) {
log('error', `Failed to merge package.json: ${error.message}`);
// Fallback to writing a backup of the existing file and creating a new one
const backupPath = `${targetPath}.backup-${Date.now()}`;
fs.copyFileSync(targetPath, backupPath);
log('info', `Created backup of existing package.json at ${backupPath}`);
fs.writeFileSync(targetPath, content);
log(
'warn',
`Replaced ${targetPath} with new content (due to JSON parsing error)`
);
}
return;
}
// Handle README.md - offer to preserve or create a different file // Handle README.md - offer to preserve or create a different file
if (filename === 'README.md') { if (filename === 'README-task-master.md') {
log('info', `${targetPath} already exists`); log('info', `${targetPath} already exists`);
// Create a separate README file specifically for this project // Create a separate README file specifically for this project
const taskMasterReadmePath = path.join( const taskMasterReadmePath = path.join(
@@ -361,7 +304,7 @@ function copyTemplateFile(templateName, targetPath, replacements = {}) {
fs.writeFileSync(taskMasterReadmePath, content); fs.writeFileSync(taskMasterReadmePath, content);
log( log(
'success', 'success',
`Created ${taskMasterReadmePath} (preserved original README.md)` `Created ${taskMasterReadmePath} (preserved original README-task-master.md)`
); );
return; return;
} }
@@ -396,6 +339,30 @@ async function initializeProject(options = {}) {
console.log('=================================================='); console.log('==================================================');
} }
// Try to get project name from package.json if not provided
if (!options.name) {
const packageJsonPath = path.join(process.cwd(), 'package.json');
try {
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(
fs.readFileSync(packageJsonPath, 'utf8')
);
if (packageJson.name) {
log(
'info',
`Found project name '${packageJson.name}' in package.json`
);
options.name = packageJson.name;
}
}
} catch (error) {
log(
'debug',
`Could not read project name from package.json: ${error.message}`
);
}
}
// Determine if we should skip prompts based on the passed options // Determine if we should skip prompts based on the passed options
const skipPrompts = options.yes || (options.name && options.description); const skipPrompts = options.yes || (options.name && options.description);
if (!isSilentMode()) { if (!isSilentMode()) {
@@ -414,7 +381,6 @@ async function initializeProject(options = {}) {
const projectVersion = options.version || '0.1.0'; // Default from commands.js or here const projectVersion = options.version || '0.1.0'; // Default from commands.js or here
const authorName = options.author || 'Vibe coder'; // Default if not provided const authorName = options.author || 'Vibe coder'; // Default if not provided
const dryRun = options.dryRun || false; const dryRun = options.dryRun || false;
const skipInstall = options.skipInstall || false;
const addAliases = options.aliases || false; const addAliases = options.aliases || false;
if (dryRun) { if (dryRun) {
@@ -429,9 +395,6 @@ async function initializeProject(options = {}) {
if (addAliases) { if (addAliases) {
log('info', 'Would add shell aliases for task-master'); log('info', 'Would add shell aliases for task-master');
} }
if (!skipInstall) {
log('info', 'Would install dependencies');
}
return { return {
projectName, projectName,
projectDescription, projectDescription,
@@ -447,7 +410,6 @@ async function initializeProject(options = {}) {
projectDescription, projectDescription,
projectVersion, projectVersion,
authorName, authorName,
skipInstall,
addAliases addAliases
); );
} else { } else {
@@ -514,9 +476,8 @@ async function initializeProject(options = {}) {
return; // Added return for clarity return; // Added return for clarity
} }
// Still respect dryRun/skipInstall if passed initially even when prompting // Still respect dryRun if passed initially even when prompting
const dryRun = options.dryRun || false; const dryRun = options.dryRun || false;
const skipInstall = options.skipInstall || false;
if (dryRun) { if (dryRun) {
log('info', 'DRY RUN MODE: No files will be modified'); log('info', 'DRY RUN MODE: No files will be modified');
@@ -530,9 +491,6 @@ async function initializeProject(options = {}) {
if (addAliasesPrompted) { if (addAliasesPrompted) {
log('info', 'Would add shell aliases for task-master'); log('info', 'Would add shell aliases for task-master');
} }
if (!skipInstall) {
log('info', 'Would install dependencies');
}
return { return {
projectName, projectName,
projectDescription, projectDescription,
@@ -548,7 +506,6 @@ async function initializeProject(options = {}) {
projectDescription, projectDescription,
projectVersion, projectVersion,
authorName, authorName,
skipInstall, // Use value from initial options
addAliasesPrompted // Use value from prompt addAliasesPrompted // Use value from prompt
); );
} catch (error) { } catch (error) {
@@ -574,7 +531,6 @@ function createProjectStructure(
projectDescription, projectDescription,
projectVersion, projectVersion,
authorName, authorName,
skipInstall,
addAliases addAliases
) { ) {
const targetDir = process.cwd(); const targetDir = process.cwd();
@@ -585,105 +541,8 @@ function createProjectStructure(
ensureDirectoryExists(path.join(targetDir, 'scripts')); ensureDirectoryExists(path.join(targetDir, 'scripts'));
ensureDirectoryExists(path.join(targetDir, 'tasks')); ensureDirectoryExists(path.join(targetDir, 'tasks'));
// Define our package.json content
const packageJson = {
name: projectName.toLowerCase().replace(/\s+/g, '-'),
version: projectVersion,
description: projectDescription,
author: authorName,
type: 'module',
scripts: {
dev: 'node scripts/dev.js',
list: 'node scripts/dev.js list',
generate: 'node scripts/dev.js generate',
'parse-prd': 'node scripts/dev.js parse-prd'
},
dependencies: {
'@anthropic-ai/sdk': '^0.39.0',
boxen: '^8.0.1',
chalk: '^4.1.2',
commander: '^11.1.0',
'cli-table3': '^0.6.5',
cors: '^2.8.5',
dotenv: '^16.3.1',
express: '^4.21.2',
fastmcp: '^1.20.5',
figlet: '^1.8.0',
'fuse.js': '^7.0.0',
'gradient-string': '^3.0.0',
helmet: '^8.1.0',
inquirer: '^12.5.0',
jsonwebtoken: '^9.0.2',
'lru-cache': '^10.2.0',
openai: '^4.89.0',
ora: '^8.2.0'
}
};
// Check if package.json exists and merge if it does
const packageJsonPath = path.join(targetDir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
log('info', 'package.json already exists, merging content...');
try {
const existingPackageJson = JSON.parse(
fs.readFileSync(packageJsonPath, 'utf8')
);
// Preserve existing fields but add our required ones
const mergedPackageJson = {
...existingPackageJson,
scripts: {
...existingPackageJson.scripts,
...Object.fromEntries(
Object.entries(packageJson.scripts).filter(
([key]) =>
!existingPackageJson.scripts ||
!existingPackageJson.scripts[key]
)
)
},
dependencies: {
...(existingPackageJson.dependencies || {}),
...Object.fromEntries(
Object.entries(packageJson.dependencies).filter(
([key]) =>
!existingPackageJson.dependencies ||
!existingPackageJson.dependencies[key]
)
)
}
};
// Ensure type is set if not already present
if (!mergedPackageJson.type && packageJson.type) {
mergedPackageJson.type = packageJson.type;
}
fs.writeFileSync(
packageJsonPath,
JSON.stringify(mergedPackageJson, null, 2)
);
log('success', 'Updated package.json with required fields');
} catch (error) {
log('error', `Failed to merge package.json: ${error.message}`);
// Create a backup before potentially modifying
const backupPath = `${packageJsonPath}.backup-${Date.now()}`;
fs.copyFileSync(packageJsonPath, backupPath);
log('info', `Created backup of existing package.json at ${backupPath}`);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
log(
'warn',
'Created new package.json (backup of original file was created)'
);
}
} else {
// If package.json doesn't exist, create it
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
log('success', 'Created package.json');
}
// Setup MCP configuration for integration with Cursor // Setup MCP configuration for integration with Cursor
setupMCPConfiguration(targetDir, packageJson.name); setupMCPConfiguration(targetDir, projectName);
// Copy template files with replacements // Copy template files with replacements
const replacements = { const replacements = {
@@ -731,15 +590,6 @@ function createProjectStructure(
// Copy .windsurfrules // Copy .windsurfrules
copyTemplateFile('windsurfrules', path.join(targetDir, '.windsurfrules')); copyTemplateFile('windsurfrules', path.join(targetDir, '.windsurfrules'));
// Copy scripts/dev.js
copyTemplateFile('dev.js', path.join(targetDir, 'scripts', 'dev.js'));
// Copy scripts/README.md
copyTemplateFile(
'scripts_README.md',
path.join(targetDir, 'scripts', 'README.md')
);
// Copy example_prd.txt // Copy example_prd.txt
copyTemplateFile( copyTemplateFile(
'example_prd.txt', 'example_prd.txt',
@@ -749,43 +599,13 @@ function createProjectStructure(
// Create main README.md // Create main README.md
copyTemplateFile( copyTemplateFile(
'README-task-master.md', 'README-task-master.md',
path.join(targetDir, 'README.md'), path.join(targetDir, 'README-task-master.md'),
replacements replacements
); );
// Initialize git repository if git is available // Add shell aliases if requested
try { if (addAliases) {
if (!fs.existsSync(path.join(targetDir, '.git'))) { addShellAliases();
log('info', 'Initializing git repository...');
execSync('git init', { stdio: 'ignore' });
log('success', 'Git repository initialized');
}
} catch (error) {
log('warn', 'Git not available, skipping repository initialization');
}
// Run npm install automatically
if (!isSilentMode()) {
console.log(
boxen(chalk.cyan('Installing dependencies...'), {
padding: 0.5,
margin: 0.5,
borderStyle: 'round',
borderColor: 'blue'
})
);
}
try {
if (!skipInstall) {
execSync('npm install', { stdio: 'inherit', cwd: targetDir });
log('success', 'Dependencies installed successfully!');
} else {
log('info', 'Dependencies installation skipped');
}
} catch (error) {
log('error', 'Failed to install dependencies:', error.message);
log('error', 'Please run npm install manually');
} }
// Display success message // Display success message
@@ -807,11 +627,6 @@ function createProjectStructure(
); );
} }
// Add shell aliases if requested
if (addAliases) {
addShellAliases();
}
// Display next steps in a nice box // Display next steps in a nice box
if (!isSilentMode()) { if (!isSilentMode()) {
console.log( console.log(

View File

@@ -1374,18 +1374,18 @@ function registerCommands(programInstance) {
// remove-task command // remove-task command
programInstance programInstance
.command('remove-task') .command('remove-task')
.description('Remove a task or subtask permanently') .description('Remove one or more tasks or subtasks permanently')
.option( .option(
'-i, --id <id>', '-i, --id <id>',
'ID of the task or subtask to remove (e.g., "5" or "5.2")' 'ID(s) of the task(s) or subtask(s) to remove (e.g., "5" or "5.2" or "5,6,7")'
) )
.option('-f, --file <file>', 'Path to the tasks file', 'tasks/tasks.json') .option('-f, --file <file>', 'Path to the tasks file', 'tasks/tasks.json')
.option('-y, --yes', 'Skip confirmation prompt', false) .option('-y, --yes', 'Skip confirmation prompt', false)
.action(async (options) => { .action(async (options) => {
const tasksPath = options.file; const tasksPath = options.file;
const taskId = options.id; const taskIds = options.id;
if (!taskId) { if (!taskIds) {
console.error(chalk.red('Error: Task ID is required')); console.error(chalk.red('Error: Task ID is required'));
console.error( console.error(
chalk.yellow('Usage: task-master remove-task --id=<taskId>') chalk.yellow('Usage: task-master remove-task --id=<taskId>')
@@ -1394,7 +1394,7 @@ function registerCommands(programInstance) {
} }
try { try {
// Check if the task exists // Check if the tasks file exists and is valid
const data = readJSON(tasksPath); const data = readJSON(tasksPath);
if (!data || !data.tasks) { if (!data || !data.tasks) {
console.error( console.error(
@@ -1403,75 +1403,89 @@ function registerCommands(programInstance) {
process.exit(1); process.exit(1);
} }
if (!taskExists(data.tasks, taskId)) { // Split task IDs if comma-separated
console.error(chalk.red(`Error: Task with ID ${taskId} not found`)); const taskIdArray = taskIds.split(',').map((id) => id.trim());
// Validate all task IDs exist before proceeding
const invalidTasks = taskIdArray.filter(
(id) => !taskExists(data.tasks, id)
);
if (invalidTasks.length > 0) {
console.error(
chalk.red(
`Error: The following tasks were not found: ${invalidTasks.join(', ')}`
)
);
process.exit(1); process.exit(1);
} }
// Load task for display
const task = findTaskById(data.tasks, taskId);
// Skip confirmation if --yes flag is provided // Skip confirmation if --yes flag is provided
if (!options.yes) { if (!options.yes) {
// Display task information // Display tasks to be removed
console.log(); console.log();
console.log( console.log(
chalk.red.bold( chalk.red.bold(
'⚠️ WARNING: This will permanently delete the following task:' '⚠️ WARNING: This will permanently delete the following tasks:'
) )
); );
console.log(); console.log();
if (typeof taskId === 'string' && taskId.includes('.')) { for (const taskId of taskIdArray) {
// It's a subtask const task = findTaskById(data.tasks, taskId);
const [parentId, subtaskId] = taskId.split('.');
console.log(chalk.white.bold(`Subtask ${taskId}: ${task.title}`));
console.log(
chalk.gray(
`Parent Task: ${task.parentTask.id} - ${task.parentTask.title}`
)
);
} else {
// It's a main task
console.log(chalk.white.bold(`Task ${taskId}: ${task.title}`));
// Show if it has subtasks if (typeof taskId === 'string' && taskId.includes('.')) {
if (task.subtasks && task.subtasks.length > 0) { // It's a subtask
const [parentId, subtaskId] = taskId.split('.');
console.log(chalk.white.bold(`Subtask ${taskId}: ${task.title}`));
console.log( console.log(
chalk.yellow( chalk.gray(
`⚠️ This task has ${task.subtasks.length} subtasks that will also be deleted!` `Parent Task: ${task.parentTask.id} - ${task.parentTask.title}`
) )
); );
} } else {
// It's a main task
console.log(chalk.white.bold(`Task ${taskId}: ${task.title}`));
// Show if other tasks depend on it // Show if it has subtasks
const dependentTasks = data.tasks.filter( if (task.subtasks && task.subtasks.length > 0) {
(t) => console.log(
t.dependencies && t.dependencies.includes(parseInt(taskId, 10)) chalk.yellow(
); `⚠️ This task has ${task.subtasks.length} subtasks that will also be deleted!`
)
);
}
if (dependentTasks.length > 0) { // Show if other tasks depend on it
console.log( const dependentTasks = data.tasks.filter(
chalk.yellow( (t) =>
`⚠️ Warning: ${dependentTasks.length} other tasks depend on this task!` t.dependencies &&
) t.dependencies.includes(parseInt(taskId, 10))
); );
console.log(chalk.yellow('These dependencies will be removed:'));
dependentTasks.forEach((t) => { if (dependentTasks.length > 0) {
console.log(chalk.yellow(` - Task ${t.id}: ${t.title}`)); console.log(
}); chalk.yellow(
`⚠️ Warning: ${dependentTasks.length} other tasks depend on this task!`
)
);
console.log(
chalk.yellow('These dependencies will be removed:')
);
dependentTasks.forEach((t) => {
console.log(chalk.yellow(` - Task ${t.id}: ${t.title}`));
});
}
} }
console.log();
} }
console.log();
// Prompt for confirmation // Prompt for confirmation
const { confirm } = await inquirer.prompt([ const { confirm } = await inquirer.prompt([
{ {
type: 'confirm', type: 'confirm',
name: 'confirm', name: 'confirm',
message: chalk.red.bold( message: chalk.red.bold(
'Are you sure you want to permanently delete this task?' `Are you sure you want to permanently delete ${taskIdArray.length > 1 ? 'these tasks' : 'this task'}?`
), ),
default: false default: false
} }
@@ -1483,31 +1497,72 @@ function registerCommands(programInstance) {
} }
} }
const indicator = startLoadingIndicator('Removing task...'); const indicator = startLoadingIndicator('Removing tasks...');
// Remove the task // Remove each task
const result = await removeTask(tasksPath, taskId); const results = [];
for (const taskId of taskIdArray) {
try {
const result = await removeTask(tasksPath, taskId);
results.push({ taskId, success: true, ...result });
} catch (error) {
results.push({ taskId, success: false, error: error.message });
}
}
stopLoadingIndicator(indicator); stopLoadingIndicator(indicator);
// Display success message with appropriate color based on task or subtask // Display results
if (typeof taskId === 'string' && taskId.includes('.')) { const successfulRemovals = results.filter((r) => r.success);
// It was a subtask const failedRemovals = results.filter((r) => !r.success);
if (successfulRemovals.length > 0) {
console.log( console.log(
boxen( boxen(
chalk.green(`Subtask ${taskId} has been successfully removed`), chalk.green(
{ padding: 1, borderColor: 'green', borderStyle: 'round' } `Successfully removed ${successfulRemovals.length} task${successfulRemovals.length > 1 ? 's' : ''}`
) +
'\n\n' +
successfulRemovals
.map((r) =>
chalk.white(
`${r.taskId.includes('.') ? 'Subtask' : 'Task'} ${r.taskId}`
)
)
.join('\n'),
{
padding: 1,
borderColor: 'green',
borderStyle: 'round',
margin: { top: 1 }
}
) )
); );
} else { }
// It was a main task
if (failedRemovals.length > 0) {
console.log( console.log(
boxen(chalk.green(`Task ${taskId} has been successfully removed`), { boxen(
padding: 1, chalk.red(
borderColor: 'green', `Failed to remove ${failedRemovals.length} task${failedRemovals.length > 1 ? 's' : ''}`
borderStyle: 'round' ) +
}) '\n\n' +
failedRemovals
.map((r) => chalk.white(`${r.taskId}: ${r.error}`))
.join('\n'),
{
padding: 1,
borderColor: 'red',
borderStyle: 'round',
margin: { top: 1 }
}
)
); );
// Exit with error if any removals failed
if (successfulRemovals.length === 0) {
process.exit(1);
}
} }
} catch (error) { } catch (error) {
console.error( console.error(