@runa-cmd/core 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.
Files changed (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +351 -0
  3. package/dist/cli.d.ts +12 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +413 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/command.d.ts +20 -0
  8. package/dist/command.d.ts.map +1 -0
  9. package/dist/command.js +46 -0
  10. package/dist/command.js.map +1 -0
  11. package/dist/config/loader.d.ts +9 -0
  12. package/dist/config/loader.d.ts.map +1 -0
  13. package/dist/config/loader.js +34 -0
  14. package/dist/config/loader.js.map +1 -0
  15. package/dist/config/merge.d.ts +18 -0
  16. package/dist/config/merge.d.ts.map +1 -0
  17. package/dist/config/merge.js +31 -0
  18. package/dist/config/merge.js.map +1 -0
  19. package/dist/config/search.d.ts +18 -0
  20. package/dist/config/search.d.ts.map +1 -0
  21. package/dist/config/search.js +40 -0
  22. package/dist/config/search.js.map +1 -0
  23. package/dist/errors.d.ts +32 -0
  24. package/dist/errors.d.ts.map +1 -0
  25. package/dist/errors.js +65 -0
  26. package/dist/errors.js.map +1 -0
  27. package/dist/index.d.ts +16 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +20 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/introspect.d.ts +16 -0
  32. package/dist/introspect.d.ts.map +1 -0
  33. package/dist/introspect.js +128 -0
  34. package/dist/introspect.js.map +1 -0
  35. package/dist/lifecycle.d.ts +27 -0
  36. package/dist/lifecycle.d.ts.map +1 -0
  37. package/dist/lifecycle.js +62 -0
  38. package/dist/lifecycle.js.map +1 -0
  39. package/dist/middleware.d.ts +16 -0
  40. package/dist/middleware.d.ts.map +1 -0
  41. package/dist/middleware.js +15 -0
  42. package/dist/middleware.js.map +1 -0
  43. package/dist/parse/parseargs-bridge.d.ts +22 -0
  44. package/dist/parse/parseargs-bridge.d.ts.map +1 -0
  45. package/dist/parse/parseargs-bridge.js +54 -0
  46. package/dist/parse/parseargs-bridge.js.map +1 -0
  47. package/dist/parse/resolve.d.ts +48 -0
  48. package/dist/parse/resolve.d.ts.map +1 -0
  49. package/dist/parse/resolve.js +240 -0
  50. package/dist/parse/resolve.js.map +1 -0
  51. package/dist/parse/schema-walker.d.ts +21 -0
  52. package/dist/parse/schema-walker.d.ts.map +1 -0
  53. package/dist/parse/schema-walker.js +121 -0
  54. package/dist/parse/schema-walker.js.map +1 -0
  55. package/dist/parse/variadic.d.ts +22 -0
  56. package/dist/parse/variadic.d.ts.map +1 -0
  57. package/dist/parse/variadic.js +71 -0
  58. package/dist/parse/variadic.js.map +1 -0
  59. package/dist/plugin.d.ts +53 -0
  60. package/dist/plugin.d.ts.map +1 -0
  61. package/dist/plugin.js +194 -0
  62. package/dist/plugin.js.map +1 -0
  63. package/dist/types.d.ts +225 -0
  64. package/dist/types.d.ts.map +1 -0
  65. package/dist/types.js +19 -0
  66. package/dist/types.js.map +1 -0
  67. package/dist/zod.d.ts +8 -0
  68. package/dist/zod.d.ts.map +1 -0
  69. package/dist/zod.js +8 -0
  70. package/dist/zod.js.map +1 -0
  71. package/package.json +66 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Runa CLI 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,351 @@
1
+ # @runa-cmd/core
2
+
3
+ The engine of the Runa CLI framework. Schema-driven, TypeScript-first, zero runtime dependencies.
4
+
5
+ Define your CLI with Zod schemas and get full type inference, validation, help text, and more — from a single source of truth.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add @runa-cmd/core zod
11
+ ```
12
+
13
+ > Requires Node 18.3+ (uses `util.parseArgs`). Zod v4 is a peer dependency.
14
+
15
+ ## Quick Start
16
+
17
+ ```ts
18
+ import { defineCommand, runCLI } from '@runa-cmd/core';
19
+ import { z } from '@runa-cmd/core/zod';
20
+
21
+ const greet = defineCommand({
22
+ meta: { name: 'greet', description: 'Say hello' },
23
+ args: { name: z.string().describe('Who to greet') },
24
+ run({ args }) {
25
+ console.log(`Hello, ${args.name}!`);
26
+ },
27
+ });
28
+
29
+ runCLI(greet);
30
+ ```
31
+
32
+ ```
33
+ $ my-cli greet world
34
+ Hello, world!
35
+ ```
36
+
37
+ ## API
38
+
39
+ ### `defineCommand(config)`
40
+
41
+ Creates a command with full type inference from Zod schemas to your `run()` handler.
42
+
43
+ ```ts
44
+ const deploy = defineCommand({
45
+ meta: {
46
+ name: 'deploy',
47
+ description: 'Deploy to an environment',
48
+ options: {
49
+ env: { alias: ['-e'], env: 'DEPLOY_ENV' },
50
+ replicas: { group: 'Scaling', hint: '<count>' },
51
+ },
52
+ },
53
+ args: {
54
+ service: z.string().describe('Service to deploy'),
55
+ },
56
+ options: {
57
+ env: z.enum(['staging', 'production']).describe('Target environment'),
58
+ replicas: z.number().default(1).describe('Number of replicas'),
59
+ dryRun: z.boolean().default(false).describe('Preview without deploying'),
60
+ },
61
+ output: z.object({ url: z.string(), replicas: z.number() }),
62
+ async run({ args, options }) {
63
+ // args.service: string
64
+ // options.env: 'staging' | 'production'
65
+ // options.replicas: number
66
+ // options.dryRun: boolean
67
+ // Must return { url: string, replicas: number } (enforced by output schema)
68
+ return {
69
+ url: `https://${options.env}.example.com/${args.service}`,
70
+ replicas: options.replicas,
71
+ };
72
+ },
73
+ });
74
+ ```
75
+
76
+ #### Config Properties
77
+
78
+ | Property | Type | Required | Description |
79
+ |---|---|---|---|
80
+ | `meta.name` | `string` | Yes | Command name |
81
+ | `meta.description` | `string` | Yes | One-line description |
82
+ | `meta.version` | `string` | No | Command version |
83
+ | `meta.options` | `Record<string, OptionMeta>` | No | Per-option metadata (alias, env, group, deprecated, hint) |
84
+ | `args` | `Record<string, ZodType>` | No | Positional arguments (insertion order matters) |
85
+ | `options` | `Record<string, ZodType>` | No | Named options (--flag style) |
86
+ | `output` | `ZodType` | No | Output schema — if set, `run()` must return matching data |
87
+ | `run` | `(ctx: RunContext) => RunReturn` | Yes | Command handler |
88
+
89
+ #### Option Metadata (`meta.options`)
90
+
91
+ Operational metadata lives in `meta.options`, separate from the Zod schema:
92
+
93
+ | Property | Type | Description |
94
+ |---|---|---|
95
+ | `alias` | `string[]` | Short flags, e.g. `['-e']` (must include dash prefix) |
96
+ | `env` | `string` | Environment variable name, e.g. `'DEPLOY_ENV'` |
97
+ | `group` | `string` | Group name for help display, e.g. `'Scaling'` |
98
+ | `deprecated` | `string \| boolean` | Deprecation notice |
99
+ | `hint` | `string` | Value hint for help, e.g. `'<count>'` |
100
+
101
+ #### Run Context
102
+
103
+ Your `run()` handler receives:
104
+
105
+ ```ts
106
+ interface RunContext {
107
+ args: InferArgs<TArgs>; // Typed positional args
108
+ options: InferOptions<TOptions>; // Typed named options
109
+ globalOptions: Record<string, unknown>;
110
+ command: CommandMeta; // Current command metadata
111
+ rawArgs: string[]; // Original argv
112
+ }
113
+ ```
114
+
115
+ ### `defineCLI(config)`
116
+
117
+ Creates a multi-command CLI with subcommands, plugins, and middleware.
118
+
119
+ ```ts
120
+ import { defineCLI, defineCommand } from '@runa-cmd/core';
121
+ import { helpPlugin } from '@runa-cmd/help';
122
+
123
+ const cli = defineCLI({
124
+ meta: { name: 'my-tool', version: '1.0.0', description: 'A great CLI' },
125
+ commands: {
126
+ init,
127
+ deploy,
128
+ config: { // Nested subcommands: my-tool config get, my-tool config set
129
+ get: configGet,
130
+ set: configSet,
131
+ },
132
+ },
133
+ plugins: [helpPlugin()],
134
+ middleware: [timer],
135
+ globalOptions: {
136
+ verbose: z.boolean().default(false).describe('Enable verbose output'),
137
+ },
138
+ });
139
+
140
+ cli.run();
141
+ ```
142
+
143
+ #### Config Properties
144
+
145
+ | Property | Type | Required | Description |
146
+ |---|---|---|---|
147
+ | `meta.name` | `string` | Yes | CLI name |
148
+ | `meta.version` | `string` | No | CLI version |
149
+ | `meta.description` | `string` | No | One-line description |
150
+ | `commands` | `CommandTree` | Yes | Commands and nested groups |
151
+ | `plugins` | `PluginConfig[]` | No | Plugins to load |
152
+ | `middleware` | `Middleware[]` | No | Global middleware chain |
153
+ | `globalOptions` | `Record<string, ZodType>` | No | Options available to all commands |
154
+ | `globalMeta` | `Record<string, OptionMeta>` | No | Metadata for global options |
155
+ | `config` | `CLIConfigOptions` | No | Config file loading settings |
156
+
157
+ ### `runCLI(command, argv?)`
158
+
159
+ Shorthand for single-command CLIs. Wraps the command in `defineCLI` and runs it.
160
+
161
+ ```ts
162
+ runCLI(myCommand);
163
+ // Equivalent to:
164
+ defineCLI({
165
+ meta: { name: command.meta.name },
166
+ commands: { [command.meta.name]: command },
167
+ }).run();
168
+ ```
169
+
170
+ ### `defineMiddleware(handler)`
171
+
172
+ Creates middleware using the onion model (like Koa/Hono):
173
+
174
+ ```ts
175
+ const auth = defineMiddleware(async ({ next, globalOptions }) => {
176
+ if (!globalOptions.token) throw new Error('Missing --token');
177
+ // Code before next() runs before the command
178
+ await next();
179
+ // Code after next() runs after the command
180
+ });
181
+ ```
182
+
183
+ ### `definePlugin(config)`
184
+
185
+ Creates a plugin that extends the CLI:
186
+
187
+ ```ts
188
+ const analytics = definePlugin({
189
+ meta: { name: 'analytics', version: '1.0.0' },
190
+ capabilities: { addGlobalOptions: true },
191
+ setup(api) {
192
+ api.addGlobalOption('trackUsage', z.boolean().default(true));
193
+ api.hook('afterRun', (ctx) => {
194
+ if (ctx.globalOptions?.trackUsage) {
195
+ trackEvent(ctx.command?.name);
196
+ }
197
+ });
198
+ },
199
+ cleanup() {
200
+ flushAnalytics();
201
+ },
202
+ });
203
+ ```
204
+
205
+ #### Plugin API
206
+
207
+ The `setup(api)` function receives:
208
+
209
+ | Method | Description |
210
+ |---|---|
211
+ | `api.addCommand(name, command)` | Register a new command (requires `addCommands` capability) |
212
+ | `api.addGlobalOption(name, schema, meta?)` | Add a global option (requires `addGlobalOptions` capability) |
213
+ | `api.addMiddleware(middleware)` | Add middleware (requires `addMiddleware` capability) |
214
+ | `api.hook(name, handler)` | Register a lifecycle hook handler |
215
+ | `api.getSchema()` | Get the introspected CLI schema |
216
+ | `api.getCommands()` | Get the live command tree reference |
217
+
218
+ ### `getSchema(config)`
219
+
220
+ Introspects a CLI config and returns structured metadata. Used internally by plugins like help and MCP.
221
+
222
+ ```ts
223
+ const schema = getSchema(cliConfig);
224
+ // schema.meta — CLI name, version, description
225
+ // schema.commands — Array of CommandSchema with args, options, etc.
226
+ // schema.globalOptions — Array of OptionSchema
227
+ ```
228
+
229
+ ### `jsonLoader()`
230
+
231
+ Built-in JSON config file loader:
232
+
233
+ ```ts
234
+ const cli = defineCLI({
235
+ // ...
236
+ config: {
237
+ name: 'my-tool', // Searches for my-tool.config.json, .my-toolrc.json, etc.
238
+ loaders: [jsonLoader()],
239
+ searchPaths: ['./'], // Where to search
240
+ },
241
+ });
242
+ ```
243
+
244
+ ### Error Classes
245
+
246
+ ```ts
247
+ import {
248
+ RunaError, // Base: exit code 1
249
+ ValidationError, // Exit code 2 (POSIX usage error), has .issues
250
+ CommandNotFoundError, // Exit code 127, has .suggestion
251
+ CommandError, // Configurable exit code, for user-thrown errors
252
+ } from '@runa-cmd/core';
253
+
254
+ // Throw from run() handlers:
255
+ throw new CommandError('Deployment failed', { code: 'DEPLOY_FAILED', exitCode: 3 });
256
+ ```
257
+
258
+ ### Constants
259
+
260
+ ```ts
261
+ import { HOOK_NAMES, PARAM_TYPES } from '@runa-cmd/core';
262
+
263
+ HOOK_NAMES.BEFORE_PARSE // 'beforeParse'
264
+ HOOK_NAMES.ON_GLOBAL_FLAGS // 'onGlobalFlags'
265
+ HOOK_NAMES.AFTER_PARSE // 'afterParse'
266
+ HOOK_NAMES.BEFORE_RUN // 'beforeRun'
267
+ HOOK_NAMES.AFTER_RUN // 'afterRun'
268
+ HOOK_NAMES.ON_ERROR // 'onError'
269
+ HOOK_NAMES.CLEANUP // 'cleanup'
270
+
271
+ PARAM_TYPES.STRING // 'string'
272
+ PARAM_TYPES.NUMBER // 'number'
273
+ PARAM_TYPES.BOOLEAN // 'boolean'
274
+ PARAM_TYPES.ENUM // 'enum'
275
+ PARAM_TYPES.ARRAY // 'array'
276
+ ```
277
+
278
+ ### Subpath Export: `@runa-cmd/core/zod`
279
+
280
+ Re-exports Zod v4 for convenience:
281
+
282
+ ```ts
283
+ import { z } from '@runa-cmd/core/zod';
284
+ // Same as: import { z } from 'zod';
285
+ ```
286
+
287
+ ## Lifecycle
288
+
289
+ The full execution lifecycle with seven hooks:
290
+
291
+ ```
292
+ beforeParse
293
+ ↓
294
+ Parse global flags
295
+ ↓
296
+ onGlobalFlags ← (help/mcp plugins intercept here)
297
+ ↓
298
+ Resolve command from argv
299
+ ↓
300
+ Parse command args & options
301
+ ↓
302
+ afterParse
303
+ ↓
304
+ beforeRun
305
+ ↓
306
+ Middleware chain (onion model)
307
+ ↓
308
+ command.run()
309
+ ↓
310
+ afterRun
311
+ ↓
312
+ cleanup ← (ALWAYS runs, even on error)
313
+
314
+ On error → onError hook fires before cleanup
315
+ ```
316
+
317
+ ## Value Resolution
318
+
319
+ Options are resolved in this priority order:
320
+
321
+ ```
322
+ CLI args > Environment variables > Config file values > Zod defaults
323
+ ```
324
+
325
+ For example, if an option has `env: 'PORT'` and `z.number().default(3000)`:
326
+ 1. `--port 8080` wins over everything
327
+ 2. `PORT=4000` wins over config file and default
328
+ 3. Config file value wins over default
329
+ 4. `3000` is the fallback
330
+
331
+ ## Types
332
+
333
+ All types are exported for consumers who need them:
334
+
335
+ ```ts
336
+ import type {
337
+ CLI, CLIConfig, CLIMeta, CLISchema, CLIConfigOptions,
338
+ Command, CommandConfig, CommandMeta, CommandSchema, CommandTree,
339
+ HookContext, HookHandler, HookName,
340
+ Middleware, MiddlewareContext, MiddlewareFn,
341
+ PluginAPI, PluginCapabilities, PluginConfig, PluginMeta,
342
+ RunContext, RunReturn,
343
+ ArgSchema, OptionSchema, OptionMeta,
344
+ InferArgs, InferOptions,
345
+ ConfigLoader, ParamMetadata, ParamType,
346
+ } from '@runa-cmd/core';
347
+ ```
348
+
349
+ ## License
350
+
351
+ MIT
package/dist/cli.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { CLI, CLIConfig, Command } from './types.js';
2
+ /**
3
+ * Create a CLI from a configuration.
4
+ * Returns a branded CLI object with a .run() method.
5
+ */
6
+ export declare function defineCLI(config: CLIConfig): CLI;
7
+ /**
8
+ * Single-command shorthand.
9
+ * Wraps a command in defineCLI() and runs it.
10
+ */
11
+ export declare function runCLI(command: Command, argv?: string[]): Promise<void>;
12
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAuBA,OAAO,KAAK,EACX,GAAG,EACH,SAAS,EACT,OAAO,EAOP,MAAM,YAAY,CAAC;AA0cpB;;;GAGG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,GAAG,CAShD;AAED;;;GAGG;AACH,wBAAsB,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAM7E"}