@zfadhli/koko-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +206 -0
- package/dist/index.d.mts +161 -0
- package/dist/index.mjs +329 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Koko contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# koko
|
|
2
|
+
|
|
3
|
+
**Composition-based CLI toolkit** — wraps [cac](https://github.com/egoist/cac), [picocolors](https://github.com/alexeyraspopov/picocolors), [cli-progress](https://github.com/nicejade/cli-progress), and [cli-spinners](https://github.com/sindresorhus/cli-spinners) into a cohesive, function-based API.
|
|
4
|
+
|
|
5
|
+
## Design
|
|
6
|
+
|
|
7
|
+
koko follows a **function-based Composition API** (inspired by Vue 3):
|
|
8
|
+
|
|
9
|
+
- **Stateless utilities** (colors, CLI builder) are plain objects — no factory needed
|
|
10
|
+
- **Stateful widgets** (spinner, progress) are factory functions — closures, not classes
|
|
11
|
+
- **Progressive complexity** — `ctx.spinner()` is a one-liner; `import { createSpinner }` for standalone use
|
|
12
|
+
- **Minimal API surface** — 4 imports cover everything
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
bun add @zfadhli/koko-cli
|
|
18
|
+
# or
|
|
19
|
+
npm install @zfadhli/koko-cli
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { color, createSpinner, createProgress, createCLI } from "@zfadhli/koko-cli";
|
|
26
|
+
|
|
27
|
+
// Colors
|
|
28
|
+
console.log(color.red("error"));
|
|
29
|
+
console.log(color.bold(color.green("success")));
|
|
30
|
+
|
|
31
|
+
// Spinner
|
|
32
|
+
const spin = createSpinner("Loading...");
|
|
33
|
+
spin.start();
|
|
34
|
+
await someWork();
|
|
35
|
+
spin.succeed("Done!");
|
|
36
|
+
|
|
37
|
+
// Progress bar
|
|
38
|
+
const bar = createProgress({ total: 100 });
|
|
39
|
+
bar.update(50);
|
|
40
|
+
bar.stop();
|
|
41
|
+
|
|
42
|
+
// CLI app
|
|
43
|
+
const cli = createCLI("my-app", "1.0.0").description("My CLI");
|
|
44
|
+
cli.command("build <input>", "Build project", (cmd) => {
|
|
45
|
+
cmd.option("--out <dir>", "Output dir", { default: "dist" });
|
|
46
|
+
cmd.action(async (options, ctx) => {
|
|
47
|
+
const spin = ctx.spinner("Building...");
|
|
48
|
+
spin.start();
|
|
49
|
+
// ...
|
|
50
|
+
spin.succeed("Built!");
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
cli.parse();
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
### `color`
|
|
59
|
+
|
|
60
|
+
A plain object with all picocolors functions:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
color.red("text") // red text
|
|
64
|
+
color.green("text") // green text
|
|
65
|
+
color.bold("text") // bold text
|
|
66
|
+
color.dim("text") // dim text
|
|
67
|
+
color.italic("text") // italic
|
|
68
|
+
color.underline("text") // underline
|
|
69
|
+
// ... and 18 more (all bright variants, bg variants omitted for brevity)
|
|
70
|
+
|
|
71
|
+
// Nesting
|
|
72
|
+
color.bold(color.red("bold red text"))
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### `createSpinner(text?, options?)`
|
|
76
|
+
|
|
77
|
+
Creates a terminal spinner with start/stop lifecycle.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
const spin = createSpinner("Installing...");
|
|
81
|
+
spin.start();
|
|
82
|
+
// ... async work
|
|
83
|
+
spin.succeed("Installed!"); // ✔ Installed!
|
|
84
|
+
spin.fail("Failed!"); // ✘ Failed!
|
|
85
|
+
spin.warn("Caution!"); // ⚠ Caution!
|
|
86
|
+
spin.info("Note!"); // ℹ Note!
|
|
87
|
+
|
|
88
|
+
// Custom style
|
|
89
|
+
createSpinner("Loading", { style: "arc", color: "cyan" });
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Options:**
|
|
93
|
+
|
|
94
|
+
| Option | Type | Default |
|
|
95
|
+
|--------|------|---------|
|
|
96
|
+
| `text` | `string` | `""` |
|
|
97
|
+
| `style` | `SpinnerStyle` | `"dots"` |
|
|
98
|
+
| `color` | `ColorName` | — (no color) |
|
|
99
|
+
| `frames` | `string[]` | from style |
|
|
100
|
+
| `interval` | `number` | from style |
|
|
101
|
+
|
|
102
|
+
### `createProgress(options)`
|
|
103
|
+
|
|
104
|
+
Creates a terminal progress bar.
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const bar = createProgress({ total: 100 });
|
|
108
|
+
bar.update(50); // set exact value
|
|
109
|
+
bar.increment(10); // increment by delta
|
|
110
|
+
bar.stop();
|
|
111
|
+
|
|
112
|
+
// With custom format and payload
|
|
113
|
+
const bar = createProgress({
|
|
114
|
+
total: 10,
|
|
115
|
+
format: " {bar} {percentage}% | Installing {package}",
|
|
116
|
+
});
|
|
117
|
+
bar.increment(1, { package: "koko" });
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
**Options:**
|
|
121
|
+
|
|
122
|
+
| Option | Type | Default |
|
|
123
|
+
|--------|------|---------|
|
|
124
|
+
| `total` | `number` | **(required)** |
|
|
125
|
+
| `start` | `number` | `0` |
|
|
126
|
+
| `format` | `string` | rect preset |
|
|
127
|
+
| `barsize` | `number` | terminal width |
|
|
128
|
+
| `barCompleteChar` | `string` | `■` |
|
|
129
|
+
| `barIncompleteChar` | `string` | ` ` (space) |
|
|
130
|
+
| `clearOnComplete` | `boolean` | `false` |
|
|
131
|
+
| `stopOnComplete` | `boolean` | `false` |
|
|
132
|
+
|
|
133
|
+
### `createCLI(name, version?)`
|
|
134
|
+
|
|
135
|
+
Creates an opinionated CLI application builder (wraps cac).
|
|
136
|
+
|
|
137
|
+
- Auto-attaches `--help` and `--version`
|
|
138
|
+
- Action handlers receive `(options, ctx)` with typed options
|
|
139
|
+
- `ctx` provides `.spinner()`, `.progress()`, `.color`
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
const cli = createCLI("deploy", "1.0.0")
|
|
143
|
+
.description("Deployment tool");
|
|
144
|
+
|
|
145
|
+
cli.command("build <input>", "Build project", (cmd) => {
|
|
146
|
+
cmd.option("--out <dir>", "Output dir", { default: "dist" });
|
|
147
|
+
cmd.option("--prod", "Production mode");
|
|
148
|
+
cmd.action(async (options, ctx) => {
|
|
149
|
+
// options: { input: string; out: string; prod: boolean }
|
|
150
|
+
// ctx: { spinner, progress, color }
|
|
151
|
+
const spin = ctx.spinner("Compiling...");
|
|
152
|
+
spin.start();
|
|
153
|
+
await build(options.input);
|
|
154
|
+
spin.succeed("Built!");
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
cli.parse();
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Icons
|
|
162
|
+
|
|
163
|
+
Standardized icon constants:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { ICON_SUCCESS, ICON_ERROR, ICON_WARN, ICON_INFO } from "@zfadhli/koko-cli";
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
| Constant | Character | Description |
|
|
170
|
+
|----------|-----------|-------------|
|
|
171
|
+
| `ICON_SUCCESS` | `✔` | Success / complete |
|
|
172
|
+
| `ICON_ERROR` | `✘` | Error / failure |
|
|
173
|
+
| `ICON_WARN` | `⚠` | Warning |
|
|
174
|
+
| `ICON_INFO` | `ℹ` | Info |
|
|
175
|
+
|
|
176
|
+
### Errors
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
import { CliToolkitError } from "@zfadhli/koko-cli";
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
createProgress({ total: 0 });
|
|
183
|
+
} catch (err) {
|
|
184
|
+
if (err instanceof CliToolkitError) {
|
|
185
|
+
console.error(err.message); // "total must be > 0, got 0"
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Examples
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
# Run any example
|
|
194
|
+
bun run examples/01-color.ts # all color functions
|
|
195
|
+
bun run examples/02-spinner.ts # spinner lifecycle + styles
|
|
196
|
+
bun run examples/03-progress.ts # progress bars + payloads
|
|
197
|
+
bun run examples/04-cli-app.ts # full CLI app with ctx
|
|
198
|
+
bun run examples/05-composition.ts # real-world patterns
|
|
199
|
+
|
|
200
|
+
# All at once
|
|
201
|
+
bun run examples
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## License
|
|
205
|
+
|
|
206
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
//#region src/color.d.ts
|
|
2
|
+
type ColorName = "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "blackBright" | "redBright" | "greenBright" | "yellowBright" | "blueBright" | "magentaBright" | "cyanBright" | "whiteBright";
|
|
3
|
+
type StyleName = "reset" | "bold" | "dim" | "italic" | "underline" | "inverse" | "hidden" | "strikethrough";
|
|
4
|
+
type ColorFunctions = { [K in ColorName | StyleName]: (text: string) => string };
|
|
5
|
+
/**
|
|
6
|
+
* Minimal color palette — a plain object wrapping picocolors.
|
|
7
|
+
*
|
|
8
|
+
* Stateless, no factory needed. Import and use:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { color } from 'koko'
|
|
12
|
+
* color.red('error')
|
|
13
|
+
* color.bold(color.green('success'))
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare const color: ColorFunctions;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/types.d.ts
|
|
19
|
+
interface ProgressOptions {
|
|
20
|
+
total: number;
|
|
21
|
+
start?: number;
|
|
22
|
+
format?: string;
|
|
23
|
+
barCompleteChar?: string;
|
|
24
|
+
barIncompleteChar?: string;
|
|
25
|
+
barsize?: number;
|
|
26
|
+
clearOnComplete?: boolean;
|
|
27
|
+
stopOnComplete?: boolean;
|
|
28
|
+
}
|
|
29
|
+
interface ProgressInstance {
|
|
30
|
+
update(current: number, payload?: Record<string, unknown>): void;
|
|
31
|
+
increment(delta?: number, payload?: Record<string, unknown>): void;
|
|
32
|
+
stop(): void;
|
|
33
|
+
readonly total: number;
|
|
34
|
+
readonly value: number;
|
|
35
|
+
}
|
|
36
|
+
type SpinnerStyle = "dots" | "dots2" | "line" | "arc" | "bouncingBar" | "clock" | "moon" | "toggle" | "arrow" | "shark";
|
|
37
|
+
interface SpinnerOptions {
|
|
38
|
+
text?: string;
|
|
39
|
+
style?: SpinnerStyle;
|
|
40
|
+
color?: ColorName;
|
|
41
|
+
frames?: string[];
|
|
42
|
+
interval?: number;
|
|
43
|
+
}
|
|
44
|
+
interface SpinnerInstance {
|
|
45
|
+
start(text?: string): void;
|
|
46
|
+
stop(finalText?: string): void;
|
|
47
|
+
succeed(text?: string): void;
|
|
48
|
+
fail(text?: string): void;
|
|
49
|
+
warn(text?: string): void;
|
|
50
|
+
info(text?: string): void;
|
|
51
|
+
text: string;
|
|
52
|
+
isSpinning: boolean;
|
|
53
|
+
}
|
|
54
|
+
type CLIAction<T = Record<string, unknown>> = (options: T, ctx: CommandContext) => void | Promise<void>;
|
|
55
|
+
interface CommandContext {
|
|
56
|
+
spinner(text?: string): SpinnerInstance;
|
|
57
|
+
progress(options: ProgressOptions): ProgressInstance;
|
|
58
|
+
color: { [K in ColorName | StyleName]: (text: string) => string };
|
|
59
|
+
}
|
|
60
|
+
interface OptionConfig {
|
|
61
|
+
default?: unknown;
|
|
62
|
+
required?: boolean;
|
|
63
|
+
}
|
|
64
|
+
interface CommandBuilder {
|
|
65
|
+
option(name: string, description?: string, config?: OptionConfig): CommandBuilder;
|
|
66
|
+
alias(name: string): CommandBuilder;
|
|
67
|
+
action<T>(handler: CLIAction<T>): void;
|
|
68
|
+
}
|
|
69
|
+
type CommandSetup = (cmd: CommandBuilder) => void;
|
|
70
|
+
interface CLIBuilder {
|
|
71
|
+
command(name: string, description: string, setup: CommandSetup): CLIBuilder;
|
|
72
|
+
description(text: string): CLIBuilder;
|
|
73
|
+
parse(argv?: string[]): void;
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/cli.d.ts
|
|
77
|
+
/**
|
|
78
|
+
* Create a CLI application with an opinionated builder API.
|
|
79
|
+
*
|
|
80
|
+
* Auto-attaches `--help` and `--version`. Each command's action handler
|
|
81
|
+
* receives a typed options object and a `ctx` with built-in access to
|
|
82
|
+
* spinner, progress bar, and colors.
|
|
83
|
+
*
|
|
84
|
+
* ```ts
|
|
85
|
+
* const cli = createCLI('my-app', '1.0.0').description('My CLI')
|
|
86
|
+
*
|
|
87
|
+
* cli.command('build <input>', 'Build the project', (cmd) => {
|
|
88
|
+
* cmd.option('--out <dir>', 'Output dir', { default: 'dist' })
|
|
89
|
+
* cmd.option('--prod', 'Production mode')
|
|
90
|
+
* cmd.action(async (options, ctx) => {
|
|
91
|
+
* const spin = ctx.spinner('Building...')
|
|
92
|
+
* spin.start()
|
|
93
|
+
* // ...
|
|
94
|
+
* spin.succeed('Built!')
|
|
95
|
+
* })
|
|
96
|
+
* })
|
|
97
|
+
*
|
|
98
|
+
* cli.parse()
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
declare function createCLI(name: string, version?: string): CLIBuilder;
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/errors.d.ts
|
|
104
|
+
declare class CliToolkitError extends Error {
|
|
105
|
+
constructor(message: string);
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/icons.d.ts
|
|
109
|
+
/**
|
|
110
|
+
* Standardized terminal icons.
|
|
111
|
+
*
|
|
112
|
+
* Single source of truth for all status symbols used across koko.
|
|
113
|
+
* Import these instead of hardcoding Unicode characters.
|
|
114
|
+
*
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { ICON_SUCCESS, ICON_ERROR } from "koko"
|
|
117
|
+
* console.log(`${ICON_SUCCESS} Build complete`)
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
/** ✔ — success / complete */
|
|
121
|
+
declare const ICON_SUCCESS = "\u2714 ";
|
|
122
|
+
/** ✘ — error / failure */
|
|
123
|
+
declare const ICON_ERROR = "\u2718 ";
|
|
124
|
+
/** ⚠ — warning */
|
|
125
|
+
declare const ICON_WARN = "\u26A0 ";
|
|
126
|
+
/** ℹ — info */
|
|
127
|
+
declare const ICON_INFO = "\u2139 ";
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/progress.d.ts
|
|
130
|
+
/**
|
|
131
|
+
* Create a terminal progress bar.
|
|
132
|
+
*
|
|
133
|
+
* ```ts
|
|
134
|
+
* const bar = createProgress({ total: 100 })
|
|
135
|
+
* bar.update(50)
|
|
136
|
+
* // ...
|
|
137
|
+
* bar.stop()
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* Returns a {@link ProgressInstance} wrapping cli-progress's `SingleBar`.
|
|
141
|
+
* Throws {@link CliToolkitError} if `total` is missing or <= 0.
|
|
142
|
+
*/
|
|
143
|
+
declare function createProgress(options: ProgressOptions): ProgressInstance;
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/spinner.d.ts
|
|
146
|
+
/**
|
|
147
|
+
* Create a terminal spinner.
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* const spin = createSpinner('Installing...')
|
|
151
|
+
* spin.start()
|
|
152
|
+
* // ... async work
|
|
153
|
+
* spin.succeed('Installed!')
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* Returns a {@link SpinnerInstance} with start/stop lifecycle
|
|
157
|
+
* and convenience methods (succeed, fail, warn, info).
|
|
158
|
+
*/
|
|
159
|
+
declare function createSpinner(text?: string, options?: SpinnerOptions): SpinnerInstance;
|
|
160
|
+
//#endregion
|
|
161
|
+
export { type CLIAction, type CLIBuilder, CliToolkitError, type ColorName, type CommandBuilder, type CommandContext, type CommandSetup, ICON_ERROR, ICON_INFO, ICON_SUCCESS, ICON_WARN, type OptionConfig, type ProgressInstance, type ProgressOptions, type SpinnerInstance, type SpinnerOptions, type SpinnerStyle, type StyleName, color, createCLI, createProgress, createSpinner };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import cac from "cac";
|
|
2
|
+
import pc from "picocolors";
|
|
3
|
+
import cliProgress from "cli-progress";
|
|
4
|
+
import process$1 from "node:process";
|
|
5
|
+
import spinners from "cli-spinners";
|
|
6
|
+
//#region src/color.ts
|
|
7
|
+
/**
|
|
8
|
+
* Minimal color palette — a plain object wrapping picocolors.
|
|
9
|
+
*
|
|
10
|
+
* Stateless, no factory needed. Import and use:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { color } from 'koko'
|
|
14
|
+
* color.red('error')
|
|
15
|
+
* color.bold(color.green('success'))
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
const color = {
|
|
19
|
+
reset: pc.reset,
|
|
20
|
+
bold: pc.bold,
|
|
21
|
+
dim: pc.dim,
|
|
22
|
+
italic: pc.italic,
|
|
23
|
+
underline: pc.underline,
|
|
24
|
+
inverse: pc.inverse,
|
|
25
|
+
hidden: pc.hidden,
|
|
26
|
+
strikethrough: pc.strikethrough,
|
|
27
|
+
black: pc.black,
|
|
28
|
+
red: pc.red,
|
|
29
|
+
green: pc.green,
|
|
30
|
+
yellow: pc.yellow,
|
|
31
|
+
blue: pc.blue,
|
|
32
|
+
magenta: pc.magenta,
|
|
33
|
+
cyan: pc.cyan,
|
|
34
|
+
white: pc.white,
|
|
35
|
+
gray: pc.gray,
|
|
36
|
+
blackBright: pc.blackBright,
|
|
37
|
+
redBright: pc.redBright,
|
|
38
|
+
greenBright: pc.greenBright,
|
|
39
|
+
yellowBright: pc.yellowBright,
|
|
40
|
+
blueBright: pc.blueBright,
|
|
41
|
+
magentaBright: pc.magentaBright,
|
|
42
|
+
cyanBright: pc.cyanBright,
|
|
43
|
+
whiteBright: pc.whiteBright
|
|
44
|
+
};
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/errors.ts
|
|
47
|
+
var CliToolkitError = class extends Error {
|
|
48
|
+
constructor(message) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = "CliToolkitError";
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/progress.ts
|
|
55
|
+
/**
|
|
56
|
+
* Create a terminal progress bar.
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* const bar = createProgress({ total: 100 })
|
|
60
|
+
* bar.update(50)
|
|
61
|
+
* // ...
|
|
62
|
+
* bar.stop()
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* Returns a {@link ProgressInstance} wrapping cli-progress's `SingleBar`.
|
|
66
|
+
* Throws {@link CliToolkitError} if `total` is missing or <= 0.
|
|
67
|
+
*/
|
|
68
|
+
function createProgress(options) {
|
|
69
|
+
if (options.total <= 0) throw new CliToolkitError(`Progress total must be > 0, got ${options.total}`);
|
|
70
|
+
const bar = new cliProgress.SingleBar({
|
|
71
|
+
format: options.format,
|
|
72
|
+
barCompleteChar: options.barCompleteChar,
|
|
73
|
+
barIncompleteChar: options.barIncompleteChar,
|
|
74
|
+
barsize: options.barsize,
|
|
75
|
+
clearOnComplete: options.clearOnComplete,
|
|
76
|
+
stopOnComplete: options.stopOnComplete
|
|
77
|
+
}, cliProgress.Presets.rect);
|
|
78
|
+
let started = false;
|
|
79
|
+
let currentValue = options.start ?? 0;
|
|
80
|
+
function ensureStarted() {
|
|
81
|
+
if (!started) {
|
|
82
|
+
bar.start(options.total, currentValue);
|
|
83
|
+
started = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
get total() {
|
|
88
|
+
return options.total;
|
|
89
|
+
},
|
|
90
|
+
get value() {
|
|
91
|
+
return currentValue;
|
|
92
|
+
},
|
|
93
|
+
update(current, payload) {
|
|
94
|
+
currentValue = current;
|
|
95
|
+
ensureStarted();
|
|
96
|
+
bar.update(current, payload);
|
|
97
|
+
},
|
|
98
|
+
increment(delta, payload) {
|
|
99
|
+
ensureStarted();
|
|
100
|
+
currentValue += delta ?? 1;
|
|
101
|
+
bar.update(currentValue, payload);
|
|
102
|
+
},
|
|
103
|
+
stop() {
|
|
104
|
+
if (started) {
|
|
105
|
+
bar.stop();
|
|
106
|
+
started = false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/icons.ts
|
|
113
|
+
/**
|
|
114
|
+
* Standardized terminal icons.
|
|
115
|
+
*
|
|
116
|
+
* Single source of truth for all status symbols used across koko.
|
|
117
|
+
* Import these instead of hardcoding Unicode characters.
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { ICON_SUCCESS, ICON_ERROR } from "koko"
|
|
121
|
+
* console.log(`${ICON_SUCCESS} Build complete`)
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
/** ✔ — success / complete */
|
|
125
|
+
const ICON_SUCCESS = "✔ ";
|
|
126
|
+
/** ✘ — error / failure */
|
|
127
|
+
const ICON_ERROR = "✘ ";
|
|
128
|
+
/** ⚠ — warning */
|
|
129
|
+
const ICON_WARN = "⚠ ";
|
|
130
|
+
/** ℹ — info */
|
|
131
|
+
const ICON_INFO = "ℹ ";
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/spinner.ts
|
|
134
|
+
const frameSets = {
|
|
135
|
+
dots: spinners.dots,
|
|
136
|
+
dots2: spinners.dots2,
|
|
137
|
+
line: spinners.line,
|
|
138
|
+
arc: spinners.arc,
|
|
139
|
+
bouncingBar: spinners.bouncingBar,
|
|
140
|
+
clock: spinners.clock,
|
|
141
|
+
moon: spinners.moon,
|
|
142
|
+
toggle: spinners.toggle12,
|
|
143
|
+
arrow: spinners.arrow3,
|
|
144
|
+
shark: spinners.shark
|
|
145
|
+
};
|
|
146
|
+
const defaultStyle = "dots";
|
|
147
|
+
/**
|
|
148
|
+
* Create a terminal spinner.
|
|
149
|
+
*
|
|
150
|
+
* ```ts
|
|
151
|
+
* const spin = createSpinner('Installing...')
|
|
152
|
+
* spin.start()
|
|
153
|
+
* // ... async work
|
|
154
|
+
* spin.succeed('Installed!')
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* Returns a {@link SpinnerInstance} with start/stop lifecycle
|
|
158
|
+
* and convenience methods (succeed, fail, warn, info).
|
|
159
|
+
*/
|
|
160
|
+
function createSpinner(text, options) {
|
|
161
|
+
const { text: initialText = text ?? "", style = defaultStyle, color: spinnerColor, frames: customFrames, interval: customInterval } = options ?? {};
|
|
162
|
+
const frameSet = frameSets[style];
|
|
163
|
+
if (!frameSet && !customFrames) throw new CliToolkitError(`Unknown spinner style: "${style}". Available styles: ${Object.keys(frameSets).join(", ")}`);
|
|
164
|
+
const frames = customFrames ?? frameSet.frames;
|
|
165
|
+
const interval = customInterval ?? frameSet.interval;
|
|
166
|
+
let timer = null;
|
|
167
|
+
let index = 0;
|
|
168
|
+
let currentText = initialText;
|
|
169
|
+
let spinning = false;
|
|
170
|
+
function write(frame) {
|
|
171
|
+
const coloredFrame = spinnerColor ? color[spinnerColor](frame) : frame;
|
|
172
|
+
const line = currentText ? `${coloredFrame} ${currentText}` : coloredFrame;
|
|
173
|
+
process$1.stdout.write(`\r${line}\x1b[K`);
|
|
174
|
+
}
|
|
175
|
+
function clear() {
|
|
176
|
+
process$1.stdout.write("\r\x1B[K");
|
|
177
|
+
}
|
|
178
|
+
function start(newText) {
|
|
179
|
+
if (spinning) return;
|
|
180
|
+
if (newText !== void 0) currentText = newText;
|
|
181
|
+
spinning = true;
|
|
182
|
+
index = 0;
|
|
183
|
+
clear();
|
|
184
|
+
write(frames[0]);
|
|
185
|
+
timer = setInterval(() => {
|
|
186
|
+
index = (index + 1) % frames.length;
|
|
187
|
+
write(frames[index]);
|
|
188
|
+
}, interval);
|
|
189
|
+
}
|
|
190
|
+
function stop(finalText) {
|
|
191
|
+
if (!spinning) return;
|
|
192
|
+
spinning = false;
|
|
193
|
+
if (timer) {
|
|
194
|
+
clearInterval(timer);
|
|
195
|
+
timer = null;
|
|
196
|
+
}
|
|
197
|
+
if (finalText !== void 0) {
|
|
198
|
+
clear();
|
|
199
|
+
process$1.stdout.write(`${finalText}\n`);
|
|
200
|
+
} else clear();
|
|
201
|
+
}
|
|
202
|
+
function succeed(text) {
|
|
203
|
+
const t = text ?? currentText;
|
|
204
|
+
stop(`${color.green(ICON_SUCCESS)} ${t}`);
|
|
205
|
+
}
|
|
206
|
+
function fail(text) {
|
|
207
|
+
const t = text ?? currentText;
|
|
208
|
+
stop(`${color.red(ICON_ERROR)} ${t}`);
|
|
209
|
+
}
|
|
210
|
+
function warn(text) {
|
|
211
|
+
const t = text ?? currentText;
|
|
212
|
+
stop(`${color.yellow(ICON_WARN)} ${t}`);
|
|
213
|
+
}
|
|
214
|
+
function info(text) {
|
|
215
|
+
const t = text ?? currentText;
|
|
216
|
+
stop(`${color.blue(ICON_INFO)} ${t}`);
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
get text() {
|
|
220
|
+
return currentText;
|
|
221
|
+
},
|
|
222
|
+
set text(val) {
|
|
223
|
+
currentText = val;
|
|
224
|
+
},
|
|
225
|
+
get isSpinning() {
|
|
226
|
+
return spinning;
|
|
227
|
+
},
|
|
228
|
+
start,
|
|
229
|
+
stop,
|
|
230
|
+
succeed,
|
|
231
|
+
fail,
|
|
232
|
+
warn,
|
|
233
|
+
info
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/cli.ts
|
|
238
|
+
/**
|
|
239
|
+
* Parse positional argument names from a CAC command pattern.
|
|
240
|
+
*
|
|
241
|
+
* `'build <input> [output]'` → `['input', 'output']`
|
|
242
|
+
*/
|
|
243
|
+
function parsePositionalNames(rawName) {
|
|
244
|
+
const names = [];
|
|
245
|
+
const re = /[<[](\w+)[>\]]/g;
|
|
246
|
+
for (let match = re.exec(rawName); match; match = re.exec(rawName)) names.push(match[1]);
|
|
247
|
+
return names;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Create a CLI application with an opinionated builder API.
|
|
251
|
+
*
|
|
252
|
+
* Auto-attaches `--help` and `--version`. Each command's action handler
|
|
253
|
+
* receives a typed options object and a `ctx` with built-in access to
|
|
254
|
+
* spinner, progress bar, and colors.
|
|
255
|
+
*
|
|
256
|
+
* ```ts
|
|
257
|
+
* const cli = createCLI('my-app', '1.0.0').description('My CLI')
|
|
258
|
+
*
|
|
259
|
+
* cli.command('build <input>', 'Build the project', (cmd) => {
|
|
260
|
+
* cmd.option('--out <dir>', 'Output dir', { default: 'dist' })
|
|
261
|
+
* cmd.option('--prod', 'Production mode')
|
|
262
|
+
* cmd.action(async (options, ctx) => {
|
|
263
|
+
* const spin = ctx.spinner('Building...')
|
|
264
|
+
* spin.start()
|
|
265
|
+
* // ...
|
|
266
|
+
* spin.succeed('Built!')
|
|
267
|
+
* })
|
|
268
|
+
* })
|
|
269
|
+
*
|
|
270
|
+
* cli.parse()
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
function createCLI(name, version) {
|
|
274
|
+
const cli = cac(name);
|
|
275
|
+
if (version) cli.version(version);
|
|
276
|
+
cli.help();
|
|
277
|
+
function createContext() {
|
|
278
|
+
return {
|
|
279
|
+
color,
|
|
280
|
+
spinner(text) {
|
|
281
|
+
return createSpinner(text);
|
|
282
|
+
},
|
|
283
|
+
progress(options) {
|
|
284
|
+
return createProgress(options);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
const builder = {
|
|
289
|
+
description(text) {
|
|
290
|
+
cli.usage(text);
|
|
291
|
+
return builder;
|
|
292
|
+
},
|
|
293
|
+
command(rawName, description, setup) {
|
|
294
|
+
const positionalNames = parsePositionalNames(rawName);
|
|
295
|
+
const rawCmd = cli.command(rawName, description);
|
|
296
|
+
const cmdBuilder = {
|
|
297
|
+
option(name, desc, config) {
|
|
298
|
+
rawCmd.option(name, desc ?? "", config);
|
|
299
|
+
return cmdBuilder;
|
|
300
|
+
},
|
|
301
|
+
alias(name) {
|
|
302
|
+
rawCmd.alias(name);
|
|
303
|
+
return cmdBuilder;
|
|
304
|
+
},
|
|
305
|
+
action(handler) {
|
|
306
|
+
rawCmd.action((...args) => {
|
|
307
|
+
const options = args[args.length - 1];
|
|
308
|
+
for (let i = 0; i < positionalNames.length; i++) options[positionalNames[i]] = args[i];
|
|
309
|
+
return handler(options, createContext());
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
setup(cmdBuilder);
|
|
314
|
+
return builder;
|
|
315
|
+
},
|
|
316
|
+
parse(argv) {
|
|
317
|
+
try {
|
|
318
|
+
if (argv) cli.parse(argv);
|
|
319
|
+
else cli.parse(process.argv);
|
|
320
|
+
} catch (err) {
|
|
321
|
+
if (err instanceof Error && err.name === "CACError") throw new CliToolkitError(err.message);
|
|
322
|
+
throw err;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
return builder;
|
|
327
|
+
}
|
|
328
|
+
//#endregion
|
|
329
|
+
export { CliToolkitError, ICON_ERROR, ICON_INFO, ICON_SUCCESS, ICON_WARN, color, createCLI, createProgress, createSpinner };
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zfadhli/koko-cli",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Composition-based CLI toolkit — wraps cac, picocolors, cli-progress, cli-spinners",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Koko contributors",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/zfadhli/koko-cli.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/zfadhli/koko-cli/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/zfadhli/koko-cli#readme",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./dist/index.mjs",
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.mjs",
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsdown",
|
|
30
|
+
"test": "bun test",
|
|
31
|
+
"lint": "biome check src tests",
|
|
32
|
+
"format": "biome format --write src tests",
|
|
33
|
+
"typecheck": "tsc --noEmit",
|
|
34
|
+
"example:color": "bun run examples/01-color.ts",
|
|
35
|
+
"example:spinner": "bun run examples/02-spinner.ts",
|
|
36
|
+
"example:progress": "bun run examples/03-progress.ts",
|
|
37
|
+
"example:cli": "bun run examples/04-cli-app.ts",
|
|
38
|
+
"example:composition": "bun run examples/05-composition.ts",
|
|
39
|
+
"examples": "bun run examples/01-color.ts && bun run examples/02-spinner.ts && bun run examples/03-progress.ts && bun run examples/04-cli-app.ts --help && bun run examples/05-composition.ts"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"cac": "^7.0.0",
|
|
43
|
+
"cli-progress": "^3.12.0",
|
|
44
|
+
"cli-spinners": "^3.4.0",
|
|
45
|
+
"picocolors": "^1.1.1"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@biomejs/biome": "^2.4.16",
|
|
49
|
+
"@types/bun": "^1.3.14",
|
|
50
|
+
"@types/cli-progress": "^3.11.0",
|
|
51
|
+
"tsdown": "^0.22.2",
|
|
52
|
+
"typescript": "^6.0.3"
|
|
53
|
+
}
|
|
54
|
+
}
|