UNPKG

@qodalis/cli-todo

Version:

A command-line tool for managing your tasks efficiently. Add, list, complete, and remove TODO items with simple commands.

211 lines (204 loc) 9.17 kB
import * as i0 from '@angular/core'; import { Injectable, NgModule } from '@angular/core'; import { resolveCommandProcessorProvider } from '@qodalis/angular-cli'; import { DefaultLibraryAuthor, CliIcon, CliForegroundColor } from '@qodalis/cli-core'; // Automatically generated during build const LIBRARY_VERSION = '0.0.4'; class CliTodoCommandProcessor { constructor() { this.command = 'todo'; this.description = 'A command-line tool for managing your tasks efficiently. Add, list, complete, and remove TODO items with simple commands.'; this.author = DefaultLibraryAuthor; this.version = LIBRARY_VERSION; this.processors = []; this.metadata = { icon: '📝', }; this.stateConfiguration = { initialState: { todos: this.loadFromOldStorage() ?? [], }, }; this.todos = []; this.nextId = 1; this.registerSubProcessors(); } async processCommand(command, context) { context.executor.showHelp(command, context); } writeDescription(context) { context.writer.writeln(this.description); context.writer.writeln(); context.writer.writeln('Usage:'); context.writer.writeln(' todo <command> [options]'); context.writer.writeln(); context.writer.writeln('Commands:'); context.writer.writeln(' ls List all TODO items'); context.writer.writeln(' add <text> Add a new TODO item with the given text'); context.writer.writeln(' rm <id> Remove a TODO item by its ID'); context.writer.writeln(' complete <id> Mark a TODO item as completed by its ID'); context.writer.writeln(); context.writer.writeln('Examples:'); context.writer.writeln(' todo add Buy milk'); context.writer.writeln(' todo ls'); context.writer.writeln(' todo complete 1'); context.writer.writeln(' todo rm 2'); context.writer.writeln(); } async initialize(context) { context.state .select((x) => x['todos']) .subscribe((todos) => { this.todos = todos; this.nextId = this.todos.length > 0 ? Math.max(...this.todos.map((t) => t.id)) + 1 : 1; }); } lineThroughText(text) { return text .split('') .map((char) => char + '\u0336') .join(''); } registerSubProcessors() { this.processors = [ { command: 'ls', description: 'List all TODO items', processCommand: async (_, context) => { if (this.todos.length === 0) { context.writer.writeWarning('No TODO items found.'); context.writer.writeln('Use "todo add <text>" to add a new TODO item.'); context.process.output(JSON.stringify(this.todos)); return; } this.todos.forEach((todo) => { context.writer.writeln(`[${todo.completed ? context.writer.wrapInColor(CliIcon.CheckIcon, CliForegroundColor.Green) : ' '}] #${todo.id} - ${todo.text}`); }); context.process.output(this.todos); }, }, { command: 'add', description: 'Add a new TODO item', allowUnlistedCommands: true, valueRequired: true, processCommand: async (command, context) => { const text = command.value; if (!text) { context.writer.writeError('Please provide a TODO description.'); return; } const newItem = { id: this.nextId++, text, completed: false, }; this.todos.push(newItem); await this.saveToStorage(context); context.writer.writeSuccess(`Added TODO: "${text}"`); context.process.output(newItem.id.toString()); }, }, { command: 'rm', description: 'Remove a TODO item by ID', allowUnlistedCommands: true, parameters: [ { name: 'all', description: 'Remove all TODO items', type: 'boolean', defaultValue: false, aliases: ['a'], required: false, }, ], processCommand: async (command, context) => { if (command.args['all'] || command.args['a']) { this.todos = []; await this.saveToStorage(context); context.writer.writeSuccess('Removed all TODO items.'); return; } const id = parseInt(command.value, 10); if (isNaN(id)) { context.writer.writeError('Please provide a valid TODO ID.'); return; } const index = this.todos.findIndex((todo) => todo.id === id); if (index === -1) { context.writer.writeInfo(`TODO item with ID ${id} not found.`); return; } this.todos.splice(index, 1); await this.saveToStorage(context); context.writer.writeSuccess(`Removed TODO item with ID ${id}.`); }, }, { command: 'complete', description: 'Mark a TODO item as completed by ID', allowUnlistedCommands: true, valueRequired: true, processCommand: async (command, context) => { const id = parseInt(command.value || '', 10); if (isNaN(id)) { context.writer.writeln('Please provide a valid TODO ID.'); return; } const todo = this.todos.find((todo) => todo.id === id); if (!todo) { context.writer.writeError(`TODO item with ID ${id} not found.`); return; } if (todo.completed) { context.writer.writeInfo(`TODO item with ID ${id} is already completed.`); return; } todo.completed = true; await this.saveToStorage(context); context.writer.writeSuccess(`Marked TODO item with ID ${id} as completed.`); }, }, ]; } loadFromOldStorage() { const data = localStorage.getItem('todo-items'); return data ? JSON.parse(data) : []; } async saveToStorage(context) { context.state.updateState({ todos: this.todos }); await context.state.persist(); localStorage.removeItem('todo-items'); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CliTodoCommandProcessor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CliTodoCommandProcessor }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CliTodoCommandProcessor, decorators: [{ type: Injectable }], ctorParameters: function () { return []; } }); class CliTodoModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CliTodoModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: CliTodoModule }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CliTodoModule, providers: [resolveCommandProcessorProvider(CliTodoCommandProcessor)] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CliTodoModule, decorators: [{ type: NgModule, args: [{ declarations: [], imports: [], exports: [], providers: [resolveCommandProcessorProvider(CliTodoCommandProcessor)], }] }] }); /* * Public API Surface of string */ /** * Generated bundle index. Do not edit. */ export { CliTodoCommandProcessor, CliTodoModule }; //# sourceMappingURL=qodalis-cli-todo.mjs.map