@clerc/plugin-completions 0.25.1 → 0.27.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.
package/dist/index.js CHANGED
@@ -1,14 +1,7 @@
1
- import { definePlugin } from '@clerc/core';
2
- import { gracefulFlagName, kebabCase } from '@clerc/utils';
3
-
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}() {
1
+ import{definePlugin as p}from"@clerc/core";const c=e=>`
2
+ ${e})
3
+ cmd+="__${e}"
4
+ ;;`,$=e=>{const{cli:n}=e,{_name:t,_commands:o}=n;return`_${t}() {
12
5
  local i cur prev opts cmds
13
6
  COMPREPLY=()
14
7
  cur="\${COMP_WORDS[COMP_CWORD]}"
@@ -20,44 +13,27 @@ const getBashCompletion = (ctx) => {
20
13
  do
21
14
  case "\${i}" in
22
15
  "$1")
23
- cmd="${name}"
16
+ cmd="${t}"
24
17
  ;;
25
- ${Object.keys(commands).map(generateCommandCompletion).join("")}
18
+ ${Object.keys(o).map(c).join("")}
26
19
  *)
27
20
  ;;
28
21
  esac
29
22
  done
30
23
  }
31
24
 
32
- complete -F _${name} -o bashdefault -o default ${name}
33
- `;
34
- };
35
-
36
- const NO_DESCRIPTION = "(No Description)";
37
- const getCompletionValue = (command) => `[CompletionResult]::new('${command.name}', '${command.name}', [CompletionResultType]::ParameterValue, '${command.description}')`;
38
- const getCompletionFlag = (command) => {
39
- return Object.entries(command.flags || {}).map(([flagName, flag]) => {
40
- const gen = [`[CompletionResult]::new('${gracefulFlagName(flagName)}', '${kebabCase(flagName)}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`];
41
- if (flag == null ? void 0 : flag.alias) {
42
- gen.push(
43
- `[CompletionResult]::new('${gracefulFlagName(flag.alias)}', '${flag.alias}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`
44
- );
45
- }
46
- return gen.join("\n ");
47
- }).join("\n ");
48
- };
49
- const getPwshCompletion = (ctx) => {
50
- const { cli } = ctx;
51
- const { _name: name, _commands: commands } = cli;
52
- return `using namespace System.Management.Automation
25
+ complete -F _${t} -o bashdefault -o default ${t}
26
+ `},i=e=>e.replace(/([A-Z])/g,(n,t)=>`-${t.toLowerCase()}`),r=e=>e.length<=1?`-${e}`:`--${i(e)}`,l="(No Description)",u=e=>`[CompletionResult]::new('${e.name}', '${e.name}', [CompletionResultType]::ParameterValue, '${e.description}')`,d=e=>Object.entries(e.flags||{}).map(([n,t])=>{const o=[`[CompletionResult]::new('${r(n)}', '${i(n)}', [CompletionResultType]::ParameterName, '${e.flags[n].description||l}')`];return t!=null&&t.alias&&o.push(`[CompletionResult]::new('${r(t.alias)}', '${t.alias}', [CompletionResultType]::ParameterName, '${e.flags[n].description||l}')`),o.join(`
27
+ `)}).join(`
28
+ `),C=e=>{const{cli:n}=e,{_name:t,_commands:o}=n;return`using namespace System.Management.Automation
53
29
  using namespace System.Management.Automation.Language
54
30
 
55
- Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
31
+ Register-ArgumentCompleter -Native -CommandName '${t}' -ScriptBlock {
56
32
  param($wordToComplete, $commandAst, $cursorPosition)
57
33
 
58
34
  $commandElements = $commandAst.CommandElements
59
35
  $command = @(
60
- '${name}'
36
+ '${t}'
61
37
  for ($i = 1; $i -lt $commandElements.Count; $i++) {
62
38
  $element = $commandElements[$i]
63
39
  if ($element -isnot [StringConstantExpressionAst] -or
@@ -70,57 +46,18 @@ Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
70
46
  }) -join ';'
71
47
 
72
48
  $completions = @(switch ($command) {
73
- '${name}' {
74
- ${Object.entries(commands).map(([_, command]) => getCompletionValue(command)).join("\n ")}
49
+ '${t}' {
50
+ ${Object.entries(o).map(([s,m])=>u(m)).join(`
51
+ `)}
75
52
  break
76
53
  }
77
- ${Object.entries(commands).map(([commandName, command]) => `'${name};${commandName.split(" ").join(";")}' {
78
- ${getCompletionFlag(command)}
54
+ ${Object.entries(o).map(([s,m])=>`'${t};${s.split(" ").join(";")}' {
55
+ ${d(m)}
79
56
  break
80
- }`).join("\n ")}
57
+ }`).join(`
58
+ `)}
81
59
  })
82
60
 
83
61
  $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
84
62
  Sort-Object -Property ListItemText
85
- }`;
86
- };
87
-
88
- const completionMap = {
89
- bash: getBashCompletion,
90
- pwsh: getPwshCompletion
91
- };
92
- const completionsPlugin = (options = {}) => definePlugin({
93
- setup: (cli) => {
94
- const { command = true } = options;
95
- if (command) {
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
- parameters: [
105
- "[shell]"
106
- ]
107
- }).on("completions", (ctx) => {
108
- if (!cli._name) {
109
- throw new Error("CLI name is not defined!");
110
- }
111
- const shell = String(ctx.parameters.shell || ctx.flags.shell);
112
- if (!shell) {
113
- throw new Error("Missing shell name");
114
- }
115
- if (shell in completionMap) {
116
- process.stdout.write(completionMap[shell](ctx));
117
- } else {
118
- throw new Error(`No such shell: ${shell}`);
119
- }
120
- });
121
- }
122
- return cli;
123
- }
124
- });
125
-
126
- export { completionsPlugin };
63
+ }`},a={bash:$,pwsh:C},g=(e={})=>p({setup:n=>{const{command:t=!0}=e;return t&&(n=n.command("completions","Print shell completions to stdout",{flags:{shell:{description:"Shell type",type:String,default:""}},parameters:["[shell]"]}).on("completions",o=>{if(!n._name)throw new Error("CLI name is not defined!");const s=String(o.parameters.shell||o.flags.shell);if(!s)throw new Error("Missing shell name");if(s in a)process.stdout.write(a[s](o));else throw new Error(`No such shell: ${s}`)})),n}});export{g as completionsPlugin};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-completions",
3
- "version": "0.25.1",
3
+ "version": "0.27.1",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin completions",
6
6
  "keywords": [
@@ -27,11 +27,11 @@
27
27
  "exports": {
28
28
  ".": {
29
29
  "types": "./dist/index.d.ts",
30
- "import": "./dist/index.mjs"
30
+ "import": "./dist/index.js"
31
31
  }
32
32
  },
33
33
  "main": "dist/index.js",
34
- "module": "dist/index.mjs",
34
+ "module": "dist/index.js",
35
35
  "types": "dist/index.d.ts",
36
36
  "typesVersions": {
37
37
  "*": {
@@ -50,14 +50,12 @@
50
50
  "peerDependencies": {
51
51
  "@clerc/core": "*"
52
52
  },
53
- "dependencies": {
54
- "@clerc/utils": "0.25.1"
55
- },
56
53
  "devDependencies": {
57
- "@clerc/core": "0.25.1"
54
+ "@clerc/core": "0.27.1",
55
+ "@clerc/utils": "0.27.1"
58
56
  },
59
57
  "scripts": {
60
- "build": "puild",
58
+ "build": "puild --minify",
61
59
  "watch": "puild --watch"
62
60
  }
63
61
  }
package/dist/index.mjs DELETED
@@ -1,126 +0,0 @@
1
- import { definePlugin } from '@clerc/core';
2
- import { gracefulFlagName, kebabCase } from '@clerc/utils';
3
-
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
- };
35
-
36
- const NO_DESCRIPTION = "(No Description)";
37
- const getCompletionValue = (command) => `[CompletionResult]::new('${command.name}', '${command.name}', [CompletionResultType]::ParameterValue, '${command.description}')`;
38
- const getCompletionFlag = (command) => {
39
- return Object.entries(command.flags || {}).map(([flagName, flag]) => {
40
- const gen = [`[CompletionResult]::new('${gracefulFlagName(flagName)}', '${kebabCase(flagName)}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`];
41
- if (flag == null ? void 0 : flag.alias) {
42
- gen.push(
43
- `[CompletionResult]::new('${gracefulFlagName(flag.alias)}', '${flag.alias}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`
44
- );
45
- }
46
- return gen.join("\n ");
47
- }).join("\n ");
48
- };
49
- const getPwshCompletion = (ctx) => {
50
- const { cli } = ctx;
51
- const { _name: name, _commands: commands } = cli;
52
- return `using namespace System.Management.Automation
53
- using namespace System.Management.Automation.Language
54
-
55
- Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
56
- param($wordToComplete, $commandAst, $cursorPosition)
57
-
58
- $commandElements = $commandAst.CommandElements
59
- $command = @(
60
- '${name}'
61
- for ($i = 1; $i -lt $commandElements.Count; $i++) {
62
- $element = $commandElements[$i]
63
- if ($element -isnot [StringConstantExpressionAst] -or
64
- $element.StringConstantType -ne [StringConstantType]::BareWord -or
65
- $element.Value.StartsWith('-') -or
66
- $element.Value -eq $wordToComplete) {
67
- break
68
- }
69
- $element.Value
70
- }) -join ';'
71
-
72
- $completions = @(switch ($command) {
73
- '${name}' {
74
- ${Object.entries(commands).map(([_, command]) => getCompletionValue(command)).join("\n ")}
75
- break
76
- }
77
- ${Object.entries(commands).map(([commandName, command]) => `'${name};${commandName.split(" ").join(";")}' {
78
- ${getCompletionFlag(command)}
79
- break
80
- }`).join("\n ")}
81
- })
82
-
83
- $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
84
- Sort-Object -Property ListItemText
85
- }`;
86
- };
87
-
88
- const completionMap = {
89
- bash: getBashCompletion,
90
- pwsh: getPwshCompletion
91
- };
92
- const completionsPlugin = (options = {}) => definePlugin({
93
- setup: (cli) => {
94
- const { command = true } = options;
95
- if (command) {
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
- parameters: [
105
- "[shell]"
106
- ]
107
- }).on("completions", (ctx) => {
108
- if (!cli._name) {
109
- throw new Error("CLI name is not defined!");
110
- }
111
- const shell = String(ctx.parameters.shell || ctx.flags.shell);
112
- if (!shell) {
113
- throw new Error("Missing shell name");
114
- }
115
- if (shell in completionMap) {
116
- process.stdout.write(completionMap[shell](ctx));
117
- } else {
118
- throw new Error(`No such shell: ${shell}`);
119
- }
120
- });
121
- }
122
- return cli;
123
- }
124
- });
125
-
126
- export { completionsPlugin };