@clerc/plugin-completions 0.3.3 → 0.4.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-completions",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
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.4.0",
47
+ "clerc": "0.4.0"
47
48
  },
48
49
  "scripts": {
49
50
  "build": "puild",
package/dist/index.cjs DELETED
@@ -1,88 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var clerc = require('clerc');
6
-
7
- const gracefulFlag = (n) => n.length === 1 ? `-${n}` : `--${n}`;
8
-
9
- const getCompletionValue = (command) => `[CompletionResult]::new('${command.name}', '${command.name}', [CompletionResultType]::ParameterValue, '${command.description}')`;
10
- const getCompletionFlag = (command) => {
11
- return Object.entries(command.flags || {}).map(([flagName, flag]) => {
12
- let gen = [`[CompletionResult]::new('${gracefulFlag(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`];
13
- 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
- ];
19
- }
20
- return gen.join("\n ");
21
- }).join("\n ");
22
- };
23
- function getPwshCompletion(ctx) {
24
- const { cli } = ctx;
25
- const { _name: name, _commands: commands } = cli;
26
- return `using namespace System.Management.Automation
27
- using namespace System.Management.Automation.Language
28
-
29
- Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
30
- param($wordToComplete, $commandAst, $cursorPosition)
31
-
32
- $commandElements = $commandAst.CommandElements
33
- $command = @(
34
- '${name}'
35
- for ($i = 1; $i -lt $commandElements.Count; $i++) {
36
- $element = $commandElements[$i]
37
- if ($element -isnot [StringConstantExpressionAst] -or
38
- $element.StringConstantType -ne [StringConstantType]::BareWord -or
39
- $element.Value.StartsWith('-') -or
40
- $element.Value -eq $wordToComplete) {
41
- break
42
- }
43
- $element.Value
44
- }) -join ';'
45
-
46
- $completions = @(switch ($command) {
47
- '${name}' {
48
- ${Object.entries(commands).map(([_, command]) => getCompletionValue(command)).join("\n ")}
49
- break
50
- }
51
- ${Object.entries(commands).map(([commandName, command]) => `'${name};${commandName}' {
52
- ${getCompletionFlag(command)}
53
- break
54
- }`).join("\n ")}
55
- })
56
-
57
- $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
58
- Sort-Object -Property ListItemText
59
- }`;
60
- }
61
-
62
- const completionMap = {
63
- pwsh: getPwshCompletion
64
- };
65
- const completionsPlugin = (options = {}) => clerc.definePlugin({
66
- setup(cli) {
67
- const { command = true } = options;
68
- if (command) {
69
- cli = cli.command("completions", "Print shell completions to stdout").on("completions", (ctx) => {
70
- if (!cli._name) {
71
- throw new Error("CLI name is not defined!");
72
- }
73
- const shell = String(ctx.parameters[0]);
74
- if (!shell) {
75
- throw new Error("Missing shell name");
76
- }
77
- if (shell in completionMap) {
78
- console.log(completionMap[shell](ctx));
79
- } else {
80
- throw new clerc.NoSuchCommandError(`No such shell: ${shell}`);
81
- }
82
- });
83
- }
84
- return cli;
85
- }
86
- });
87
-
88
- exports.completionsPlugin = completionsPlugin;
package/dist/index.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import * as clerc from 'clerc';
2
-
3
- interface Options {
4
- command?: boolean;
5
- }
6
- declare const completionsPlugin: (options?: Options) => clerc.Plugin<clerc.Clerc<{}>, clerc.Clerc<{}>>;
7
-
8
- export { Options, completionsPlugin };
package/dist/index.mjs DELETED
@@ -1,84 +0,0 @@
1
- import { mustArray, definePlugin, NoSuchCommandError } from 'clerc';
2
-
3
- const gracefulFlag = (n) => n.length === 1 ? `-${n}` : `--${n}`;
4
-
5
- const getCompletionValue = (command) => `[CompletionResult]::new('${command.name}', '${command.name}', [CompletionResultType]::ParameterValue, '${command.description}')`;
6
- const getCompletionFlag = (command) => {
7
- return Object.entries(command.flags || {}).map(([flagName, flag]) => {
8
- let gen = [`[CompletionResult]::new('${gracefulFlag(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || ""}')`];
9
- if (flag == null ? void 0 : flag.alias) {
10
- 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
- ];
15
- }
16
- return gen.join("\n ");
17
- }).join("\n ");
18
- };
19
- function getPwshCompletion(ctx) {
20
- const { cli } = ctx;
21
- const { _name: name, _commands: commands } = cli;
22
- return `using namespace System.Management.Automation
23
- using namespace System.Management.Automation.Language
24
-
25
- Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
26
- param($wordToComplete, $commandAst, $cursorPosition)
27
-
28
- $commandElements = $commandAst.CommandElements
29
- $command = @(
30
- '${name}'
31
- for ($i = 1; $i -lt $commandElements.Count; $i++) {
32
- $element = $commandElements[$i]
33
- if ($element -isnot [StringConstantExpressionAst] -or
34
- $element.StringConstantType -ne [StringConstantType]::BareWord -or
35
- $element.Value.StartsWith('-') -or
36
- $element.Value -eq $wordToComplete) {
37
- break
38
- }
39
- $element.Value
40
- }) -join ';'
41
-
42
- $completions = @(switch ($command) {
43
- '${name}' {
44
- ${Object.entries(commands).map(([_, command]) => getCompletionValue(command)).join("\n ")}
45
- break
46
- }
47
- ${Object.entries(commands).map(([commandName, command]) => `'${name};${commandName}' {
48
- ${getCompletionFlag(command)}
49
- break
50
- }`).join("\n ")}
51
- })
52
-
53
- $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
54
- Sort-Object -Property ListItemText
55
- }`;
56
- }
57
-
58
- const completionMap = {
59
- pwsh: getPwshCompletion
60
- };
61
- const completionsPlugin = (options = {}) => definePlugin({
62
- setup(cli) {
63
- const { command = true } = options;
64
- if (command) {
65
- cli = cli.command("completions", "Print shell completions to stdout").on("completions", (ctx) => {
66
- if (!cli._name) {
67
- throw new Error("CLI name is not defined!");
68
- }
69
- const shell = String(ctx.parameters[0]);
70
- if (!shell) {
71
- throw new Error("Missing shell name");
72
- }
73
- if (shell in completionMap) {
74
- console.log(completionMap[shell](ctx));
75
- } else {
76
- throw new NoSuchCommandError(`No such shell: ${shell}`);
77
- }
78
- });
79
- }
80
- return cli;
81
- }
82
- });
83
-
84
- export { completionsPlugin };