|
1 |
| -# cmdctr |
| 1 | +# Command Center |
2 | 2 |
|
3 |
| -To install dependencies: |
| 3 | +Command Center (`cmdctr`) is a TypeScript project that provides a simple and flexible way to create command line interfaces (CLI). It allows you to define tasks with specific options and actions, and then run these tasks from the command line. |
| 4 | + |
| 5 | +> [! WARNING] |
| 6 | +> This project is still in early development and is not ready for production use. |
| 7 | +
|
| 8 | +## Installation |
4 | 9 |
|
5 | 10 | ```bash
|
6 |
| -bun install |
| 11 | +bun i cmdctr |
| 12 | +# or |
| 13 | +npm i cmdctr |
7 | 14 | ```
|
8 | 15 |
|
9 |
| -To run: |
| 16 | +Or clone the repository: |
10 | 17 |
|
11 | 18 | ```bash
|
12 |
| -bun run index.ts |
| 19 | +git clone https://github.com/trvswgnr/cmdctr.git |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +Command Center provides three main functions: `CmdCtr`, `Data`, and `Task`. |
| 25 | + |
| 26 | +### CmdCtr |
| 27 | + |
| 28 | +`CmdCtr` creates a new command center. It takes a string argument which is the name of the base command. This is the command that will be used to run tasks. |
| 29 | + |
| 30 | +```ts |
| 31 | +import { CmdCtr } from "cmdctr"; |
| 32 | +const cmdCtr = CmdCtr("example"); |
13 | 33 | ```
|
| 34 | + |
| 35 | +### Data |
| 36 | + |
| 37 | +`Data` creates a new task data object. This object defines the name, description, and options for a task. |
| 38 | + |
| 39 | +```ts |
| 40 | +const task1Data = Data({ |
| 41 | + name: "task-1", |
| 42 | + description: "A task that does something", |
| 43 | + options: { |
| 44 | + input: { |
| 45 | + short: "i", |
| 46 | + type: "string", |
| 47 | + description: "The input file to be processed", |
| 48 | + required: true, |
| 49 | + }, |
| 50 | + output: { |
| 51 | + short: "o", |
| 52 | + type: "string", |
| 53 | + description: "The output file to be written", |
| 54 | + required: true, |
| 55 | + }, |
| 56 | + }, |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +### Task |
| 61 | + |
| 62 | +`Task` creates a new task. It takes a data object and an action function as arguments. The action function is what will be executed when the task is run. |
| 63 | + |
| 64 | +```ts |
| 65 | +const task1 = Task(task1Data, (opts) => { |
| 66 | + const { input, output } = opts; |
| 67 | + console.log(`input: ${input}`); |
| 68 | + console.log(`output: ${output}`); |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +### Registering and Running Tasks |
| 73 | + |
| 74 | +After creating tasks, you can register them to the command center using the `register` method. Then, you can run the tasks using the `run` method. |
| 75 | + |
| 76 | +```ts |
| 77 | +cmdCtr.register(task1); |
| 78 | +cmdCtr.register(task2); |
| 79 | +cmdCtr.run(); |
| 80 | +``` |
| 81 | + |
| 82 | +## Example |
| 83 | + |
| 84 | +Here is a complete example of how to use Command Center: |
| 85 | + |
| 86 | +```ts |
| 87 | +import { Task, Data, CmdCtr, withSpinner } from "cmdctr"; |
| 88 | + |
| 89 | +const cmdCtr = CmdCtr("example"); |
| 90 | + |
| 91 | +const task1Data = Data({ |
| 92 | + name: "task-1", |
| 93 | + description: "A task that does something", |
| 94 | + options: { |
| 95 | + input: { |
| 96 | + short: "i", |
| 97 | + type: "string", |
| 98 | + description: "The input file to be processed", |
| 99 | + required: true, |
| 100 | + }, |
| 101 | + output: { |
| 102 | + short: "o", |
| 103 | + type: "string", |
| 104 | + description: "The output file to be written", |
| 105 | + required: true, |
| 106 | + }, |
| 107 | + }, |
| 108 | +}); |
| 109 | + |
| 110 | +const task1 = Task(task1Data, (opts) => { |
| 111 | + const { input, output } = opts; |
| 112 | + console.log(`input: ${input}`); |
| 113 | + console.log(`output: ${output}`); |
| 114 | +}); |
| 115 | + |
| 116 | +const task2Data = Data({ |
| 117 | + name: "task-2", |
| 118 | + description: "A task that does something else", |
| 119 | + options: { |
| 120 | + message: { |
| 121 | + short: "m", |
| 122 | + type: "string", |
| 123 | + description: "The message to be printed", |
| 124 | + required: true, |
| 125 | + }, |
| 126 | + loud: { |
| 127 | + short: "l", |
| 128 | + type: "boolean", |
| 129 | + description: "Whether the message should be printed loudly", |
| 130 | + default: false, |
| 131 | + }, |
| 132 | + }, |
| 133 | +}); |
| 134 | + |
| 135 | +const task2 = Task(task2Data, async (opts) => { |
| 136 | + const { message, loud } = opts; |
| 137 | + const text = await withSpinner("thinking...", () => { |
| 138 | + return new Promise<string>((resolve) => { |
| 139 | + setTimeout(() => { |
| 140 | + resolve(`oh yeah, ${message}`); |
| 141 | + }, 2000); |
| 142 | + }); |
| 143 | + }); |
| 144 | + console.log(loud ? text.toUpperCase() : text); |
| 145 | +}); |
| 146 | + |
| 147 | +cmdCtr.register(task1); |
| 148 | +cmdCtr.register(task2); |
| 149 | +cmdCtr.run(); |
| 150 | +``` |
| 151 | + |
| 152 | +In this example, two tasks are created: `task-1` and `task-2`. `task-1` takes an input file and an output file as options, and `task-2` takes a message and a boolean flag as options. The tasks are then registered to the command center and run. |
| 153 | + |
| 154 | +Command Center also provides a utility function `withSpinner` that can be used to display a spinner in the console while a task is running. This is useful for tasks that may take some time to complete. |
| 155 | + |
| 156 | +## License |
| 157 | + |
| 158 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 159 | + |
| 160 | +## Contributing |
| 161 | + |
| 162 | +Contributions are welcome! Feel free to open an issue or submit a pull request. |
| 163 | + |
0 commit comments