icore 0.0.37 → 1.0.0-alpha

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/readme.md ADDED
@@ -0,0 +1,587 @@
1
+ # icore
2
+
3
+ Declarative command line interface mechanics for Node.js.
4
+
5
+ `icore` helps describe CLI commands with small option schemas and keeps command
6
+ handlers focused on application work. It standardizes argument parsing,
7
+ primitive option validation, defaults, choices, numeric ranges, and positional
8
+ argument checks. It can also resolve commands from a registry and report whether
9
+ each option was provided explicitly by the user or filled from a default.
10
+
11
+ It does not try to model your business domain. API calls, request building,
12
+ response mapping, and output formatting should stay in the application that uses
13
+ `icore`.
14
+
15
+ ## Installation
16
+
17
+ ```sh
18
+ npm install icore
19
+ ```
20
+
21
+ ## Requirements
22
+
23
+ - Node.js `>=20.19.0`
24
+ - TypeScript is supported through bundled declaration files.
25
+
26
+ ## Basic Usage
27
+
28
+ ```ts
29
+ import { defineCommand, runCommand } from 'icore';
30
+
31
+ const command = defineCommand({
32
+ path: ['users', 'get-accounts'],
33
+ options: {
34
+ format: {
35
+ type: 'string',
36
+ choices: ['json', 'table'],
37
+ default: 'table'
38
+ },
39
+ insecure: {
40
+ type: 'boolean'
41
+ }
42
+ },
43
+ async handle({ options, context }) {
44
+ const response = await context.sdk.users.getAccounts({});
45
+
46
+ return context.formatAccounts(response.accounts, options.format);
47
+ }
48
+ });
49
+
50
+ const output = await runCommand(
51
+ command,
52
+ ['users', 'get-accounts', '--format', 'json'],
53
+ {
54
+ sdk,
55
+ formatAccounts
56
+ }
57
+ );
58
+ ```
59
+
60
+ The command handler receives parsed options, user-provided option metadata,
61
+ remaining positionals, and caller provided context.
62
+
63
+ ## Design Goals
64
+
65
+ - describe CLI mechanics declaratively;
66
+ - use literal option types: `type: 'string' | 'boolean' | 'number'`;
67
+ - keep handlers free from repetitive option parsing;
68
+ - keep domain and API semantics outside the framework;
69
+ - provide predictable user-facing errors;
70
+ - avoid runtime dependencies.
71
+
72
+ ## What icore Handles
73
+
74
+ `icore` handles generic CLI mechanics:
75
+
76
+ - parsing long options;
77
+ - validating known options;
78
+ - rejecting duplicated options;
79
+ - checking required options;
80
+ - applying and validating defaults;
81
+ - validating string choices;
82
+ - validating boolean flag form;
83
+ - composing option schemas;
84
+ - defining command registries;
85
+ - resolving commands by path;
86
+ - preserving user-provided option metadata;
87
+ - parsing numbers;
88
+ - validating integer, minimum, and maximum numeric constraints;
89
+ - checking command path and extra positional arguments.
90
+
91
+ ## What icore Does Not Handle
92
+
93
+ `icore` intentionally does not handle application-specific behavior:
94
+
95
+ - building API request DTOs;
96
+ - calling SDKs or network clients;
97
+ - managing database, HTTP, or gRPC lifecycle;
98
+ - checking business invariants such as `from <= to`;
99
+ - resolving mutually exclusive command modes;
100
+ - formatting output as JSON, tables, CSV, or other application formats.
101
+
102
+ Keep those decisions near the command handler or in your application layer.
103
+
104
+ ## Option Schemas
105
+
106
+ Options are described as plain objects.
107
+
108
+ Option names are exact. `icore` does not normalize camelCase to kebab-case or
109
+ kebab-case to camelCase. Use quoted object keys when your public CLI option
110
+ contains `-`.
111
+
112
+ ### String Options
113
+
114
+ ```ts
115
+ const schema = {
116
+ token: {
117
+ type: 'string',
118
+ required: true
119
+ },
120
+ format: {
121
+ type: 'string',
122
+ choices: ['json', 'table'],
123
+ default: 'table'
124
+ }
125
+ } as const;
126
+ ```
127
+
128
+ String options reject missing required values, blank strings, boolean flag form,
129
+ and values outside `choices`.
130
+
131
+ ### Boolean Options
132
+
133
+ ```ts
134
+ const schema = {
135
+ insecure: {
136
+ type: 'boolean'
137
+ }
138
+ } as const;
139
+ ```
140
+
141
+ Boolean options accept flag form only:
142
+
143
+ ```sh
144
+ --insecure
145
+ ```
146
+
147
+ Explicit values are rejected:
148
+
149
+ ```sh
150
+ --insecure true
151
+ --insecure=true
152
+ ```
153
+
154
+ ### Number Options
155
+
156
+ ```ts
157
+ const schema = {
158
+ limit: {
159
+ type: 'number',
160
+ integer: true,
161
+ min: 1,
162
+ max: 1000,
163
+ default: 100
164
+ }
165
+ } as const;
166
+ ```
167
+
168
+ Number options parse decimal numeric values and can validate integer and range
169
+ constraints. Defaults are validated with the same rules as user-provided values.
170
+
171
+ ## API
172
+
173
+ ### `parseArgv(args, schema?)`
174
+
175
+ Parses raw CLI arguments into positionals and raw option values.
176
+
177
+ ```ts
178
+ import { parseArgv } from 'icore';
179
+
180
+ const argv = parseArgv([
181
+ 'users',
182
+ 'get-accounts',
183
+ '--format=json',
184
+ '--insecure'
185
+ ]);
186
+ ```
187
+
188
+ Result:
189
+
190
+ ```ts
191
+ {
192
+ positionals: ['users', 'get-accounts'],
193
+ options: {
194
+ format: 'json',
195
+ insecure: true
196
+ }
197
+ }
198
+ ```
199
+
200
+ When an option schema is provided, boolean options are parsed as flag-only
201
+ options without consuming the following positional argument:
202
+
203
+ ```ts
204
+ const argv = parseArgv([
205
+ 'users',
206
+ 'get-accounts',
207
+ '--insecure',
208
+ 'extra'
209
+ ], {
210
+ insecure: {
211
+ type: 'boolean'
212
+ }
213
+ });
214
+ ```
215
+
216
+ Result:
217
+
218
+ ```ts
219
+ {
220
+ positionals: ['users', 'get-accounts', 'extra'],
221
+ options: {
222
+ insecure: true
223
+ }
224
+ }
225
+ ```
226
+
227
+ ### `parseOptions(schema, values)`
228
+
229
+ Validates raw option values using an option schema and returns typed options.
230
+
231
+ ```ts
232
+ import { parseOptions } from 'icore';
233
+
234
+ const options = parseOptions({
235
+ format: {
236
+ type: 'string',
237
+ choices: ['json', 'table'],
238
+ default: 'table'
239
+ },
240
+ depth: {
241
+ type: 'number',
242
+ integer: true,
243
+ min: 1,
244
+ required: true
245
+ }
246
+ } as const, {
247
+ depth: '10'
248
+ });
249
+ ```
250
+
251
+ Result:
252
+
253
+ ```ts
254
+ {
255
+ format: 'table',
256
+ depth: 10
257
+ }
258
+ ```
259
+
260
+ ### `parseOptionsDetailed(schema, values)`
261
+
262
+ Validates raw option values and returns parsed options together with
263
+ user-provided metadata.
264
+
265
+ ```ts
266
+ import { parseOptionsDetailed } from 'icore';
267
+
268
+ const result = parseOptionsDetailed({
269
+ token: {
270
+ type: 'string',
271
+ required: true
272
+ },
273
+ format: {
274
+ type: 'string',
275
+ choices: ['json', 'table'],
276
+ default: 'table'
277
+ }
278
+ } as const, {
279
+ token: 'secret'
280
+ });
281
+ ```
282
+
283
+ Result:
284
+
285
+ ```ts
286
+ {
287
+ options: {
288
+ token: 'secret',
289
+ format: 'table'
290
+ },
291
+ provided: {
292
+ token: true,
293
+ format: false
294
+ }
295
+ }
296
+ ```
297
+
298
+ `provided` is useful when a default value and an omitted option have different
299
+ application-level meaning.
300
+
301
+ ### `defineCommand(command)`
302
+
303
+ Defines a command while preserving its option schema types.
304
+
305
+ ```ts
306
+ import { defineCommand } from 'icore';
307
+
308
+ const command = defineCommand({
309
+ path: ['marketdata', 'get-order-book'],
310
+ options: {
311
+ 'instrument-id': {
312
+ type: 'string',
313
+ required: true
314
+ },
315
+ depth: {
316
+ type: 'number',
317
+ integer: true,
318
+ min: 1,
319
+ required: true
320
+ },
321
+ format: {
322
+ type: 'string',
323
+ choices: ['json', 'table'],
324
+ default: 'table'
325
+ }
326
+ },
327
+ async handle({ options, context }) {
328
+ const response = await context.sdk.marketdata.getOrderBook({
329
+ instrumentId: options['instrument-id'],
330
+ depth: options.depth
331
+ });
332
+
333
+ return context.formatOrderBook(response, options.format);
334
+ }
335
+ });
336
+ ```
337
+
338
+ ### `defineCommandRegistry(commands)`
339
+
340
+ Defines a command registry while preserving literal command path types.
341
+
342
+ ```ts
343
+ import { defineCommandRegistry } from 'icore';
344
+
345
+ const registry = defineCommandRegistry([
346
+ usersGetAccountsCommand,
347
+ marketdataGetOrderBookCommand
348
+ ] as const);
349
+ ```
350
+
351
+ The registry exposes ordered commands and derived command names:
352
+
353
+ ```ts
354
+ registry.commandNames;
355
+ ```
356
+
357
+ Result:
358
+
359
+ ```ts
360
+ [
361
+ 'users get-accounts',
362
+ 'marketdata get-order-book'
363
+ ]
364
+ ```
365
+
366
+ Duplicate command paths are rejected.
367
+
368
+ ### `isCommandName(registry, value)`
369
+
370
+ Checks whether an unknown value is a command name registered in the registry.
371
+
372
+ ```ts
373
+ if (isCommandName(registry, value)) {
374
+ // value is narrowed to the registry command name union.
375
+ }
376
+ ```
377
+
378
+ ### `resolveCommand(registry, positionals)`
379
+
380
+ Resolves a command from already parsed positional arguments.
381
+
382
+ ```ts
383
+ import { resolveCommand } from 'icore';
384
+
385
+ const resolved = resolveCommand(registry, [
386
+ 'users',
387
+ 'get-accounts',
388
+ 'extra'
389
+ ]);
390
+ ```
391
+
392
+ Result:
393
+
394
+ ```ts
395
+ {
396
+ name: 'users get-accounts',
397
+ path: ['users', 'get-accounts'],
398
+ command: usersGetAccountsCommand,
399
+ positionals: ['extra']
400
+ }
401
+ ```
402
+
403
+ When several command paths match, the most specific command wins. For example,
404
+ `users get-accounts` is preferred over `users`.
405
+
406
+ ### `resolveCommandFromArgs(registry, args)`
407
+
408
+ Resolves a command from raw CLI arguments. Each candidate command is parsed with
409
+ its own option schema, so boolean flags do not accidentally consume command path
410
+ segments.
411
+
412
+ ```ts
413
+ const resolved = resolveCommandFromArgs(registry, [
414
+ '--verbose',
415
+ 'users',
416
+ 'get-accounts'
417
+ ]);
418
+ ```
419
+
420
+ ### `runCommandFromRegistry(registry, args, context)`
421
+
422
+ Resolves a command from a registry and runs its handler.
423
+
424
+ ```ts
425
+ const output = await runCommandFromRegistry(
426
+ registry,
427
+ ['users', 'get-accounts', '--format', 'json'],
428
+ context
429
+ );
430
+ ```
431
+
432
+ This is registry-level mechanics only. Application-specific setup, API request
433
+ building, and output formatting still belong outside `icore`.
434
+
435
+ ### `mergeOptionsSchema(...schemas)`
436
+
437
+ Merges multiple option schemas while preserving literal option definition types.
438
+ Later schemas override earlier schemas with the same option name.
439
+
440
+ ```ts
441
+ import { mergeOptionsSchema } from 'icore';
442
+
443
+ const sdkOptions = {
444
+ token: {
445
+ type: 'string',
446
+ required: true
447
+ }
448
+ } as const;
449
+
450
+ const formatOptions = {
451
+ format: {
452
+ type: 'string',
453
+ choices: ['json', 'table'],
454
+ default: 'table'
455
+ }
456
+ } as const;
457
+
458
+ const options = mergeOptionsSchema(sdkOptions, formatOptions);
459
+ ```
460
+
461
+ ### `runCommand(command, args, context)`
462
+
463
+ Parses arguments, validates options, checks command positionals, and runs the
464
+ handler.
465
+
466
+ ```ts
467
+ const output = await runCommand(
468
+ command,
469
+ [
470
+ 'marketdata',
471
+ 'get-order-book',
472
+ '--instrument-id',
473
+ 'instrument-id',
474
+ '--depth',
475
+ '10'
476
+ ],
477
+ context
478
+ );
479
+ ```
480
+
481
+ By default, extra positionals are rejected. A command can opt in to extra
482
+ positionals with `allowExtraPositionals: true`.
483
+
484
+ ## Type Inference
485
+
486
+ Use `InferOptions` when you need the parsed option type explicitly.
487
+
488
+ ```ts
489
+ import type { InferOptions } from 'icore';
490
+
491
+ const schema = {
492
+ format: {
493
+ type: 'string',
494
+ choices: ['json', 'table'],
495
+ default: 'table'
496
+ },
497
+ dryRun: {
498
+ type: 'boolean'
499
+ }
500
+ } as const;
501
+
502
+ type Options = InferOptions<typeof schema>;
503
+ ```
504
+
505
+ `Options` is equivalent to:
506
+
507
+ ```ts
508
+ type Options = {
509
+ format: 'json' | 'table';
510
+ dryRun: boolean | undefined;
511
+ };
512
+ ```
513
+
514
+ Required options and options with defaults are always present. Optional options
515
+ without defaults are returned as `T | undefined`.
516
+
517
+ Use `InferProvidedOptions` when you need the option presence type explicitly.
518
+
519
+ ```ts
520
+ import type { InferProvidedOptions } from 'icore';
521
+
522
+ type Provided = InferProvidedOptions<typeof schema>;
523
+ ```
524
+
525
+ `Provided` maps every schema option to `boolean`. `true` means the user
526
+ specified that option explicitly; defaults keep the flag `false`.
527
+
528
+ Use `MergeOptionsSchemas` when you need the merged schema type explicitly.
529
+
530
+ ```ts
531
+ import type { MergeOptionsSchemas } from 'icore';
532
+
533
+ type Schema = MergeOptionsSchemas<[typeof sdkOptions, typeof formatOptions]>;
534
+ ```
535
+
536
+ Use `CommandName` when you need the inferred command name type explicitly.
537
+
538
+ ```ts
539
+ import type { CommandName } from 'icore';
540
+
541
+ type Name = CommandName<typeof usersGetAccountsCommand>;
542
+ ```
543
+
544
+ `Name` is equivalent to:
545
+
546
+ ```ts
547
+ type Name = 'users get-accounts';
548
+ ```
549
+
550
+ ## Error Messages
551
+
552
+ `icore` throws regular `Error` objects with stable messages.
553
+
554
+ Examples:
555
+
556
+ ```txt
557
+ Unexpected argument '--unknown'
558
+ Unexpected duplicate argument '--format'
559
+ Unexpected duplicate command 'users get-accounts'
560
+ Unknown command: users get-unknown
561
+ Expected required argument '--token'
562
+ Expected '--format' as one of: json, table
563
+ Expected '--depth' as integer
564
+ Expected '--depth' to be greater than or equal to 1
565
+ ```
566
+
567
+ Applications can catch these errors and decide how to print them.
568
+
569
+ ## Project Boundary
570
+
571
+ `icore` is intended to be a small CLI mechanics module. It should not grow into a
572
+ domain-specific framework for a particular SDK or API.
573
+
574
+ Good responsibilities for `icore`:
575
+
576
+ - option schema evaluation;
577
+ - command path checking;
578
+ - common argument errors;
579
+ - typed command handler input.
580
+
581
+ Responsibilities that should stay outside `icore`:
582
+
583
+ - business validation;
584
+ - SDK lifecycle management;
585
+ - provider-specific request modes;
586
+ - generated contract mapping;
587
+ - presentation formatting.
package/ICore.js DELETED
@@ -1,59 +0,0 @@
1
- const SRC = require('./src');
2
-
3
- const {
4
- AsyncMongoDbDriver,
5
- AsyncFileSystem,
6
- AsyncHttp,
7
- Templates
8
- } = SRC;
9
-
10
-
11
-
12
-
13
-
14
- class ICore {
15
- constructor(conf) {
16
- this.conf = conf;
17
-
18
- this.http = new AsyncHttp();
19
- this.tmpl = new Templates();
20
- this.fs = new AsyncFileSystem();
21
- this.db = new AsyncMongoDbDriver();
22
- }
23
-
24
-
25
-
26
- queue({ cmds=[], it, beth, done }) {
27
- let i = 0;
28
-
29
- return new Promise((resolve, reject) => {
30
- let loop = () => {
31
- it({ item: cmds[i], index: i, beth }).then(body => {
32
- i++;
33
-
34
- if (typeof done == 'function') {
35
- done(body);
36
- }
37
-
38
- if (!cmds[i]) {
39
- return resolve(body);
40
- }
41
-
42
- loop();
43
- },
44
- reject);
45
- };
46
-
47
- if (!cmds.length) {
48
- return resolve();
49
- }
50
-
51
- loop();
52
- });
53
- }
54
- }
55
-
56
-
57
-
58
-
59
- module.exports = ICore;