@clerc/plugin-completions 0.40.0 → 0.42.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 (3) hide show
  1. package/README.md +25 -0
  2. package/dist/index.js +93 -20
  3. package/package.json +13 -14
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(context) {
9
+ const { cli } = context;
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(context) {
55
+ const { cli } = context;
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,62 @@ 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", (context) => {
113
+ var _a;
114
+ if (!cli._scriptName) {
115
+ throw new Error("CLI name is not defined!");
116
+ }
117
+ const shell = String(
118
+ (_a = context.parameters.shell) != null ? _a : context.flags.shell
119
+ );
120
+ if (!shell) {
121
+ throw new Error("Missing shell name");
122
+ }
123
+ if (shell in completionMap) {
124
+ process.stdout.write(
125
+ completionMap[shell](context)
126
+ );
127
+ } else {
128
+ throw new Error(`No such shell: ${shell}`);
129
+ }
130
+ });
131
+ }
132
+ return cli;
133
+ }
134
+ });
135
+
136
+ export { completionsPlugin };
package/package.json CHANGED
@@ -1,26 +1,26 @@
1
1
  {
2
2
  "name": "@clerc/plugin-completions",
3
- "version": "0.40.0",
3
+ "version": "0.42.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
- "homepage": "https://github.com/so1ve/clerc/tree/main/packages/plugin-completions#readme",
16
+ "homepage": "https://github.com/clercjs/clerc/tree/main/packages/plugin-completions#readme",
17
17
  "repository": {
18
18
  "type": "git",
19
- "url": "git+https://github.com/so1ve/clerc.git",
19
+ "url": "git+https://github.com/clercjs/clerc.git",
20
20
  "directory": "/"
21
21
  },
22
22
  "bugs": {
23
- "url": "https://github.com/so1ve/clerc/issues"
23
+ "url": "https://github.com/clercjs/clerc/issues"
24
24
  },
25
25
  "license": "MIT",
26
26
  "sideEffects": false,
@@ -47,15 +47,14 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
+ "dependencies": {
51
+ "@clerc/utils": "0.42.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
  }