|
| 1 | +--- |
| 2 | +name: constructive-cli |
| 3 | +description: Build interactive CLI tools with the Constructive CLI SDK. Use when asked to "create a CLI", "build a command-line tool", "add CLI prompts", "create interactive prompts", "store CLI config", "add terminal colors", or when building any CLI application in a Constructive project. This package provides runtime utilities for type coercion, config management, display formatting, and command handler patterns used by generated and custom CLIs. |
| 4 | +compatibility: inquirerer, appstash, yanse, Node.js 18+, TypeScript |
| 5 | +metadata: |
| 6 | + author: constructive-io |
| 7 | + version: "0.1.0" |
| 8 | +--- |
| 9 | + |
| 10 | +# Constructive CLI SDK |
| 11 | + |
| 12 | +Runtime utilities for building interactive command-line interfaces using Constructive's CLI toolkit: **inquirerer** for prompts and argument parsing, **appstash** for persistent storage, and **yanse** for terminal colors. |
| 13 | + |
| 14 | +## When to Apply |
| 15 | + |
| 16 | +- Creating a new CLI tool in a Constructive project |
| 17 | +- Adding interactive prompts or argument parsing to a command |
| 18 | +- Managing persistent CLI configuration (contexts, credentials, settings) |
| 19 | +- Formatting CLI output with colors, tables, or key-value displays |
| 20 | +- Coercing CLI string arguments to proper GraphQL types |
| 21 | +- Building nested subcommand structures (e.g. `cli context create`) |
| 22 | +- Working with generated CLI code from `@constructive-io/graphql-codegen` |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +```bash |
| 27 | +pnpm add @constructive-sdk/cli |
| 28 | +``` |
| 29 | + |
| 30 | +## Quick Start |
| 31 | + |
| 32 | +### Creating a CLI with Commands |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { CLI } from 'inquirerer'; |
| 36 | +import { buildCommands, CommandHandler } from '@constructive-sdk/cli'; |
| 37 | + |
| 38 | +const hello: CommandHandler = async (argv, prompter, _options) => { |
| 39 | + const answers = await prompter.prompt(argv, [ |
| 40 | + { type: 'text', name: 'name', message: 'Your name' } |
| 41 | + ]); |
| 42 | + console.log(`Hello, ${answers.name}!`); |
| 43 | +}; |
| 44 | + |
| 45 | +const commands = buildCommands([ |
| 46 | + { name: 'hello', handler: hello, usage: 'Say hello' } |
| 47 | +]); |
| 48 | + |
| 49 | +const app = new CLI(commands); |
| 50 | +app.run(); |
| 51 | +``` |
| 52 | + |
| 53 | +### Config Management with appstash |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { getConfigStore } from '@constructive-sdk/cli'; |
| 57 | + |
| 58 | +const store = getConfigStore('my-tool'); |
| 59 | + |
| 60 | +// Create and manage contexts |
| 61 | +store.createContext('production', { endpoint: 'https://api.example.com/graphql' }); |
| 62 | +store.setCurrentContext('production'); |
| 63 | + |
| 64 | +// Store credentials |
| 65 | +store.setCredentials('production', { token: 'bearer-token-here' }); |
| 66 | + |
| 67 | +// Load current context |
| 68 | +const ctx = store.getCurrentContext(); |
| 69 | +``` |
| 70 | + |
| 71 | +### Type Coercion for CLI Arguments |
| 72 | + |
| 73 | +```typescript |
| 74 | +import { coerceAnswers, stripUndefined, FieldSchema } from '@constructive-sdk/cli'; |
| 75 | + |
| 76 | +const schema: FieldSchema = { |
| 77 | + name: 'string', |
| 78 | + age: 'int', |
| 79 | + active: 'boolean', |
| 80 | + metadata: 'json' |
| 81 | +}; |
| 82 | + |
| 83 | +// CLI args arrive as strings from minimist |
| 84 | +const rawArgs = { name: 'Alice', age: '30', active: 'true', metadata: '{"role":"admin"}' }; |
| 85 | + |
| 86 | +// Coerce to proper types |
| 87 | +const typed = coerceAnswers(rawArgs, schema); |
| 88 | +// { name: 'Alice', age: 30, active: true, metadata: { role: 'admin' } } |
| 89 | + |
| 90 | +// Strip undefined values and extra minimist fields |
| 91 | +const clean = stripUndefined(typed, schema); |
| 92 | +``` |
| 93 | + |
| 94 | +### Display Utilities |
| 95 | + |
| 96 | +```typescript |
| 97 | +import { printSuccess, printError, printTable, printDetails } from '@constructive-sdk/cli'; |
| 98 | + |
| 99 | +printSuccess('Context created'); |
| 100 | +printError('Connection failed'); |
| 101 | + |
| 102 | +printTable( |
| 103 | + ['Name', 'Endpoint', 'Status'], |
| 104 | + [ |
| 105 | + ['production', 'https://api.example.com/graphql', 'active'], |
| 106 | + ['staging', 'https://staging.example.com/graphql', 'inactive'] |
| 107 | + ] |
| 108 | +); |
| 109 | + |
| 110 | +printDetails([ |
| 111 | + { key: 'Name', value: 'production' }, |
| 112 | + { key: 'Endpoint', value: 'https://api.example.com/graphql' } |
| 113 | +]); |
| 114 | +``` |
| 115 | + |
| 116 | +### Subcommand Dispatching |
| 117 | + |
| 118 | +```typescript |
| 119 | +import { createSubcommandHandler, CommandHandler } from '@constructive-sdk/cli'; |
| 120 | + |
| 121 | +const createCmd: CommandHandler = async (argv, prompter, options) => { |
| 122 | + // Handle 'context create' |
| 123 | +}; |
| 124 | + |
| 125 | +const listCmd: CommandHandler = async (argv, prompter, options) => { |
| 126 | + // Handle 'context list' |
| 127 | +}; |
| 128 | + |
| 129 | +const contextHandler = createSubcommandHandler( |
| 130 | + { create: createCmd, list: listCmd }, |
| 131 | + 'Usage: my-tool context <create|list>' |
| 132 | +); |
| 133 | +``` |
| 134 | + |
| 135 | +## API Reference |
| 136 | + |
| 137 | +### Config (`@constructive-sdk/cli`) |
| 138 | + |
| 139 | +| Export | Description | |
| 140 | +|--------|-------------| |
| 141 | +| `getAppDirs(toolName, options?)` | Get XDG-compliant app directories for a CLI tool | |
| 142 | +| `getConfigStore(toolName)` | Create a config store with context and credential management | |
| 143 | + |
| 144 | +### Commands (`@constructive-sdk/cli`) |
| 145 | + |
| 146 | +| Export | Description | |
| 147 | +|--------|-------------| |
| 148 | +| `buildCommands(definitions)` | Build a commands map from command definitions | |
| 149 | +| `createSubcommandHandler(subcommands, usage)` | Create a handler that dispatches to subcommands | |
| 150 | +| `CommandHandler` | Type: `(argv, prompter, options) => Promise<void>` | |
| 151 | +| `CommandDefinition` | Interface: `{ name, handler, usage? }` | |
| 152 | + |
| 153 | +### CLI Utilities (`@constructive-sdk/cli`) |
| 154 | + |
| 155 | +| Export | Description | |
| 156 | +|--------|-------------| |
| 157 | +| `coerceAnswers(answers, schema)` | Coerce string CLI args to proper GraphQL types | |
| 158 | +| `stripUndefined(obj, schema?)` | Remove undefined values and non-schema keys | |
| 159 | +| `parseMutationInput(answers)` | Parse JSON input field from CLI mutation commands | |
| 160 | +| `buildSelectFromPaths(paths)` | Build ORM select object from dot-notation paths | |
| 161 | + |
| 162 | +### Display (`@constructive-sdk/cli`) |
| 163 | + |
| 164 | +| Export | Description | |
| 165 | +|--------|-------------| |
| 166 | +| `printSuccess(message)` | Print green success message | |
| 167 | +| `printError(message)` | Print red error message to stderr | |
| 168 | +| `printWarning(message)` | Print yellow warning to stderr | |
| 169 | +| `printInfo(message)` | Print cyan info message | |
| 170 | +| `printKeyValue(key, value, indent?)` | Print formatted key-value pair | |
| 171 | +| `printDetails(entries, indent?)` | Print aligned key-value block | |
| 172 | +| `printTable(headers, rows, indent?)` | Print formatted table | |
| 173 | + |
| 174 | +### Re-exports from inquirerer |
| 175 | + |
| 176 | +| Export | Description | |
| 177 | +|--------|-------------| |
| 178 | +| `CLI` | Type: The main CLI class | |
| 179 | +| `CLIOptions` | Type: CLI configuration options | |
| 180 | +| `Inquirerer` | Type: The prompter interface | |
| 181 | +| `extractFirst(argv)` | Extract first positional argument | |
| 182 | +| `getPackageJson(dir)` | Load package.json from a directory | |
| 183 | + |
| 184 | +## Troubleshooting |
| 185 | + |
| 186 | +### "Cannot find module 'appstash'" |
| 187 | +Ensure `appstash` is installed: `pnpm add appstash` |
| 188 | + |
| 189 | +### Type coercion not working |
| 190 | +Check that your `FieldSchema` keys match the CLI argument names exactly. Fields not in the schema are ignored by `coerceAnswers`. |
| 191 | + |
| 192 | +### Config store not persisting |
| 193 | +The config store writes to `~/.{toolName}/`. Ensure the tool name is consistent across your application. Use `getConfigStore` with the same name everywhere. |
0 commit comments