citty 0.0.0 → 0.0.2

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 ADDED
@@ -0,0 +1,47 @@
1
+ MIT License
2
+
3
+ Copyright (c) Pooya Parsa <pooya@pi0.io>
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.
22
+
23
+ ---
24
+
25
+ Parser is based on https://github.com/lukeed/mri
26
+
27
+ The MIT License (MIT)
28
+
29
+ Copyright (c) Luke Edwards luke.edwards05@gmail.com (lukeed.com)
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
32
+
33
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
34
+
35
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36
+
37
+ ---
38
+
39
+ Colorette dependency is bundled (https://github.com/jorgebucaran/colorette)
40
+
41
+ Copyright © Jorge Bucaran <https://jorgebucaran.com>
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
46
+
47
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # 🌆 citty
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![bundle][bundle-src]][bundle-href]
6
+ [![Codecov][codecov-src]][codecov-href]
7
+ [![License][license-src]][license-href]
8
+
9
+ > Elegant CLI Builder
10
+
11
+ - Fast and lightweight argument parser based on [mri](https://github.com/lukeed/mri)
12
+ - Smart value parsing with typecast, boolean shortcuts and unknown flag handling
13
+ - Nested sub-commands
14
+ - Lazy and Async commands
15
+ - Plugable and composable API
16
+ - Auto generated usage and help
17
+
18
+ 🚧 This project is under heavy development. More features are coming soon!
19
+
20
+ ## Usage
21
+
22
+ Install package:
23
+
24
+ ```sh
25
+ # npm
26
+ npm install citty
27
+
28
+ # yarn
29
+ yarn add citty
30
+
31
+ # pnpm
32
+ pnpm install citty
33
+ ```
34
+
35
+ Import:
36
+
37
+ ```js
38
+ // ESM
39
+ import { defineCommand, runMain } from "citty";
40
+
41
+ // CommonJS
42
+ const { defineCommand, runMain } = require("citty");
43
+ ```
44
+
45
+ Define main command to run:
46
+
47
+ ```ts
48
+ import { defineCommand, runMain } from "citty";
49
+
50
+ const main = defineCommand({
51
+ meta: {
52
+ name: "hello",
53
+ version: "1.0.0",
54
+ description: "My Awesome CLI App",
55
+ },
56
+ args: {
57
+ name: {
58
+ type: "positional",
59
+ description: "Your name",
60
+ required: true,
61
+ },
62
+ friendly: {
63
+ type: "boolean",
64
+ description: "Use friendly greeting",
65
+ },
66
+ },
67
+ run({ args }) {
68
+ console.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
69
+ },
70
+ });
71
+
72
+ runMain(main);
73
+ ```
74
+
75
+ ## Utils
76
+
77
+ ### `defineCommand`
78
+
79
+ `defineCommand` is a type helper for defining commands.
80
+
81
+ ### `runMain`
82
+
83
+ Runs a command with usage support and graceful error handling.
84
+
85
+ ### `runCommand`
86
+
87
+ Parses input args and runs command and sub-commands (unsupervised).
88
+
89
+ ### `parseArgs`
90
+
91
+ Parses input arguments and applies defaults.
92
+
93
+ ### `renderUsage`
94
+
95
+ Renders command usage to a string value.
96
+
97
+ ### `showUsage`
98
+
99
+ Renders usage and prints to the console
100
+
101
+ ## Development
102
+
103
+ - Clone this repository
104
+ - Install latest LTS version of [Node.js](https://nodejs.org/en/)
105
+ - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
106
+ - Install dependencies using `pnpm install`
107
+ - Run interactive tests using `pnpm dev`
108
+
109
+ ## License
110
+
111
+ Made with 💛 Published under [MIT License](./LICENSE).
112
+
113
+ Argument parser is based on [lukeed/mri](https://github.com/lukeed/mri) by Luke Edwards ([@lukeed](https://github.com/lukeed)).
114
+
115
+ <!-- Badges -->
116
+
117
+ <!-- Badges -->
118
+
119
+ [npm-version-src]: https://img.shields.io/npm/v/citty?style=flat&colorA=18181B&colorB=F0DB4F
120
+ [npm-version-href]: https://npmjs.com/package/citty
121
+ [npm-downloads-src]: https://img.shields.io/npm/dm/citty?style=flat&colorA=18181B&colorB=F0DB4F
122
+ [npm-downloads-href]: https://npmjs.com/package/citty
123
+ [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/citty/main?style=flat&colorA=18181B&colorB=F0DB4F
124
+ [codecov-href]: https://codecov.io/gh/unjs/citty
125
+ [bundle-src]: https://img.shields.io/bundlephobia/minzip/citty?style=flat&colorA=18181B&colorB=F0DB4F
126
+ [bundle-href]: https://bundlephobia.com/result?p=citty
127
+ [license-src]: https://img.shields.io/github/license/unjs/citty.svg?style=flat&colorA=18181B&colorB=F0DB4F
128
+ [license-href]: https://github.com/unjs/citty/blob/main/LICENSE
129
+ [jsdocs-src]: https://img.shields.io/badge/jsDocs.io-reference-18181B?style=flat&colorA=18181B&colorB=F0DB4F
130
+ [jsdocs-href]: https://www.jsdocs.io/package/citty
package/dist/index.cjs ADDED
@@ -0,0 +1,544 @@
1
+ 'use strict';
2
+
3
+ const scule = require('scule');
4
+ const tty = require('tty');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ const n = Object.create(null);
8
+ if (e) {
9
+ for (const k in e) {
10
+ n[k] = e[k];
11
+ }
12
+ }
13
+ n.default = e;
14
+ return n;
15
+ }
16
+
17
+ const tty__namespace = /*#__PURE__*/_interopNamespaceDefault(tty);
18
+
19
+ function toArray(val) {
20
+ if (Array.isArray(val)) {
21
+ return val;
22
+ }
23
+ return val !== void 0 ? [val] : [];
24
+ }
25
+ function formatLineColumns(lines, linePrefix = "") {
26
+ const maxLengh = [];
27
+ for (const line of lines) {
28
+ for (const [i, element] of line.entries()) {
29
+ maxLengh[i] = Math.max(maxLengh[i] || 0, element.length);
30
+ }
31
+ }
32
+ return lines.map(
33
+ (l) => l.map(
34
+ (c, i) => linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLengh[i])
35
+ ).join(" ")
36
+ ).join("\n");
37
+ }
38
+ function resolveValue(input) {
39
+ return typeof input === "function" ? input() : input;
40
+ }
41
+ class CLIError extends Error {
42
+ constructor(message, code) {
43
+ super(message);
44
+ this.code = code;
45
+ this.name = "CLIError";
46
+ }
47
+ }
48
+
49
+ function toArr(any) {
50
+ return any == void 0 ? [] : Array.isArray(any) ? any : [any];
51
+ }
52
+ function toVal(out, key, val, opts) {
53
+ let x;
54
+ const old = out[key];
55
+ const nxt = ~opts.string.indexOf(key) ? val == void 0 || val === true ? "" : String(val) : typeof val === "boolean" ? val : ~opts.boolean.indexOf(key) ? val === "false" ? false : val === "true" || (out._.push((x = +val, x * 0 === 0) ? x : val), !!val) : (x = +val, x * 0 === 0) ? x : val;
56
+ out[key] = old == void 0 ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
57
+ }
58
+ function parseRawArgs(args = [], opts = {}) {
59
+ let k;
60
+ let arr;
61
+ let arg;
62
+ let name;
63
+ let val;
64
+ const out = { _: [] };
65
+ let i = 0;
66
+ let j = 0;
67
+ let idx = 0;
68
+ const len = args.length;
69
+ const alibi = opts.alias !== void 0;
70
+ const strict = opts.unknown !== void 0;
71
+ const defaults = opts.default !== void 0;
72
+ opts.alias = opts.alias || {};
73
+ opts.string = toArr(opts.string);
74
+ opts.boolean = toArr(opts.boolean);
75
+ if (alibi) {
76
+ for (k in opts.alias) {
77
+ arr = opts.alias[k] = toArr(opts.alias[k]);
78
+ for (i = 0; i < arr.length; i++) {
79
+ (opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
80
+ }
81
+ }
82
+ }
83
+ for (i = opts.boolean.length; i-- > 0; ) {
84
+ arr = opts.alias[opts.boolean[i]] || [];
85
+ for (j = arr.length; j-- > 0; ) {
86
+ opts.boolean.push(arr[j]);
87
+ }
88
+ }
89
+ for (i = opts.string.length; i-- > 0; ) {
90
+ arr = opts.alias[opts.string[i]] || [];
91
+ for (j = arr.length; j-- > 0; ) {
92
+ opts.string.push(arr[j]);
93
+ }
94
+ }
95
+ if (defaults) {
96
+ for (k in opts.default) {
97
+ name = typeof opts.default[k];
98
+ arr = opts.alias[k] = opts.alias[k] || [];
99
+ if (opts[name] !== void 0) {
100
+ opts[name].push(k);
101
+ for (i = 0; i < arr.length; i++) {
102
+ opts[name].push(arr[i]);
103
+ }
104
+ }
105
+ }
106
+ }
107
+ const keys = strict ? Object.keys(opts.alias) : [];
108
+ for (i = 0; i < len; i++) {
109
+ arg = args[i];
110
+ if (arg === "--") {
111
+ out._ = out._.concat(args.slice(++i));
112
+ break;
113
+ }
114
+ for (j = 0; j < arg.length; j++) {
115
+ if (arg.charCodeAt(j) !== 45) {
116
+ break;
117
+ }
118
+ }
119
+ if (j === 0) {
120
+ out._.push(arg);
121
+ } else if (arg.substring(j, j + 3) === "no-") {
122
+ name = arg.slice(Math.max(0, j + 3));
123
+ if (strict && !~keys.indexOf(name)) {
124
+ return opts.unknown(arg);
125
+ }
126
+ out[name] = false;
127
+ } else {
128
+ for (idx = j + 1; idx < arg.length; idx++) {
129
+ if (arg.charCodeAt(idx) === 61) {
130
+ break;
131
+ }
132
+ }
133
+ name = arg.substring(j, idx);
134
+ val = arg.slice(Math.max(0, ++idx)) || i + 1 === len || ("" + args[i + 1]).charCodeAt(0) === 45 || args[++i];
135
+ arr = j === 2 ? [name] : name;
136
+ for (idx = 0; idx < arr.length; idx++) {
137
+ name = arr[idx];
138
+ if (strict && !~keys.indexOf(name)) {
139
+ return opts.unknown("-".repeat(j) + name);
140
+ }
141
+ toVal(out, name, idx + 1 < arr.length || val, opts);
142
+ }
143
+ }
144
+ }
145
+ if (defaults) {
146
+ for (k in opts.default) {
147
+ if (out[k] === void 0) {
148
+ out[k] = opts.default[k];
149
+ }
150
+ }
151
+ }
152
+ if (alibi) {
153
+ for (k in out) {
154
+ arr = opts.alias[k] || [];
155
+ while (arr.length > 0) {
156
+ out[arr.shift()] = out[k];
157
+ }
158
+ }
159
+ }
160
+ return out;
161
+ }
162
+
163
+ function parseArgs(rawArgs, argsDef) {
164
+ const parseOptions = {
165
+ boolean: [],
166
+ string: [],
167
+ alias: {},
168
+ default: {}
169
+ };
170
+ const args = resolveArgs(argsDef);
171
+ for (const arg of args) {
172
+ if (arg.type === "positional") {
173
+ continue;
174
+ }
175
+ parseOptions[arg.type || "boolean"].push(arg.name);
176
+ if (arg.default !== void 0) {
177
+ parseOptions.default[arg.name] = arg.default;
178
+ }
179
+ if (arg.alias) {
180
+ parseOptions.alias[arg.name] = arg.alias;
181
+ }
182
+ }
183
+ const parsed = parseRawArgs(rawArgs, parseOptions);
184
+ const [, ...positionalArguments] = parsed._;
185
+ const parsedArgsProxy = new Proxy(parsed, {
186
+ get(target, prop) {
187
+ return target[prop] ?? target[scule.camelCase(prop)] ?? target[scule.kebabCase(prop)];
188
+ }
189
+ });
190
+ for (const [, arg] of args.entries()) {
191
+ if (arg.type === "positional") {
192
+ const nextPositionalArgument = positionalArguments.shift();
193
+ if (nextPositionalArgument !== void 0) {
194
+ parsedArgsProxy[arg.name] = nextPositionalArgument;
195
+ } else if (arg.default !== void 0) {
196
+ parsedArgsProxy[arg.name] = arg.default;
197
+ } else {
198
+ throw new CLIError(
199
+ `Missing required positional argument: ${arg.name.toUpperCase()}`,
200
+ "EARG"
201
+ );
202
+ }
203
+ } else if (arg.required && parsedArgsProxy[arg.name] === void 0) {
204
+ throw new CLIError(`Missing required argument: --${arg.name}`, "EARG");
205
+ }
206
+ }
207
+ return parsedArgsProxy;
208
+ }
209
+ function resolveArgs(argsDef) {
210
+ const args = [];
211
+ for (const [name, argDef] of Object.entries(argsDef || {})) {
212
+ args.push({
213
+ ...argDef,
214
+ name,
215
+ alias: toArray(argDef.alias)
216
+ });
217
+ }
218
+ return args;
219
+ }
220
+
221
+ function defineCommand(def) {
222
+ return def;
223
+ }
224
+ async function runCommand(cmd, opts) {
225
+ const cmdArgs = await resolveValue(cmd.args || {});
226
+ const parsedArgs = parseArgs(opts.rawArgs, cmdArgs);
227
+ const context = {
228
+ rawArgs: opts.rawArgs,
229
+ args: parsedArgs,
230
+ cmd
231
+ };
232
+ if (typeof cmd.setup === "function") {
233
+ await cmd.setup(context);
234
+ }
235
+ const subCommands = await resolveValue(cmd.subCommands);
236
+ if (subCommands && Object.keys(subCommands).length > 0) {
237
+ const subCommandArgIndex = opts.rawArgs.findIndex(
238
+ (arg) => !arg.startsWith("-")
239
+ );
240
+ const subCommandName = opts.rawArgs[subCommandArgIndex];
241
+ if (!subCommandName && !cmd.run) {
242
+ throw new CLIError(
243
+ `Missing sub command. Use --help to see available sub commands.`,
244
+ "ESUBCOMMAND"
245
+ );
246
+ }
247
+ if (!subCommands[subCommandName]) {
248
+ throw new CLIError(
249
+ `Unknown sub command: ${subCommandName}`,
250
+ "ESUBCOMMAND"
251
+ );
252
+ }
253
+ const subCommand = await resolveValue(subCommands[subCommandName]);
254
+ if (subCommand) {
255
+ await runCommand(subCommand, {
256
+ rawArgs: opts.rawArgs.slice(subCommandArgIndex)
257
+ });
258
+ }
259
+ }
260
+ if (typeof cmd.run === "function") {
261
+ await cmd.run(context);
262
+ }
263
+ }
264
+ async function resolveSubCommand(cmd, rawArgs, parent) {
265
+ const subCommands = await resolveValue(cmd.subCommands);
266
+ if (subCommands && Object.keys(subCommands).length > 0) {
267
+ const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
268
+ const subCommandName = rawArgs[subCommandArgIndex];
269
+ const subCommand = await resolveValue(subCommands[subCommandName]);
270
+ if (subCommand) {
271
+ return resolveSubCommand(
272
+ subCommand,
273
+ rawArgs.slice(subCommandArgIndex),
274
+ cmd
275
+ );
276
+ }
277
+ }
278
+ return [cmd, parent];
279
+ }
280
+
281
+ async function showUsage(cmd, parent) {
282
+ try {
283
+ console.log(await renderUsage(cmd, parent) + "\n");
284
+ } catch (error) {
285
+ console.error(error);
286
+ }
287
+ }
288
+ async function renderUsage(cmd, parent) {
289
+ const cmdMeta = await resolveValue(cmd.meta || {});
290
+ const cmdArgs = resolveArgs(await resolveValue(cmd.args || {}));
291
+ const parentMeta = await resolveValue(parent?.meta || {});
292
+ const commandName = `${parentMeta.name ? `${parentMeta.name} ` : ""}` + (cmdMeta.name || process.argv[1]);
293
+ const argLines = [];
294
+ const posLines = [];
295
+ const commandsLines = [];
296
+ const usageLine = [];
297
+ for (const arg of cmdArgs) {
298
+ if (arg.type === "positional") {
299
+ const name = arg.name.toUpperCase();
300
+ const isRequired = arg.required !== false && arg.default === void 0;
301
+ const usageHint = arg.default ? `="${arg.default}"` : "";
302
+ posLines.push([name + usageHint, arg.description || ""]);
303
+ usageLine.push(isRequired ? `<${name}>` : `[${name}]`);
304
+ } else {
305
+ const isRequired = arg.required === true && arg.default === void 0;
306
+ const argStr = (arg.type === "boolean" && arg.default === true ? [
307
+ ...(arg.alias || []).map((a) => `--no-${a}`),
308
+ `--no-${arg.name}`
309
+ ].join(", ") : [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(
310
+ ", "
311
+ )) + (arg.type === "string" && (arg.valueHint || arg.default) ? `=${arg.valueHint ? `<${arg.valueHint}>` : `"${arg.default || ""}"`}` : "");
312
+ argLines.push([
313
+ argStr + (isRequired ? " (required)" : ""),
314
+ arg.description || ""
315
+ ]);
316
+ if (isRequired) {
317
+ usageLine.push(argStr);
318
+ }
319
+ }
320
+ }
321
+ if (cmd.subCommands) {
322
+ const commandNames = [];
323
+ for (const [name, sub] of Object.entries(cmd.subCommands)) {
324
+ commandsLines.push([name, sub.meta?.description || ""]);
325
+ commandNames.push(name);
326
+ }
327
+ usageLine.push(commandNames.join("|"));
328
+ }
329
+ const usageLines = [];
330
+ const version = cmdMeta.version || parentMeta.version;
331
+ usageLines.push(
332
+ commandName + (version ? ` v${version}` : ""),
333
+ cmdMeta.description,
334
+ ""
335
+ );
336
+ const hasOptions = argLines.length > 0 || posLines.length > 0;
337
+ usageLines.push(
338
+ `USAGE: ${commandName}${hasOptions ? " [OPTIONS]" : ""} ${usageLine.join(
339
+ " "
340
+ )}`,
341
+ ""
342
+ );
343
+ if (posLines.length > 0) {
344
+ usageLines.push("ARGUMENTS:", "");
345
+ usageLines.push(formatLineColumns(posLines, " "));
346
+ usageLines.push("");
347
+ }
348
+ if (argLines.length > 0) {
349
+ usageLines.push("OPTIONS:", "");
350
+ usageLines.push(formatLineColumns(argLines, " "));
351
+ usageLines.push("");
352
+ }
353
+ if (commandsLines.length > 0) {
354
+ usageLines.push("COMMANDS:", "");
355
+ usageLines.push(formatLineColumns(commandsLines, " "));
356
+ usageLines.push(
357
+ "",
358
+ `Use \`${commandName} <command> --help\` for more information about a command.`
359
+ );
360
+ }
361
+ return usageLines.filter((l) => typeof l === "string").join("\n");
362
+ }
363
+
364
+ const {
365
+ env = {},
366
+ argv = [],
367
+ platform = "",
368
+ } = typeof process === "undefined" ? {} : process;
369
+
370
+ const isDisabled = "NO_COLOR" in env || argv.includes("--no-color");
371
+ const isForced = "FORCE_COLOR" in env || argv.includes("--color");
372
+ const isWindows = platform === "win32";
373
+ const isDumbTerminal = env.TERM === "dumb";
374
+
375
+ const isCompatibleTerminal =
376
+ tty__namespace && tty__namespace.isatty && tty__namespace.isatty(1) && env.TERM && !isDumbTerminal;
377
+
378
+ const isCI =
379
+ "CI" in env &&
380
+ ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env);
381
+
382
+ const isColorSupported =
383
+ !isDisabled &&
384
+ (isForced || (isWindows && !isDumbTerminal) || isCompatibleTerminal || isCI);
385
+
386
+ const replaceClose = (
387
+ index,
388
+ string,
389
+ close,
390
+ replace,
391
+ head = string.substring(0, index) + replace,
392
+ tail = string.substring(index + close.length),
393
+ next = tail.indexOf(close)
394
+ ) => head + (next < 0 ? tail : replaceClose(next, tail, close, replace));
395
+
396
+ const clearBleed = (index, string, open, close, replace) =>
397
+ index < 0
398
+ ? open + string + close
399
+ : open + replaceClose(index, string, close, replace) + close;
400
+
401
+ const filterEmpty =
402
+ (open, close, replace = open, at = open.length + 1) =>
403
+ (string) =>
404
+ string || !(string === "" || string === undefined)
405
+ ? clearBleed(
406
+ ("" + string).indexOf(close, at),
407
+ string,
408
+ open,
409
+ close,
410
+ replace
411
+ )
412
+ : "";
413
+
414
+ const init = (open, close, replace) =>
415
+ filterEmpty(`\x1b[${open}m`, `\x1b[${close}m`, replace);
416
+
417
+ const colors = {
418
+ reset: init(0, 0),
419
+ bold: init(1, 22, "\x1b[22m\x1b[1m"),
420
+ dim: init(2, 22, "\x1b[22m\x1b[2m"),
421
+ italic: init(3, 23),
422
+ underline: init(4, 24),
423
+ inverse: init(7, 27),
424
+ hidden: init(8, 28),
425
+ strikethrough: init(9, 29),
426
+ black: init(30, 39),
427
+ red: init(31, 39),
428
+ green: init(32, 39),
429
+ yellow: init(33, 39),
430
+ blue: init(34, 39),
431
+ magenta: init(35, 39),
432
+ cyan: init(36, 39),
433
+ white: init(37, 39),
434
+ gray: init(90, 39),
435
+ bgBlack: init(40, 49),
436
+ bgRed: init(41, 49),
437
+ bgGreen: init(42, 49),
438
+ bgYellow: init(43, 49),
439
+ bgBlue: init(44, 49),
440
+ bgMagenta: init(45, 49),
441
+ bgCyan: init(46, 49),
442
+ bgWhite: init(47, 49),
443
+ blackBright: init(90, 39),
444
+ redBright: init(91, 39),
445
+ greenBright: init(92, 39),
446
+ yellowBright: init(93, 39),
447
+ blueBright: init(94, 39),
448
+ magentaBright: init(95, 39),
449
+ cyanBright: init(96, 39),
450
+ whiteBright: init(97, 39),
451
+ bgBlackBright: init(100, 49),
452
+ bgRedBright: init(101, 49),
453
+ bgGreenBright: init(102, 49),
454
+ bgYellowBright: init(103, 49),
455
+ bgBlueBright: init(104, 49),
456
+ bgMagentaBright: init(105, 49),
457
+ bgCyanBright: init(106, 49),
458
+ bgWhiteBright: init(107, 49),
459
+ };
460
+
461
+ const createColors = ({ useColor = isColorSupported } = {}) =>
462
+ useColor
463
+ ? colors
464
+ : Object.keys(colors).reduce(
465
+ (colors, key) => ({ ...colors, [key]: String }),
466
+ {}
467
+ );
468
+
469
+ const {
470
+ reset,
471
+ bold,
472
+ dim,
473
+ italic,
474
+ underline,
475
+ inverse,
476
+ hidden,
477
+ strikethrough,
478
+ black,
479
+ red,
480
+ green,
481
+ yellow,
482
+ blue,
483
+ magenta,
484
+ cyan,
485
+ white,
486
+ gray,
487
+ bgBlack,
488
+ bgRed,
489
+ bgGreen,
490
+ bgYellow,
491
+ bgBlue,
492
+ bgMagenta,
493
+ bgCyan,
494
+ bgWhite,
495
+ blackBright,
496
+ redBright,
497
+ greenBright,
498
+ yellowBright,
499
+ blueBright,
500
+ magentaBright,
501
+ cyanBright,
502
+ whiteBright,
503
+ bgBlackBright,
504
+ bgRedBright,
505
+ bgGreenBright,
506
+ bgYellowBright,
507
+ bgBlueBright,
508
+ bgMagentaBright,
509
+ bgCyanBright,
510
+ bgWhiteBright,
511
+ } = createColors();
512
+
513
+ async function runMain(cmd, opts = {}) {
514
+ const rawArgs = opts.rawArgs || process.argv.slice(2);
515
+ try {
516
+ if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
517
+ await showUsage(...await resolveSubCommand(cmd, rawArgs));
518
+ process.exit(0);
519
+ } else {
520
+ await runCommand(cmd, { rawArgs });
521
+ }
522
+ } catch (error) {
523
+ const isCLIError = error instanceof CLIError;
524
+ if (!isCLIError) {
525
+ console.error(error, "\n");
526
+ }
527
+ console.error(
528
+ `
529
+ ${bgRed(` ${error.code || error.name} `)} ${error.message}
530
+ `
531
+ );
532
+ if (isCLIError) {
533
+ await showUsage(...await resolveSubCommand(cmd, rawArgs));
534
+ }
535
+ process.exit(1);
536
+ }
537
+ }
538
+
539
+ exports.defineCommand = defineCommand;
540
+ exports.parseArgs = parseArgs;
541
+ exports.renderUsage = renderUsage;
542
+ exports.runCommand = runCommand;
543
+ exports.runMain = runMain;
544
+ exports.showUsage = showUsage;
@@ -0,0 +1,74 @@
1
+ type ArgType = "boolean" | "string" | "positional" | undefined;
2
+ type _ArgDef<T extends ArgType, VT extends boolean | string> = {
3
+ type?: T;
4
+ description?: string;
5
+ valueHint?: string;
6
+ alias?: string | string[];
7
+ default?: VT;
8
+ required?: boolean;
9
+ };
10
+ type BooleanArgDef = _ArgDef<"boolean", boolean>;
11
+ type StringArgDef = _ArgDef<"string", string>;
12
+ type PositionalArgDef = Omit<_ArgDef<"positional", string>, "alias">;
13
+ type ArgDef = BooleanArgDef | StringArgDef | PositionalArgDef;
14
+ type ArgsDef = Record<string, ArgDef>;
15
+ type Arg = ArgDef & {
16
+ name: string;
17
+ alias: string[];
18
+ };
19
+ type ParsedArgs<T extends ArgsDef = ArgsDef> = {
20
+ _: string[];
21
+ } & Record<{
22
+ [K in keyof T]: T[K] extends {
23
+ type: "positional";
24
+ } ? K : never;
25
+ }[keyof T], string | boolean> & Record<{
26
+ [K in keyof T]: T[K] extends {
27
+ type: "string";
28
+ } ? K : never;
29
+ }[keyof T], string> & Record<{
30
+ [K in keyof T]: T[K] extends {
31
+ type: "boolean";
32
+ } ? K : never;
33
+ }[keyof T], boolean>;
34
+ interface CommandMeta {
35
+ name?: string;
36
+ version?: string;
37
+ description?: string;
38
+ }
39
+ type SubCommandsDef = Record<string, Resolvable<CommandDef<any>>>;
40
+ type CommandDef<T extends ArgsDef = ArgsDef> = {
41
+ meta?: Resolvable<CommandMeta>;
42
+ args?: Resolvable<T>;
43
+ subCommands?: Resolvable<SubCommandsDef>;
44
+ setup?: (context: CommandContext<T>) => any | Promise<any>;
45
+ cleanup?: (context: CommandContext<T>) => any | Promise<any>;
46
+ run?: (context: CommandContext<T>) => any | Promise<any>;
47
+ };
48
+ type CommandContext<T extends ArgsDef = ArgsDef> = {
49
+ rawArgs: string[];
50
+ args: ParsedArgs<T>;
51
+ cmd: CommandDef;
52
+ subCommand?: CommandDef<T>;
53
+ };
54
+ type Awaitable<T> = () => T | Promise<T>;
55
+ type Resolvable<T> = T | Promise<T> | (() => T) | (() => Promise<T>);
56
+
57
+ declare function defineCommand<T extends ArgsDef = ArgsDef>(def: CommandDef<T>): CommandDef<T>;
58
+ interface RunCommandOptions {
59
+ rawArgs: string[];
60
+ showUsage?: boolean;
61
+ }
62
+ declare function runCommand(cmd: CommandDef, opts: RunCommandOptions): Promise<void>;
63
+
64
+ interface RunMainOptions {
65
+ rawArgs?: string[];
66
+ }
67
+ declare function runMain(cmd: CommandDef, opts?: RunMainOptions): Promise<void>;
68
+
69
+ declare function parseArgs(rawArgs: string[], argsDef: ArgsDef): ParsedArgs;
70
+
71
+ declare function showUsage(cmd: CommandDef, parent?: CommandDef): Promise<void>;
72
+ declare function renderUsage(cmd: CommandDef, parent?: CommandDef): Promise<string>;
73
+
74
+ export { Arg, ArgDef, ArgType, ArgsDef, Awaitable, BooleanArgDef, CommandContext, CommandDef, CommandMeta, ParsedArgs, PositionalArgDef, Resolvable, RunCommandOptions, RunMainOptions, StringArgDef, SubCommandsDef, _ArgDef, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
package/dist/index.mjs ADDED
@@ -0,0 +1,524 @@
1
+ import { camelCase, kebabCase } from 'scule';
2
+ import * as tty from 'tty';
3
+
4
+ function toArray(val) {
5
+ if (Array.isArray(val)) {
6
+ return val;
7
+ }
8
+ return val !== void 0 ? [val] : [];
9
+ }
10
+ function formatLineColumns(lines, linePrefix = "") {
11
+ const maxLengh = [];
12
+ for (const line of lines) {
13
+ for (const [i, element] of line.entries()) {
14
+ maxLengh[i] = Math.max(maxLengh[i] || 0, element.length);
15
+ }
16
+ }
17
+ return lines.map(
18
+ (l) => l.map(
19
+ (c, i) => linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLengh[i])
20
+ ).join(" ")
21
+ ).join("\n");
22
+ }
23
+ function resolveValue(input) {
24
+ return typeof input === "function" ? input() : input;
25
+ }
26
+ class CLIError extends Error {
27
+ constructor(message, code) {
28
+ super(message);
29
+ this.code = code;
30
+ this.name = "CLIError";
31
+ }
32
+ }
33
+
34
+ function toArr(any) {
35
+ return any == void 0 ? [] : Array.isArray(any) ? any : [any];
36
+ }
37
+ function toVal(out, key, val, opts) {
38
+ let x;
39
+ const old = out[key];
40
+ const nxt = ~opts.string.indexOf(key) ? val == void 0 || val === true ? "" : String(val) : typeof val === "boolean" ? val : ~opts.boolean.indexOf(key) ? val === "false" ? false : val === "true" || (out._.push((x = +val, x * 0 === 0) ? x : val), !!val) : (x = +val, x * 0 === 0) ? x : val;
41
+ out[key] = old == void 0 ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
42
+ }
43
+ function parseRawArgs(args = [], opts = {}) {
44
+ let k;
45
+ let arr;
46
+ let arg;
47
+ let name;
48
+ let val;
49
+ const out = { _: [] };
50
+ let i = 0;
51
+ let j = 0;
52
+ let idx = 0;
53
+ const len = args.length;
54
+ const alibi = opts.alias !== void 0;
55
+ const strict = opts.unknown !== void 0;
56
+ const defaults = opts.default !== void 0;
57
+ opts.alias = opts.alias || {};
58
+ opts.string = toArr(opts.string);
59
+ opts.boolean = toArr(opts.boolean);
60
+ if (alibi) {
61
+ for (k in opts.alias) {
62
+ arr = opts.alias[k] = toArr(opts.alias[k]);
63
+ for (i = 0; i < arr.length; i++) {
64
+ (opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
65
+ }
66
+ }
67
+ }
68
+ for (i = opts.boolean.length; i-- > 0; ) {
69
+ arr = opts.alias[opts.boolean[i]] || [];
70
+ for (j = arr.length; j-- > 0; ) {
71
+ opts.boolean.push(arr[j]);
72
+ }
73
+ }
74
+ for (i = opts.string.length; i-- > 0; ) {
75
+ arr = opts.alias[opts.string[i]] || [];
76
+ for (j = arr.length; j-- > 0; ) {
77
+ opts.string.push(arr[j]);
78
+ }
79
+ }
80
+ if (defaults) {
81
+ for (k in opts.default) {
82
+ name = typeof opts.default[k];
83
+ arr = opts.alias[k] = opts.alias[k] || [];
84
+ if (opts[name] !== void 0) {
85
+ opts[name].push(k);
86
+ for (i = 0; i < arr.length; i++) {
87
+ opts[name].push(arr[i]);
88
+ }
89
+ }
90
+ }
91
+ }
92
+ const keys = strict ? Object.keys(opts.alias) : [];
93
+ for (i = 0; i < len; i++) {
94
+ arg = args[i];
95
+ if (arg === "--") {
96
+ out._ = out._.concat(args.slice(++i));
97
+ break;
98
+ }
99
+ for (j = 0; j < arg.length; j++) {
100
+ if (arg.charCodeAt(j) !== 45) {
101
+ break;
102
+ }
103
+ }
104
+ if (j === 0) {
105
+ out._.push(arg);
106
+ } else if (arg.substring(j, j + 3) === "no-") {
107
+ name = arg.slice(Math.max(0, j + 3));
108
+ if (strict && !~keys.indexOf(name)) {
109
+ return opts.unknown(arg);
110
+ }
111
+ out[name] = false;
112
+ } else {
113
+ for (idx = j + 1; idx < arg.length; idx++) {
114
+ if (arg.charCodeAt(idx) === 61) {
115
+ break;
116
+ }
117
+ }
118
+ name = arg.substring(j, idx);
119
+ val = arg.slice(Math.max(0, ++idx)) || i + 1 === len || ("" + args[i + 1]).charCodeAt(0) === 45 || args[++i];
120
+ arr = j === 2 ? [name] : name;
121
+ for (idx = 0; idx < arr.length; idx++) {
122
+ name = arr[idx];
123
+ if (strict && !~keys.indexOf(name)) {
124
+ return opts.unknown("-".repeat(j) + name);
125
+ }
126
+ toVal(out, name, idx + 1 < arr.length || val, opts);
127
+ }
128
+ }
129
+ }
130
+ if (defaults) {
131
+ for (k in opts.default) {
132
+ if (out[k] === void 0) {
133
+ out[k] = opts.default[k];
134
+ }
135
+ }
136
+ }
137
+ if (alibi) {
138
+ for (k in out) {
139
+ arr = opts.alias[k] || [];
140
+ while (arr.length > 0) {
141
+ out[arr.shift()] = out[k];
142
+ }
143
+ }
144
+ }
145
+ return out;
146
+ }
147
+
148
+ function parseArgs(rawArgs, argsDef) {
149
+ const parseOptions = {
150
+ boolean: [],
151
+ string: [],
152
+ alias: {},
153
+ default: {}
154
+ };
155
+ const args = resolveArgs(argsDef);
156
+ for (const arg of args) {
157
+ if (arg.type === "positional") {
158
+ continue;
159
+ }
160
+ parseOptions[arg.type || "boolean"].push(arg.name);
161
+ if (arg.default !== void 0) {
162
+ parseOptions.default[arg.name] = arg.default;
163
+ }
164
+ if (arg.alias) {
165
+ parseOptions.alias[arg.name] = arg.alias;
166
+ }
167
+ }
168
+ const parsed = parseRawArgs(rawArgs, parseOptions);
169
+ const [, ...positionalArguments] = parsed._;
170
+ const parsedArgsProxy = new Proxy(parsed, {
171
+ get(target, prop) {
172
+ return target[prop] ?? target[camelCase(prop)] ?? target[kebabCase(prop)];
173
+ }
174
+ });
175
+ for (const [, arg] of args.entries()) {
176
+ if (arg.type === "positional") {
177
+ const nextPositionalArgument = positionalArguments.shift();
178
+ if (nextPositionalArgument !== void 0) {
179
+ parsedArgsProxy[arg.name] = nextPositionalArgument;
180
+ } else if (arg.default !== void 0) {
181
+ parsedArgsProxy[arg.name] = arg.default;
182
+ } else {
183
+ throw new CLIError(
184
+ `Missing required positional argument: ${arg.name.toUpperCase()}`,
185
+ "EARG"
186
+ );
187
+ }
188
+ } else if (arg.required && parsedArgsProxy[arg.name] === void 0) {
189
+ throw new CLIError(`Missing required argument: --${arg.name}`, "EARG");
190
+ }
191
+ }
192
+ return parsedArgsProxy;
193
+ }
194
+ function resolveArgs(argsDef) {
195
+ const args = [];
196
+ for (const [name, argDef] of Object.entries(argsDef || {})) {
197
+ args.push({
198
+ ...argDef,
199
+ name,
200
+ alias: toArray(argDef.alias)
201
+ });
202
+ }
203
+ return args;
204
+ }
205
+
206
+ function defineCommand(def) {
207
+ return def;
208
+ }
209
+ async function runCommand(cmd, opts) {
210
+ const cmdArgs = await resolveValue(cmd.args || {});
211
+ const parsedArgs = parseArgs(opts.rawArgs, cmdArgs);
212
+ const context = {
213
+ rawArgs: opts.rawArgs,
214
+ args: parsedArgs,
215
+ cmd
216
+ };
217
+ if (typeof cmd.setup === "function") {
218
+ await cmd.setup(context);
219
+ }
220
+ const subCommands = await resolveValue(cmd.subCommands);
221
+ if (subCommands && Object.keys(subCommands).length > 0) {
222
+ const subCommandArgIndex = opts.rawArgs.findIndex(
223
+ (arg) => !arg.startsWith("-")
224
+ );
225
+ const subCommandName = opts.rawArgs[subCommandArgIndex];
226
+ if (!subCommandName && !cmd.run) {
227
+ throw new CLIError(
228
+ `Missing sub command. Use --help to see available sub commands.`,
229
+ "ESUBCOMMAND"
230
+ );
231
+ }
232
+ if (!subCommands[subCommandName]) {
233
+ throw new CLIError(
234
+ `Unknown sub command: ${subCommandName}`,
235
+ "ESUBCOMMAND"
236
+ );
237
+ }
238
+ const subCommand = await resolveValue(subCommands[subCommandName]);
239
+ if (subCommand) {
240
+ await runCommand(subCommand, {
241
+ rawArgs: opts.rawArgs.slice(subCommandArgIndex)
242
+ });
243
+ }
244
+ }
245
+ if (typeof cmd.run === "function") {
246
+ await cmd.run(context);
247
+ }
248
+ }
249
+ async function resolveSubCommand(cmd, rawArgs, parent) {
250
+ const subCommands = await resolveValue(cmd.subCommands);
251
+ if (subCommands && Object.keys(subCommands).length > 0) {
252
+ const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
253
+ const subCommandName = rawArgs[subCommandArgIndex];
254
+ const subCommand = await resolveValue(subCommands[subCommandName]);
255
+ if (subCommand) {
256
+ return resolveSubCommand(
257
+ subCommand,
258
+ rawArgs.slice(subCommandArgIndex),
259
+ cmd
260
+ );
261
+ }
262
+ }
263
+ return [cmd, parent];
264
+ }
265
+
266
+ async function showUsage(cmd, parent) {
267
+ try {
268
+ console.log(await renderUsage(cmd, parent) + "\n");
269
+ } catch (error) {
270
+ console.error(error);
271
+ }
272
+ }
273
+ async function renderUsage(cmd, parent) {
274
+ const cmdMeta = await resolveValue(cmd.meta || {});
275
+ const cmdArgs = resolveArgs(await resolveValue(cmd.args || {}));
276
+ const parentMeta = await resolveValue(parent?.meta || {});
277
+ const commandName = `${parentMeta.name ? `${parentMeta.name} ` : ""}` + (cmdMeta.name || process.argv[1]);
278
+ const argLines = [];
279
+ const posLines = [];
280
+ const commandsLines = [];
281
+ const usageLine = [];
282
+ for (const arg of cmdArgs) {
283
+ if (arg.type === "positional") {
284
+ const name = arg.name.toUpperCase();
285
+ const isRequired = arg.required !== false && arg.default === void 0;
286
+ const usageHint = arg.default ? `="${arg.default}"` : "";
287
+ posLines.push([name + usageHint, arg.description || ""]);
288
+ usageLine.push(isRequired ? `<${name}>` : `[${name}]`);
289
+ } else {
290
+ const isRequired = arg.required === true && arg.default === void 0;
291
+ const argStr = (arg.type === "boolean" && arg.default === true ? [
292
+ ...(arg.alias || []).map((a) => `--no-${a}`),
293
+ `--no-${arg.name}`
294
+ ].join(", ") : [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(
295
+ ", "
296
+ )) + (arg.type === "string" && (arg.valueHint || arg.default) ? `=${arg.valueHint ? `<${arg.valueHint}>` : `"${arg.default || ""}"`}` : "");
297
+ argLines.push([
298
+ argStr + (isRequired ? " (required)" : ""),
299
+ arg.description || ""
300
+ ]);
301
+ if (isRequired) {
302
+ usageLine.push(argStr);
303
+ }
304
+ }
305
+ }
306
+ if (cmd.subCommands) {
307
+ const commandNames = [];
308
+ for (const [name, sub] of Object.entries(cmd.subCommands)) {
309
+ commandsLines.push([name, sub.meta?.description || ""]);
310
+ commandNames.push(name);
311
+ }
312
+ usageLine.push(commandNames.join("|"));
313
+ }
314
+ const usageLines = [];
315
+ const version = cmdMeta.version || parentMeta.version;
316
+ usageLines.push(
317
+ commandName + (version ? ` v${version}` : ""),
318
+ cmdMeta.description,
319
+ ""
320
+ );
321
+ const hasOptions = argLines.length > 0 || posLines.length > 0;
322
+ usageLines.push(
323
+ `USAGE: ${commandName}${hasOptions ? " [OPTIONS]" : ""} ${usageLine.join(
324
+ " "
325
+ )}`,
326
+ ""
327
+ );
328
+ if (posLines.length > 0) {
329
+ usageLines.push("ARGUMENTS:", "");
330
+ usageLines.push(formatLineColumns(posLines, " "));
331
+ usageLines.push("");
332
+ }
333
+ if (argLines.length > 0) {
334
+ usageLines.push("OPTIONS:", "");
335
+ usageLines.push(formatLineColumns(argLines, " "));
336
+ usageLines.push("");
337
+ }
338
+ if (commandsLines.length > 0) {
339
+ usageLines.push("COMMANDS:", "");
340
+ usageLines.push(formatLineColumns(commandsLines, " "));
341
+ usageLines.push(
342
+ "",
343
+ `Use \`${commandName} <command> --help\` for more information about a command.`
344
+ );
345
+ }
346
+ return usageLines.filter((l) => typeof l === "string").join("\n");
347
+ }
348
+
349
+ const {
350
+ env = {},
351
+ argv = [],
352
+ platform = "",
353
+ } = typeof process === "undefined" ? {} : process;
354
+
355
+ const isDisabled = "NO_COLOR" in env || argv.includes("--no-color");
356
+ const isForced = "FORCE_COLOR" in env || argv.includes("--color");
357
+ const isWindows = platform === "win32";
358
+ const isDumbTerminal = env.TERM === "dumb";
359
+
360
+ const isCompatibleTerminal =
361
+ tty && tty.isatty && tty.isatty(1) && env.TERM && !isDumbTerminal;
362
+
363
+ const isCI =
364
+ "CI" in env &&
365
+ ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env);
366
+
367
+ const isColorSupported =
368
+ !isDisabled &&
369
+ (isForced || (isWindows && !isDumbTerminal) || isCompatibleTerminal || isCI);
370
+
371
+ const replaceClose = (
372
+ index,
373
+ string,
374
+ close,
375
+ replace,
376
+ head = string.substring(0, index) + replace,
377
+ tail = string.substring(index + close.length),
378
+ next = tail.indexOf(close)
379
+ ) => head + (next < 0 ? tail : replaceClose(next, tail, close, replace));
380
+
381
+ const clearBleed = (index, string, open, close, replace) =>
382
+ index < 0
383
+ ? open + string + close
384
+ : open + replaceClose(index, string, close, replace) + close;
385
+
386
+ const filterEmpty =
387
+ (open, close, replace = open, at = open.length + 1) =>
388
+ (string) =>
389
+ string || !(string === "" || string === undefined)
390
+ ? clearBleed(
391
+ ("" + string).indexOf(close, at),
392
+ string,
393
+ open,
394
+ close,
395
+ replace
396
+ )
397
+ : "";
398
+
399
+ const init = (open, close, replace) =>
400
+ filterEmpty(`\x1b[${open}m`, `\x1b[${close}m`, replace);
401
+
402
+ const colors = {
403
+ reset: init(0, 0),
404
+ bold: init(1, 22, "\x1b[22m\x1b[1m"),
405
+ dim: init(2, 22, "\x1b[22m\x1b[2m"),
406
+ italic: init(3, 23),
407
+ underline: init(4, 24),
408
+ inverse: init(7, 27),
409
+ hidden: init(8, 28),
410
+ strikethrough: init(9, 29),
411
+ black: init(30, 39),
412
+ red: init(31, 39),
413
+ green: init(32, 39),
414
+ yellow: init(33, 39),
415
+ blue: init(34, 39),
416
+ magenta: init(35, 39),
417
+ cyan: init(36, 39),
418
+ white: init(37, 39),
419
+ gray: init(90, 39),
420
+ bgBlack: init(40, 49),
421
+ bgRed: init(41, 49),
422
+ bgGreen: init(42, 49),
423
+ bgYellow: init(43, 49),
424
+ bgBlue: init(44, 49),
425
+ bgMagenta: init(45, 49),
426
+ bgCyan: init(46, 49),
427
+ bgWhite: init(47, 49),
428
+ blackBright: init(90, 39),
429
+ redBright: init(91, 39),
430
+ greenBright: init(92, 39),
431
+ yellowBright: init(93, 39),
432
+ blueBright: init(94, 39),
433
+ magentaBright: init(95, 39),
434
+ cyanBright: init(96, 39),
435
+ whiteBright: init(97, 39),
436
+ bgBlackBright: init(100, 49),
437
+ bgRedBright: init(101, 49),
438
+ bgGreenBright: init(102, 49),
439
+ bgYellowBright: init(103, 49),
440
+ bgBlueBright: init(104, 49),
441
+ bgMagentaBright: init(105, 49),
442
+ bgCyanBright: init(106, 49),
443
+ bgWhiteBright: init(107, 49),
444
+ };
445
+
446
+ const createColors = ({ useColor = isColorSupported } = {}) =>
447
+ useColor
448
+ ? colors
449
+ : Object.keys(colors).reduce(
450
+ (colors, key) => ({ ...colors, [key]: String }),
451
+ {}
452
+ );
453
+
454
+ const {
455
+ reset,
456
+ bold,
457
+ dim,
458
+ italic,
459
+ underline,
460
+ inverse,
461
+ hidden,
462
+ strikethrough,
463
+ black,
464
+ red,
465
+ green,
466
+ yellow,
467
+ blue,
468
+ magenta,
469
+ cyan,
470
+ white,
471
+ gray,
472
+ bgBlack,
473
+ bgRed,
474
+ bgGreen,
475
+ bgYellow,
476
+ bgBlue,
477
+ bgMagenta,
478
+ bgCyan,
479
+ bgWhite,
480
+ blackBright,
481
+ redBright,
482
+ greenBright,
483
+ yellowBright,
484
+ blueBright,
485
+ magentaBright,
486
+ cyanBright,
487
+ whiteBright,
488
+ bgBlackBright,
489
+ bgRedBright,
490
+ bgGreenBright,
491
+ bgYellowBright,
492
+ bgBlueBright,
493
+ bgMagentaBright,
494
+ bgCyanBright,
495
+ bgWhiteBright,
496
+ } = createColors();
497
+
498
+ async function runMain(cmd, opts = {}) {
499
+ const rawArgs = opts.rawArgs || process.argv.slice(2);
500
+ try {
501
+ if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
502
+ await showUsage(...await resolveSubCommand(cmd, rawArgs));
503
+ process.exit(0);
504
+ } else {
505
+ await runCommand(cmd, { rawArgs });
506
+ }
507
+ } catch (error) {
508
+ const isCLIError = error instanceof CLIError;
509
+ if (!isCLIError) {
510
+ console.error(error, "\n");
511
+ }
512
+ console.error(
513
+ `
514
+ ${bgRed(` ${error.code || error.name} `)} ${error.message}
515
+ `
516
+ );
517
+ if (isCLIError) {
518
+ await showUsage(...await resolveSubCommand(cmd, rawArgs));
519
+ }
520
+ process.exit(1);
521
+ }
522
+ }
523
+
524
+ export { defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
package/package.json CHANGED
@@ -1,5 +1,49 @@
1
1
  {
2
- "name": "citty",
3
- "version": "0.0.0",
4
- "license": "MIT"
2
+ "name": "citty",
3
+ "version": "0.0.2",
4
+ "description": "Elegant CLI Builder",
5
+ "repository": "unjs/citty",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "main": "./dist/index.cjs",
17
+ "module": "./dist/index.mjs",
18
+ "types": "./dist/index.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "unbuild",
24
+ "dev": "vitest dev",
25
+ "lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
26
+ "lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test -w",
27
+ "prepack": "pnpm run build",
28
+ "play": "jiti ./playground/cli.ts",
29
+ "release": "pnpm test && changelogen --release --push && npm publish",
30
+ "test": "pnpm lint && vitest run --coverage"
31
+ },
32
+ "dependencies": {
33
+ "scule": "^1.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^18.15.10",
37
+ "@vitest/coverage-c8": "^0.29.8",
38
+ "changelogen": "^0.5.2",
39
+ "colorette": "^2.0.19",
40
+ "eslint": "^8.36.0",
41
+ "eslint-config-unjs": "^0.1.0",
42
+ "jiti": "^1.18.2",
43
+ "prettier": "^2.8.7",
44
+ "typescript": "^5.0.2",
45
+ "unbuild": "^1.1.2",
46
+ "vitest": "^0.29.8"
47
+ },
48
+ "packageManager": "pnpm@8.0.0"
5
49
  }