@clerc/plugin-completions 0.40.0 → 0.41.0

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 (4) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +25 -0
  3. package/dist/index.js +91 -20
  4. package/package.json +10 -11
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Ray <https://github.com/so1ve>
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.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Ray <https://github.com/so1ve>
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.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @clerc/plugin-completions
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/@clerc/plugin-completions?color=a1b858&label=)](https://www.npmjs.com/package/@clerc/plugin-completions)
4
+
5
+ Clerc plugin to add shell completions to your cli.
6
+
7
+ ## 📦 Installation
8
+
9
+ ```bash
10
+ $ npm install @clerc/plugin-completions
11
+ $ yarn add @clerc/plugin-completions
12
+ $ pnpm add @clerc/plugin-completions
13
+ ```
14
+
15
+ ## 🚀 Usage
16
+
17
+ ```ts
18
+ import { completionsPlugin } from "@clerc/plugin-completions";
19
+
20
+ cli.use(completionsPlugin());
21
+ ```
22
+
23
+ ## 📝 License
24
+
25
+ [MIT](../../LICENSE). Made with ❤️ by [Ray](https://github.com/so1ve)
package/dist/index.js CHANGED
@@ -1,7 +1,14 @@
1
- import{definePlugin as p}from"@clerc/core";const c=e=>`
2
- ${e})
3
- cmd+="__${e}"
4
- ;;`,$=e=>{const{cli:o}=e,{_scriptName:t,_commands:n}=o;return`_${t}() {
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
+ function getBashCompletion(ctx) {
9
+ const { cli } = ctx;
10
+ const { _scriptName: name, _commands: commands } = cli;
11
+ return `_${name}() {
5
12
  local i cur prev opts cmds
6
13
  COMPREPLY=()
7
14
  cur="\${COMP_WORDS[COMP_CWORD]}"
@@ -13,27 +20,49 @@ import{definePlugin as p}from"@clerc/core";const c=e=>`
13
20
  do
14
21
  case "\${i}" in
15
22
  "$1")
16
- cmd="${t}"
23
+ cmd="${name}"
17
24
  ;;
18
- ${Object.keys(n).map(c).join("")}
25
+ ${Object.keys(commands).map(generateCommandCompletion).join("")}
19
26
  *)
20
27
  ;;
21
28
  esac
22
29
  done
23
30
  }
24
31
 
25
- complete -F _${t} -o bashdefault -o default ${t}
26
- `},i=e=>e.replace(/([A-Z])/g,(o,t)=>`-${t.toLowerCase()}`),r=e=>e.length<=1?`-${e}`:`--${i(e)}`,a="(No Description)",u=e=>`[CompletionResult]::new('${e.name}', '${e.name}', [CompletionResultType]::ParameterValue, '${e.description}')`,d=e=>{var o;return Object.entries((o=e.flags)!=null?o:{}).map(([t,n])=>{const s=[`[CompletionResult]::new('${r(t)}', '${i(t)}', [CompletionResultType]::ParameterName, '${e.flags[t].description||a}')`];return n!=null&&n.alias&&s.push(`[CompletionResult]::new('${r(n.alias)}', '${n.alias}', [CompletionResultType]::ParameterName, '${e.flags[t].description||a}')`),s.join(`
27
- `)}).join(`
28
- `)},C=e=>{const{cli:o}=e,{_scriptName:t,_commands:n}=o;return`using namespace System.Management.Automation
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
+ var _a;
40
+ return Object.entries((_a = command.flags) != null ? _a : {}).map(([flagName, flag]) => {
41
+ const gen = [
42
+ `[CompletionResult]::new('${gracefulFlagName(flagName)}', '${kebabCase(
43
+ flagName
44
+ )}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`
45
+ ];
46
+ if (flag == null ? void 0 : flag.alias) {
47
+ gen.push(
48
+ `[CompletionResult]::new('${gracefulFlagName(flag.alias)}', '${flag.alias}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`
49
+ );
50
+ }
51
+ return gen.join("\n ");
52
+ }).join("\n ");
53
+ };
54
+ function getPwshCompletion(ctx) {
55
+ const { cli } = ctx;
56
+ const { _scriptName: name, _commands: commands } = cli;
57
+ return `using namespace System.Management.Automation
29
58
  using namespace System.Management.Automation.Language
30
59
 
31
- Register-ArgumentCompleter -Native -CommandName '${t}' -ScriptBlock {
60
+ Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
32
61
  param($wordToComplete, $commandAst, $cursorPosition)
33
62
 
34
63
  $commandElements = $commandAst.CommandElements
35
64
  $command = @(
36
- '${t}'
65
+ '${name}'
37
66
  for ($i = 1; $i -lt $commandElements.Count; $i++) {
38
67
  $element = $commandElements[$i]
39
68
  if ($element -isnot [StringConstantExpressionAst] -or
@@ -46,18 +75,60 @@ Register-ArgumentCompleter -Native -CommandName '${t}' -ScriptBlock {
46
75
  }) -join ';'
47
76
 
48
77
  $completions = @(switch ($command) {
49
- '${t}' {
50
- ${Object.entries(n).map(([s,m])=>u(m)).join(`
51
- `)}
78
+ '${name}' {
79
+ ${Object.entries(commands).map(([_, command]) => getCompletionValue(command)).join("\n ")}
52
80
  break
53
81
  }
54
- ${Object.entries(n).map(([s,m])=>`'${t};${s.split(" ").join(";")}' {
55
- ${d(m)}
82
+ ${Object.entries(commands).map(
83
+ ([commandName, command]) => `'${name};${commandName.split(" ").join(";")}' {
84
+ ${getCompletionFlag(command)}
56
85
  break
57
- }`).join(`
58
- `)}
86
+ }`
87
+ ).join("\n ")}
59
88
  })
60
89
 
61
90
  $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
62
91
  Sort-Object -Property ListItemText
63
- }`},l={bash:$,pwsh:C},g=(e={})=>p({setup:o=>{const{command:t=!0}=e;return t&&(o=o.command("completions","Print shell completions to stdout",{flags:{shell:{description:"Shell type",type:String,default:""}},parameters:["[shell]"]}).on("completions",n=>{var s;if(!o._scriptName)throw new Error("CLI name is not defined!");const m=String((s=n.parameters.shell)!=null?s:n.flags.shell);if(!m)throw new Error("Missing shell name");if(m in l)process.stdout.write(l[m](n));else throw new Error(`No such shell: ${m}`)})),o}});export{g as completionsPlugin};
92
+ }`;
93
+ }
94
+
95
+ const completionMap = {
96
+ bash: getBashCompletion,
97
+ pwsh: getPwshCompletion
98
+ };
99
+ const completionsPlugin = (options = {}) => definePlugin({
100
+ setup: (cli) => {
101
+ const { command = true } = options;
102
+ if (command) {
103
+ cli = cli.command("completions", "Print shell completions to stdout", {
104
+ flags: {
105
+ shell: {
106
+ description: "Shell type",
107
+ type: String,
108
+ default: ""
109
+ }
110
+ },
111
+ parameters: ["[shell]"]
112
+ }).on("completions", (ctx) => {
113
+ var _a;
114
+ if (!cli._scriptName) {
115
+ throw new Error("CLI name is not defined!");
116
+ }
117
+ const shell = String((_a = ctx.parameters.shell) != null ? _a : ctx.flags.shell);
118
+ if (!shell) {
119
+ throw new Error("Missing shell name");
120
+ }
121
+ if (shell in completionMap) {
122
+ process.stdout.write(
123
+ completionMap[shell](ctx)
124
+ );
125
+ } else {
126
+ throw new Error(`No such shell: ${shell}`);
127
+ }
128
+ });
129
+ }
130
+ return cli;
131
+ }
132
+ });
133
+
134
+ export { completionsPlugin };
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@clerc/plugin-completions",
3
- "version": "0.40.0",
3
+ "version": "0.41.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
+ "type": "module",
5
6
  "description": "Clerc plugin completions",
6
7
  "keywords": [
7
- "cli",
8
- "clerc",
9
- "clerc-plugin",
8
+ "args",
10
9
  "arguments",
11
10
  "argv",
12
- "args",
11
+ "clerc",
12
+ "clerc-plugin",
13
+ "cli",
13
14
  "terminal"
14
15
  ],
15
- "type": "module",
16
16
  "homepage": "https://github.com/so1ve/clerc/tree/main/packages/plugin-completions#readme",
17
17
  "repository": {
18
18
  "type": "git",
@@ -47,15 +47,14 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
+ "dependencies": {
51
+ "@clerc/utils": "0.41.0"
52
+ },
50
53
  "peerDependencies": {
51
54
  "@clerc/core": "*"
52
55
  },
53
- "devDependencies": {
54
- "@clerc/core": "0.40.0",
55
- "@clerc/utils": "0.40.0"
56
- },
57
56
  "scripts": {
58
- "build": "pkgroll --minify",
57
+ "build": "pkgroll",
59
58
  "watch": "pkgroll --watch"
60
59
  }
61
60
  }