@clerc/plugin-completions 0.3.3 → 0.3.4

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/dist/index.cjs CHANGED
@@ -3,24 +3,54 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var clerc = require('clerc');
6
+ var utils = require('@clerc/utils');
6
7
 
7
- const gracefulFlag = (n) => n.length === 1 ? `-${n}` : `--${n}`;
8
+ const generateCommandCompletion = (name) => `
9
+ ${name})
10
+ cmd+="__${name}"
11
+ ;;`;
12
+ const getBashCompletion = (ctx) => {
13
+ const { cli } = ctx;
14
+ const { _name: name, _commands: commands } = cli;
15
+ return `_${name}() {
16
+ local i cur prev opts cmds
17
+ COMPREPLY=()
18
+ cur="\${COMP_WORDS[COMP_CWORD]}"
19
+ prev="\${COMP_WORDS[COMP_CWORD-1]}"
20
+ cmd=""
21
+ opts=""
22
+
23
+ for i in \${COMP_WORDS[@]}
24
+ do
25
+ case "\${i}" in
26
+ "$1")
27
+ cmd="${name}"
28
+ ;;
29
+ ${Object.keys(commands).map(generateCommandCompletion).join("")}
30
+ *)
31
+ ;;
32
+ esac
33
+ done
34
+ }
35
+
36
+ complete -F _${name} -o bashdefault -o default ${name}
37
+ `;
38
+ };
8
39
 
9
40
  const getCompletionValue = (command) => `[CompletionResult]::new('${command.name}', '${command.name}', [CompletionResultType]::ParameterValue, '${command.description}')`;
10
41
  const getCompletionFlag = (command) => {
11
42
  return Object.entries(command.flags || {}).map(([flagName, flag]) => {
12
- let gen = [`[CompletionResult]::new('${gracefulFlag(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`];
43
+ const gen = [`[CompletionResult]::new('${utils.gracefulFlagName(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`];
13
44
  if (flag == null ? void 0 : flag.alias) {
14
- const arrayAlias = clerc.mustArray(flag.alias);
15
- gen = [
16
- ...gen,
17
- ...arrayAlias.map((n) => `[CompletionResult]::new('${gracefulFlag(n)}', '${n}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`)
18
- ];
45
+ const arrayAlias = utils.mustArray(flag.alias);
46
+ gen.push(
47
+ ...arrayAlias.map((n) => `[CompletionResult]::new('${utils.gracefulFlagName(n)}', '${n}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`)
48
+ );
19
49
  }
20
50
  return gen.join("\n ");
21
51
  }).join("\n ");
22
52
  };
23
- function getPwshCompletion(ctx) {
53
+ const getPwshCompletion = (ctx) => {
24
54
  const { cli } = ctx;
25
55
  const { _name: name, _commands: commands } = cli;
26
56
  return `using namespace System.Management.Automation
@@ -57,20 +87,29 @@ Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
57
87
  $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
58
88
  Sort-Object -Property ListItemText
59
89
  }`;
60
- }
90
+ };
61
91
 
62
92
  const completionMap = {
93
+ bash: getBashCompletion,
63
94
  pwsh: getPwshCompletion
64
95
  };
65
96
  const completionsPlugin = (options = {}) => clerc.definePlugin({
66
97
  setup(cli) {
67
98
  const { command = true } = options;
68
99
  if (command) {
69
- cli = cli.command("completions", "Print shell completions to stdout").on("completions", (ctx) => {
100
+ cli = cli.command("completions", "Print shell completions to stdout", {
101
+ flags: {
102
+ shell: {
103
+ description: "Shell type",
104
+ type: String,
105
+ default: ""
106
+ }
107
+ }
108
+ }).on("completions", (ctx) => {
70
109
  if (!cli._name) {
71
110
  throw new Error("CLI name is not defined!");
72
111
  }
73
- const shell = String(ctx.parameters[0]);
112
+ const shell = String(ctx.parameters[0] || ctx.flags.shell);
74
113
  if (!shell) {
75
114
  throw new Error("Missing shell name");
76
115
  }
package/dist/index.mjs CHANGED
@@ -1,22 +1,52 @@
1
- import { mustArray, definePlugin, NoSuchCommandError } from 'clerc';
1
+ import { definePlugin, NoSuchCommandError } from 'clerc';
2
+ import { gracefulFlagName, mustArray } from '@clerc/utils';
2
3
 
3
- const gracefulFlag = (n) => n.length === 1 ? `-${n}` : `--${n}`;
4
+ const generateCommandCompletion = (name) => `
5
+ ${name})
6
+ cmd+="__${name}"
7
+ ;;`;
8
+ const getBashCompletion = (ctx) => {
9
+ const { cli } = ctx;
10
+ const { _name: name, _commands: commands } = cli;
11
+ return `_${name}() {
12
+ local i cur prev opts cmds
13
+ COMPREPLY=()
14
+ cur="\${COMP_WORDS[COMP_CWORD]}"
15
+ prev="\${COMP_WORDS[COMP_CWORD-1]}"
16
+ cmd=""
17
+ opts=""
18
+
19
+ for i in \${COMP_WORDS[@]}
20
+ do
21
+ case "\${i}" in
22
+ "$1")
23
+ cmd="${name}"
24
+ ;;
25
+ ${Object.keys(commands).map(generateCommandCompletion).join("")}
26
+ *)
27
+ ;;
28
+ esac
29
+ done
30
+ }
31
+
32
+ complete -F _${name} -o bashdefault -o default ${name}
33
+ `;
34
+ };
4
35
 
5
36
  const getCompletionValue = (command) => `[CompletionResult]::new('${command.name}', '${command.name}', [CompletionResultType]::ParameterValue, '${command.description}')`;
6
37
  const getCompletionFlag = (command) => {
7
38
  return Object.entries(command.flags || {}).map(([flagName, flag]) => {
8
- let gen = [`[CompletionResult]::new('${gracefulFlag(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`];
39
+ const gen = [`[CompletionResult]::new('${gracefulFlagName(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`];
9
40
  if (flag == null ? void 0 : flag.alias) {
10
41
  const arrayAlias = mustArray(flag.alias);
11
- gen = [
12
- ...gen,
13
- ...arrayAlias.map((n) => `[CompletionResult]::new('${gracefulFlag(n)}', '${n}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`)
14
- ];
42
+ gen.push(
43
+ ...arrayAlias.map((n) => `[CompletionResult]::new('${gracefulFlagName(n)}', '${n}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`)
44
+ );
15
45
  }
16
46
  return gen.join("\n ");
17
47
  }).join("\n ");
18
48
  };
19
- function getPwshCompletion(ctx) {
49
+ const getPwshCompletion = (ctx) => {
20
50
  const { cli } = ctx;
21
51
  const { _name: name, _commands: commands } = cli;
22
52
  return `using namespace System.Management.Automation
@@ -53,20 +83,29 @@ Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
53
83
  $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
54
84
  Sort-Object -Property ListItemText
55
85
  }`;
56
- }
86
+ };
57
87
 
58
88
  const completionMap = {
89
+ bash: getBashCompletion,
59
90
  pwsh: getPwshCompletion
60
91
  };
61
92
  const completionsPlugin = (options = {}) => definePlugin({
62
93
  setup(cli) {
63
94
  const { command = true } = options;
64
95
  if (command) {
65
- cli = cli.command("completions", "Print shell completions to stdout").on("completions", (ctx) => {
96
+ cli = cli.command("completions", "Print shell completions to stdout", {
97
+ flags: {
98
+ shell: {
99
+ description: "Shell type",
100
+ type: String,
101
+ default: ""
102
+ }
103
+ }
104
+ }).on("completions", (ctx) => {
66
105
  if (!cli._name) {
67
106
  throw new Error("CLI name is not defined!");
68
107
  }
69
- const shell = String(ctx.parameters[0]);
108
+ const shell = String(ctx.parameters[0] || ctx.flags.shell);
70
109
  if (!shell) {
71
110
  throw new Error("Missing shell name");
72
111
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-completions",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin completions",
6
6
  "keywords": [
@@ -43,7 +43,8 @@
43
43
  "clerc": "*"
44
44
  },
45
45
  "dependencies": {
46
- "clerc": "0.3.3"
46
+ "@clerc/utils": "0.3.4",
47
+ "clerc": "0.3.4"
47
48
  },
48
49
  "scripts": {
49
50
  "build": "puild",