@kaynooo/cli-dev 0.1.0 → 0.2.1

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 (2) hide show
  1. package/dist/cli.js +69 -40
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -1,15 +1,14 @@
1
- #!/usr/bin/env bun
1
+ #!/usr/bin/env -S bun --no-env-file
2
2
  // @bun
3
3
 
4
4
  // src/cli.ts
5
- import process7 from "process";
6
-
5
+ import process8 from "process";
7
6
  // ../kit/src/args.ts
8
- import process3 from "process";
7
+ import process4 from "process";
9
8
  import { parseArgs } from "util";
10
9
 
11
10
  // ../kit/src/style.ts
12
- import process from "process";
11
+ import process2 from "process";
13
12
 
14
13
  // ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
15
14
  function x(s) {
@@ -273,10 +272,10 @@ function le(i = {}) {
273
272
  var t = le();
274
273
 
275
274
  // ../kit/src/style.ts
276
- function takesControlCodes(stream, env = process.env) {
275
+ function takesControlCodes(stream, env = process2.env) {
277
276
  return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb";
278
277
  }
279
- function paint(stream, env = process.env) {
278
+ function paint(stream, env = process2.env) {
280
279
  return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
281
280
  }
282
281
  var STATUSES = {
@@ -299,24 +298,30 @@ function printJson(value) {
299
298
  }
300
299
 
301
300
  // ../kit/src/help.ts
302
- import process2 from "process";
301
+ import process3 from "process";
303
302
  var OPTIONAL = /\[[^\]]+\]/g;
304
303
  var USAGE_PREFIX = /^usage:\s*/i;
305
304
  var FULL_STOP = /\.$/;
306
- function usageLine(usage, colors = paint(process2.stdout)) {
305
+ function usageLine(usage, colors = paint(process3.stdout)) {
307
306
  return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
308
307
  }
309
- function helpText(usage, options, colors = paint(process2.stdout), { examples = [], replaces } = {}) {
310
- return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
308
+ function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces, exits = [] } = {}) {
309
+ return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
311
310
  `);
312
311
  }
313
- function replacesRows(replaces, colors = paint(process2.stdout)) {
312
+ function exitRows(exits, colors = paint(process3.stdout)) {
313
+ if (exits.length === 0)
314
+ return [];
315
+ const width = Math.max(...exits.map(([code]) => String(code).length));
316
+ return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
317
+ }
318
+ function replacesRows(replaces, colors = paint(process3.stdout)) {
314
319
  return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
315
320
  }
316
- function wantsHelpJson(env = process2.env) {
321
+ function wantsHelpJson(env = process3.env) {
317
322
  return env.KIT_HELP === "json";
318
323
  }
319
- function description(usage, { examples = [], replaces } = {}) {
324
+ function description(usage, { examples = [], replaces, exits } = {}, options) {
320
325
  const lines = usage.replace(USAGE_PREFIX, "").split(`
321
326
  `);
322
327
  const first = lines[0];
@@ -327,15 +332,17 @@ function description(usage, { examples = [], replaces } = {}) {
327
332
  synopsis: colon === -1 ? first : first.slice(0, colon),
328
333
  summary: colon === -1 ? fallback : first.slice(colon + 2),
329
334
  ...replaces === undefined ? {} : { replaces },
330
- examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
335
+ examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] }),
336
+ ...exits === undefined ? {} : { exits: exits.map(([code, meaning]) => ({ code, meaning })) },
337
+ ...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
331
338
  };
332
339
  }
333
- function exampleRows(examples, colors = paint(process2.stdout)) {
340
+ function exampleRows(examples, colors = paint(process3.stdout)) {
334
341
  if (examples.length === 0)
335
342
  return [];
336
343
  return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
337
344
  }
338
- function optionRows(options, colors = paint(process2.stdout)) {
345
+ function optionRows(options, colors = paint(process3.stdout)) {
339
346
  const rows = Object.entries(options).map(([name, option]) => {
340
347
  const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
341
348
  const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
@@ -431,7 +438,7 @@ var OUTPUT_OPTIONS = {
431
438
  color: { type: "string", description: "auto (on a terminal alone), always or never" },
432
439
  plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
433
440
  };
434
- function applyOutput(color, plain, env = process3.env) {
441
+ function applyOutput(color, plain, env = process4.env) {
435
442
  const when = plain === true ? "never" : color ?? "auto";
436
443
  if (when === "auto")
437
444
  return;
@@ -454,7 +461,7 @@ function parse(args, usage, options, settings = {}) {
454
461
  const { own, rest } = settings.trailing === true ? splitTrailing(args, all, settings.positionals?.length ?? 0) : splitRest(args, settings.rest === true);
455
462
  let parsed;
456
463
  try {
457
- parsed = parseArgs({ args: joinDashValues(withBare(own, all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
464
+ parsed = parseArgs({ args: joinDashValues(withBare(withAliases(own, all), all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
458
465
  } catch (error) {
459
466
  const message = parseError(error);
460
467
  throw new UsageError(message, nearestOption(message, all, settings.allowNegative === true));
@@ -462,7 +469,7 @@ function parse(args, usage, options, settings = {}) {
462
469
  const values = parsed.values;
463
470
  applyOutput("color" in output ? values.color : undefined, "plain" in output ? values.plain : undefined);
464
471
  if (values.help === true) {
465
- console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings)) : helpText(usage, listing, undefined, settings));
472
+ console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings, listing)) : helpText(usage, listing, undefined, settings));
466
473
  throw new HelpShown;
467
474
  }
468
475
  checkChoices(parsed.values, all);
@@ -504,6 +511,18 @@ function listed(options, allowNegative) {
504
511
  return [...option.description === undefined ? [] : [[name, option]], [`no-${name}`, { type: "boolean", description: option.negative }]];
505
512
  }));
506
513
  }
514
+ function withAliases(args, options) {
515
+ const named = new Map(Object.entries(options).flatMap(([name, { aliases = [] }]) => aliases.map((alias) => [`--${alias}`, `--${name}`])));
516
+ if (named.size === 0)
517
+ return args;
518
+ const separator = args.indexOf("--");
519
+ const own = separator === -1 ? args : args.slice(0, separator);
520
+ return [...own.map((arg) => {
521
+ const at = arg.indexOf("=");
522
+ const current = named.get(at === -1 ? arg : arg.slice(0, at));
523
+ return current === undefined ? arg : `${current}${at === -1 ? "" : arg.slice(at)}`;
524
+ }), ...separator === -1 ? [] : args.slice(separator)];
525
+ }
507
526
  function withBare(args, options) {
508
527
  const bare = new Map(Object.entries(options).flatMap(([name, { short, optional }]) => optional === undefined ? [] : [[`--${name}`, optional], ...short === undefined ? [] : [[`-${short}`, optional]]]));
509
528
  if (bare.size === 0)
@@ -594,13 +613,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
594
613
  var RESET_STYLE = `${ESC}[0m`;
595
614
  var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
596
615
  // ../kit/src/cli.ts
597
- import process5 from "process";
616
+ import { readFileSync, realpathSync } from "fs";
617
+ import { dirname as dirname2, join as join2 } from "path";
618
+ import process6 from "process";
598
619
 
599
620
  // ../kit/src/usage.ts
600
621
  import { appendFileSync, mkdirSync } from "fs";
601
622
  import { homedir } from "os";
602
623
  import { dirname, isAbsolute, join } from "path";
603
- import process4 from "process";
624
+ import process5 from "process";
604
625
 
605
626
  // ../kit/src/logs.ts
606
627
  var DAY_MS = 24 * 60 * 60 * 1000;
@@ -614,14 +635,14 @@ var usageError;
614
635
  function noteUsageError(message) {
615
636
  usageError = message;
616
637
  }
617
- function stateHome(env = process4.env) {
638
+ function stateHome(env = process5.env) {
618
639
  const dir = env.XDG_STATE_HOME;
619
640
  return dir !== undefined && isAbsolute(dir) ? dir : join(homedir(), ".local", "state");
620
641
  }
621
- function usagePath(env = process4.env) {
642
+ function usagePath(env = process5.env) {
622
643
  return join(stateHome(env), CLI_FOLDER, "usage.jsonl");
623
644
  }
624
- function usageRecorded(env = process4.env) {
645
+ function usageRecorded(env = process5.env) {
625
646
  const off = env.KAYNOOO_CLI_NO_USAGE;
626
647
  return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
627
648
  }
@@ -635,7 +656,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
635
656
  usageError = undefined;
636
657
  return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
637
658
  }
638
- function recordUsage(entry, env = process4.env) {
659
+ function recordUsage(entry, env = process5.env) {
639
660
  if (!usageRecorded(env))
640
661
  return;
641
662
  try {
@@ -726,8 +747,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
726
747
  `);
727
748
  }
728
749
  function printError(prefix, message, hint, cause) {
729
- const colors = paint(process5.stderr);
730
- const styled = takesControlCodes(process5.stderr);
750
+ const colors = paint(process6.stderr);
751
+ const styled = takesControlCodes(process6.stderr);
731
752
  const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
732
753
  const under = (label, text, tint) => text === undefined ? "" : `
733
754
  ${styled ? tint(label) : `${label}:`} ${text}`;
@@ -770,11 +791,11 @@ async function dispatch(level, typed, { version, redact }) {
770
791
  }
771
792
  }
772
793
  if (name === undefined && level.path.length > 1) {
773
- console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process5.stderr));
794
+ console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process6.stderr));
774
795
  return 2;
775
796
  }
776
797
  if (name === undefined || name === "help" || name === "--help" || name === "-h") {
777
- console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process5.stdout));
798
+ console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process6.stdout));
778
799
  return 0;
779
800
  }
780
801
  if (level.path.length === 1 && (name === "--version" || name === "-v")) {
@@ -890,10 +911,22 @@ async function printCandidates(found) {
890
911
  console.log(line);
891
912
  return 0;
892
913
  }
893
- async function runCli(cli, argv = process5.argv.slice(2)) {
914
+ function packageVersion(entry = Bun.main) {
915
+ for (let dir = dirname2(realpathSync(entry));; dir = dirname2(dir)) {
916
+ try {
917
+ const { version } = JSON.parse(readFileSync(join2(dir, "package.json"), "utf8"));
918
+ if (typeof version === "string")
919
+ return version;
920
+ } catch {}
921
+ if (dirname2(dir) === dir)
922
+ return "0.0.0";
923
+ }
924
+ }
925
+ async function runCli(cli, argv = process6.argv.slice(2)) {
926
+ const version = cli.version ?? packageVersion();
894
927
  if (argv[0] === COMPLETE)
895
928
  return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
896
- const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(cli.version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version: cli.version, redact: cli.redact });
929
+ const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version, redact: cli.redact });
897
930
  if (cli.usage !== false) {
898
931
  try {
899
932
  recordUsage(usageEntry(cli.name, commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv), argv, code));
@@ -922,9 +955,6 @@ var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "ses
922
955
  var {Glob } = globalThis.Bun;
923
956
  var SESSION_FILES = new Glob("*.json");
924
957
  var ACCOUNT_DIRS = new Glob("*/sessions");
925
- // package.json
926
- var version = "0.1.0";
927
-
928
958
  // src/encode.ts
929
959
  function b64Encode(bytes, url = false) {
930
960
  return Buffer.from(bytes).toString(url ? "base64url" : "base64");
@@ -1174,7 +1204,7 @@ async function copyToClipboard(text) {
1174
1204
  return await copy.exited === 0;
1175
1205
  }
1176
1206
  function printRows(rows) {
1177
- const { cyan } = paint(process7.stdout);
1207
+ const { cyan } = paint(process8.stdout);
1178
1208
  const width = Math.max(...rows.map(([label]) => label.length));
1179
1209
  for (const [label, value] of rows)
1180
1210
  console.log(`${cyan(label.padEnd(width))} ${value}`);
@@ -1209,7 +1239,7 @@ async function password(args) {
1209
1239
  return 0;
1210
1240
  }
1211
1241
  if (copied)
1212
- console.error(paint(process7.stderr).dim("copied to clipboard"));
1242
+ console.error(paint(process8.stderr).dim("copied to clipboard"));
1213
1243
  if (hash !== undefined && given === undefined)
1214
1244
  console.error(secret);
1215
1245
  console.log(hash ?? secret);
@@ -1237,7 +1267,7 @@ async function jwt(args) {
1237
1267
  printJson({ header: decoded.header, payload: decoded.payload, claims, signatureBytes: decoded.signatureBytes });
1238
1268
  return 0;
1239
1269
  }
1240
- const { bold } = paint(process7.stdout);
1270
+ const { bold } = paint(process8.stdout);
1241
1271
  console.log(`${bold("header")}
1242
1272
  ${JSON.stringify(decoded.header, null, 2)}
1243
1273
 
@@ -1391,9 +1421,8 @@ async function hash(args) {
1391
1421
  return 0;
1392
1422
  }
1393
1423
  if (import.meta.main) {
1394
- process7.exitCode = await runCli({
1424
+ process8.exitCode = await runCli({
1395
1425
  name: "dev",
1396
- version,
1397
1426
  about: "small developer utilities: passwords and bcrypt hashes, JWTs, timestamps, UUIDs, base64, URLs and file hashes",
1398
1427
  replaces: "openssl rand, htpasswd, jwt.io, date -d, uuidgen, base64, sha256sum and one-off scripts for each",
1399
1428
  examples: ["dev password", ["dev jwt <token>", "header, payload and expiry"], ["dev ts 1700000000", "an epoch as dates"]],
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-dev",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.2.1",
5
5
  "description": "dev: small developer utilities in one bin: random passwords and bcrypt hashes, JWT decoding, timestamps, UUIDs and ULIDs, base64, URL parts and file hashes.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -36,6 +36,6 @@
36
36
  "dev": "$npm_execpath src/cli.ts"
37
37
  },
38
38
  "devDependencies": {
39
- "@kaynooo/cli-kit": "0.19.0"
39
+ "@kaynooo/cli-kit": "0.21.0"
40
40
  }
41
41
  }