@kaynooo/cli-dev 0.6.0 → 0.6.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.
Files changed (2) hide show
  1. package/dist/cli.js +81 -2
  2. package/package.json +5 -5
package/dist/cli.js CHANGED
@@ -309,7 +309,7 @@ class Completing {
309
309
  }
310
310
  var completion = { active: false };
311
311
  var OUTPUT_OPTIONS = {
312
- color: { type: "string", description: "auto (on a terminal alone), always or never" },
312
+ color: { type: "string", choices: ["auto", "always", "never"], description: "auto (the default) colours on a terminal alone" },
313
313
  plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
314
314
  };
315
315
  function applyOutput(color, plain, env = process6.env) {
@@ -534,6 +534,66 @@ import { readFileSync as readFileSync2, realpathSync as realpathSync2 } from "fs
534
534
  import { dirname as dirname2, join as join2 } from "path";
535
535
  import process10 from "process";
536
536
 
537
+ // ../kit/src/completions.ts
538
+ var COMPLETION_SHELLS = ["fish", "bash", "zsh"];
539
+ var FISH_FUNCTION = `functions -q __kaynooo_cli_complete; or function __kaynooo_cli_complete --argument-names bin --description 'what a @kaynooo/cli-kit bin offers for the word being typed'
540
+ set -l tokens (commandline -xpc 2>/dev/null; or commandline -opc)
541
+ set -l current (commandline -ct)
542
+ for line in ($bin __complete $tokens[2..-1] $current 2>/dev/null)
543
+ if test "$line" = __files__
544
+ __fish_complete_path $current
545
+ else
546
+ echo $line
547
+ end
548
+ end
549
+ end`;
550
+ var BASH_FUNCTION = `__kaynooo_cli_complete() {
551
+ local IFS=$'\\n' cur=\${COMP_WORDS[COMP_CWORD]} line words=
552
+ local -a lines
553
+ COMPREPLY=()
554
+ lines=($("\${COMP_WORDS[0]}" __complete "\${COMP_WORDS[@]:1:COMP_CWORD}" 2>/dev/null))
555
+ for line in "\${lines[@]}"; do
556
+ if [[ $line == __files__ ]]; then
557
+ COMPREPLY+=($(compgen -f -- "$cur"))
558
+ else
559
+ words+="\${line%%$'\\t'*}"$'\\n'
560
+ fi
561
+ done
562
+ COMPREPLY+=($(compgen -W "$words" -- "$cur"))
563
+ }`;
564
+ var ZSH_FUNCTION = `__kaynooo_cli_complete() {
565
+ local line
566
+ local -a described plain
567
+ for line in \${(f)"$("\${words[1]}" __complete "\${(@)words[2,CURRENT]}" 2>/dev/null)"}; do
568
+ if [[ $line == __files__ ]]; then
569
+ _files
570
+ elif [[ $line == *$'\\t'* ]]; then
571
+ described+=("\${\${line%%$'\\t'*}//:/\\\\:}:\${line#*$'\\t'}")
572
+ else
573
+ plain+=("$line")
574
+ fi
575
+ done
576
+ (( \${#described} )) && _describe -V value described
577
+ (( \${#plain} )) && compadd -- "\${plain[@]}"
578
+ return 0
579
+ }`;
580
+ function completionScript(shell, bins, header = `# Generated by \`${bins[0]} --completions ${shell}\``) {
581
+ if (shell === "fish")
582
+ return `${header}
583
+ ${FISH_FUNCTION}
584
+ ${bins.map((bin) => `complete -c ${bin} -f -a '(__kaynooo_cli_complete ${bin})'
585
+ `).join("")}`;
586
+ if (shell === "bash")
587
+ return `${header}
588
+ ${BASH_FUNCTION}
589
+ complete -F __kaynooo_cli_complete ${bins.join(" ")}
590
+ `;
591
+ return `${header}
592
+ ${ZSH_FUNCTION}
593
+ compdef __kaynooo_cli_complete ${bins.join(" ")}
594
+ `;
595
+ }
596
+
537
597
  // ../kit/src/notify.ts
538
598
  import process8 from "process";
539
599
  var NOTIFY_AFTER_MS = 1e4;
@@ -607,7 +667,11 @@ var LEVEL_OPTIONS = {
607
667
  ...OUTPUT_OPTIONS,
608
668
  help: { type: "boolean", short: "h", description: "show this help" }
609
669
  };
610
- var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
670
+ var TOP_OPTIONS = {
671
+ ...LEVEL_OPTIONS,
672
+ version: { type: "boolean", short: "v", description: "print the version" },
673
+ completions: { type: "string", choices: COMPLETION_SHELLS, description: "print the shell completion script" }
674
+ };
611
675
  async function commandRows(all, colors) {
612
676
  const commands = shown(all);
613
677
  const width = Math.max(0, ...Object.keys(commands).map((name) => name.length));
@@ -840,6 +904,19 @@ async function candidates(scope, defaultCommand, words, before = []) {
840
904
  }
841
905
  return "commands" in command ? candidates(scopeOf(command.commands, command.extraCommands, command.aliases), undefined, rest, [...before, word]) : leafCandidates(command.run, rest);
842
906
  }
907
+ var COMPLETIONS = "--completions";
908
+ function flagValue([flag = "", ...rest]) {
909
+ return flag.includes("=") ? [flag.slice(flag.indexOf("=") + 1), ...rest] : rest;
910
+ }
911
+ function printCompletionScript(name, args) {
912
+ const shell = COMPLETION_SHELLS.find((each) => each === args[0]);
913
+ if (shell === undefined || args.length !== 1) {
914
+ printError(name, `${COMPLETIONS} takes one of ${COMPLETION_SHELLS.join(", ")}`, `${name} ${COMPLETIONS} fish`);
915
+ return 2;
916
+ }
917
+ console.log(completionScript(shell, [name]).trimEnd());
918
+ return 0;
919
+ }
843
920
  async function printCandidates(found) {
844
921
  for (const line of await found)
845
922
  console.log(line);
@@ -864,6 +941,8 @@ async function runCli(cli, argv = scriptArgs()) {
864
941
  const version = cli.version ?? packageVersion();
865
942
  if (argv[0] === COMPLETE)
866
943
  return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
944
+ if (argv[0] === COMPLETIONS || argv[0]?.startsWith(`${COMPLETIONS}=`) === true)
945
+ return printCompletionScript(cli.name, flagValue(argv));
867
946
  const started = performance.now();
868
947
  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 });
869
948
  const command = commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv);
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-dev",
3
3
  "type": "module",
4
- "version": "0.6.0",
4
+ "version": "0.6.2",
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",
8
- "homepage": "https://git.kaynooo.fr/kaynooo/cli/src/branch/main/packages/dev#readme",
8
+ "homepage": "https://github.com/Kayno0o/cli/tree/main/packages/dev#readme",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://git.kaynooo.fr/kaynooo/cli.git",
11
+ "url": "git+https://github.com/Kayno0o/cli.git",
12
12
  "directory": "packages/dev"
13
13
  },
14
- "bugs": "https://git.kaynooo.fr/kaynooo/cli/issues",
14
+ "bugs": "https://github.com/Kayno0o/cli/issues",
15
15
  "keywords": [
16
16
  "cli",
17
17
  "password",
@@ -36,6 +36,6 @@
36
36
  "dev": "$npm_execpath src/cli.ts"
37
37
  },
38
38
  "devDependencies": {
39
- "@kaynooo/cli-kit": "0.40.0"
39
+ "@kaynooo/cli-kit": "0.41.0"
40
40
  }
41
41
  }