@qodalis/cli-todo
Version:
A command-line tool for managing your tasks efficiently. Add, list, complete, and remove TODO items with simple commands.
221 lines (210 loc) • 10.4 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('@qodalis/cli-core'), require('@angular/core')) :
typeof define === 'function' && define.amd ? define(['@qodalis/cli-core', '@angular/core'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.cliCore, global.ngCore));
})(this, (function (cliCore, core) { 'use strict';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
// Automatically generated during build
const LIBRARY_VERSION = '0.0.4';
let CliTodoCommandProcessor = 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 = cliCore.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(cliCore.CliIcon.CheckIcon, cliCore.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');
}
};
CliTodoCommandProcessor = __decorate([
core.Injectable()
], CliTodoCommandProcessor);
const module = {
name: '@qodalis/cli-todo',
processors: [new CliTodoCommandProcessor()],
};
cliCore.bootUmdModule(module);
}));