UNPKG

advanced-live-server-installer

Version:

Auto-installer for Advanced Live Server VS Code Extension

365 lines 12.9 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TaskRunner = void 0; const vscode = __importStar(require("vscode")); const cp = __importStar(require("child_process")); class TaskRunner { constructor(outputChannel) { this.tasks = new Map(); this.runningTasks = new Set(); this.outputChannel = outputChannel; this.initializeDefaultTasks(); } initializeDefaultTasks() { const defaultTasks = [ // NPM Tasks { id: 'npm-install', name: 'npm install', command: 'npm', args: ['install'], group: 'npm', description: 'Install project dependencies', icon: '$(package)', }, { id: 'npm-start', name: 'npm start', command: 'npm', args: ['start'], group: 'npm', description: 'Start the development server', icon: '$(play)', }, { id: 'npm-build', name: 'npm run build', command: 'npm', args: ['run', 'build'], group: 'build', description: 'Build the project for production', icon: '$(gear)', }, { id: 'npm-test', name: 'npm test', command: 'npm', args: ['test'], group: 'test', description: 'Run tests', icon: '$(testing)', }, { id: 'npm-lint', name: 'npm run lint', command: 'npm', args: ['run', 'lint'], group: 'test', description: 'Run linting', icon: '$(check)', }, // Git Tasks { id: 'git-status', name: 'git status', command: 'git', args: ['status'], group: 'git', description: 'Check git status', icon: '$(git-branch)', }, { id: 'git-add', name: 'git add .', command: 'git', args: ['add', '.'], group: 'git', description: 'Stage all changes', icon: '$(plus)', }, { id: 'git-commit', name: 'git commit', command: 'git', args: ['commit', '-m', 'Update'], group: 'git', description: 'Commit changes', icon: '$(check)', }, { id: 'git-push', name: 'git push', command: 'git', args: ['push'], group: 'git', description: 'Push to remote', icon: '$(arrow-up)', }, { id: 'git-pull', name: 'git pull', command: 'git', args: ['pull'], group: 'git', description: 'Pull from remote', icon: '$(arrow-down)', }, // Build Tasks { id: 'build-prod', name: 'Build Production', command: 'npm', args: ['run', 'build'], group: 'build', description: 'Build for production', icon: '$(rocket)', }, { id: 'build-dev', name: 'Build Development', command: 'npm', args: ['run', 'build:dev'], group: 'build', description: 'Build for development', icon: '$(tools)', }, // Custom Tasks { id: 'clear-cache', name: 'Clear Cache', command: 'npm', args: ['cache', 'clean', '--force'], group: 'custom', description: 'Clear npm cache', icon: '$(trash)', }, { id: 'update-deps', name: 'Update Dependencies', command: 'npm', args: ['update'], group: 'custom', description: 'Update all dependencies', icon: '$(refresh)', }, ]; defaultTasks.forEach(task => { this.tasks.set(task.id, task); }); } async showTaskPicker() { const taskGroups = this.groupTasksByCategory(); const items = []; // Add grouped tasks Object.entries(taskGroups).forEach(([group, tasks]) => { items.push({ label: `$(folder) ${group.toUpperCase()}`, kind: vscode.QuickPickItemKind.Separator, }); tasks.forEach(task => { items.push({ label: `${task.icon || '$(terminal)'} ${task.name}`, description: task.description, detail: `${task.command} ${task.args?.join(' ') || ''}`, }); }); items.push({ label: '', kind: vscode.QuickPickItemKind.Separator, }); }); // Add custom task option items.push({ label: '$(plus) Add Custom Task', description: 'Create a new custom task', }); const selection = await vscode.window.showQuickPick(items, { placeHolder: 'Select a task to run', matchOnDescription: true, matchOnDetail: true, }); if (selection) { if (selection.label.includes('Add Custom Task')) { await this.addCustomTask(); } else { const taskName = selection.label.replace(/^\$\([^)]+\)\s/, ''); const task = Array.from(this.tasks.values()).find(t => t.name === taskName); if (task) { await this.runTask(task); } } } } groupTasksByCategory() { const groups = {}; this.tasks.forEach(task => { const group = task.group || 'custom'; if (!groups[group]) { groups[group] = []; } groups[group].push(task); }); return groups; } async runTask(task) { if (this.runningTasks.has(task.id)) { vscode.window.showWarningMessage(`Task "${task.name}" is already running`); return { success: false, output: '', error: 'Task already running', duration: 0, }; } this.runningTasks.add(task.id); const startTime = Date.now(); try { this.outputChannel.appendLine(`🚀 Running task: ${task.name}`); this.outputChannel.appendLine(`📝 Command: ${task.command} ${task.args?.join(' ') || ''}`); const result = await this.executeCommand(task); const duration = Date.now() - startTime; if (result.success) { this.outputChannel.appendLine(`✅ Task completed successfully in ${duration}ms`); vscode.window.showInformationMessage(`Task "${task.name}" completed successfully`); } else { this.outputChannel.appendLine(`❌ Task failed after ${duration}ms`); vscode.window.showErrorMessage(`Task "${task.name}" failed: ${result.error}`); } return { ...result, duration }; } finally { this.runningTasks.delete(task.id); } } executeCommand(task) { return new Promise(resolve => { const cwd = task.cwd || vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || process.cwd(); const child = cp.spawn(task.command, task.args || [], { cwd, shell: true, stdio: ['pipe', 'pipe', 'pipe'], }); let output = ''; let error = ''; child.stdout?.on('data', data => { const text = data.toString(); output += text; this.outputChannel.append(text); }); child.stderr?.on('data', data => { const text = data.toString(); error += text; this.outputChannel.append(text); }); child.on('close', code => { if (code === 0) { resolve({ success: true, output }); } else { resolve({ success: false, output, error: error || `Process exited with code ${code}`, }); } }); child.on('error', err => { resolve({ success: false, output, error: err.message }); }); }); } async addCustomTask() { const name = await vscode.window.showInputBox({ prompt: 'Enter task name', placeHolder: 'e.g., Deploy to Production', }); if (!name) { return; } const command = await vscode.window.showInputBox({ prompt: 'Enter command', placeHolder: 'e.g., npm', }); if (!command) { return; } const argsInput = await vscode.window.showInputBox({ prompt: 'Enter arguments (space-separated)', placeHolder: 'e.g., run deploy', }); const description = await vscode.window.showInputBox({ prompt: 'Enter description (optional)', placeHolder: 'What does this task do?', }); const group = await vscode.window.showQuickPick(['custom', 'build', 'test', 'npm', 'git'], { placeHolder: 'Select task group', }); const task = { id: `custom-${Date.now()}`, name, command, args: argsInput ? argsInput.split(' ') : [], description, group: group, icon: '$(terminal)', }; this.tasks.set(task.id, task); this.outputChannel.appendLine(`➕ Added custom task: ${name}`); const runNow = await vscode.window.showQuickPick(['Yes', 'No'], { placeHolder: 'Run this task now?', }); if (runNow === 'Yes') { await this.runTask(task); } } getTasks() { return Array.from(this.tasks.values()); } getTask(id) { return this.tasks.get(id); } removeTask(id) { const task = this.tasks.get(id); if (task && task.group === 'custom') { this.tasks.delete(id); this.outputChannel.appendLine(`➖ Removed custom task: ${task.name}`); return true; } return false; } isTaskRunning(id) { return this.runningTasks.has(id); } getRunningTasks() { return Array.from(this.runningTasks); } } exports.TaskRunner = TaskRunner; //# sourceMappingURL=task-runner.js.map