@clerc/plugin-completions 0.4.0 → 0.5.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/dist/index.cjs +128 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.mjs +124 -0
- package/package.json +3 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var clerc = require('clerc');
|
|
6
|
+
var utils = require('@clerc/utils');
|
|
7
|
+
|
|
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
|
+
};
|
|
39
|
+
|
|
40
|
+
const NO_DESCRIPTION = "(No Description)";
|
|
41
|
+
const getCompletionValue = (command) => `[CompletionResult]::new('${command.name}', '${command.name}', [CompletionResultType]::ParameterValue, '${command.description}')`;
|
|
42
|
+
const getCompletionFlag = (command) => {
|
|
43
|
+
return Object.entries(command.flags || {}).map(([flagName, flag]) => {
|
|
44
|
+
const gen = [`[CompletionResult]::new('${utils.gracefulFlagName(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`];
|
|
45
|
+
if (flag == null ? void 0 : flag.alias) {
|
|
46
|
+
const arrayAlias = utils.mustArray(flag.alias);
|
|
47
|
+
gen.push(
|
|
48
|
+
...arrayAlias.map((n) => `[CompletionResult]::new('${utils.gracefulFlagName(n)}', '${n}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return gen.join("\n ");
|
|
52
|
+
}).join("\n ");
|
|
53
|
+
};
|
|
54
|
+
const getPwshCompletion = (ctx) => {
|
|
55
|
+
const { cli } = ctx;
|
|
56
|
+
const { _name: name, _commands: commands } = cli;
|
|
57
|
+
return `using namespace System.Management.Automation
|
|
58
|
+
using namespace System.Management.Automation.Language
|
|
59
|
+
|
|
60
|
+
Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
|
|
61
|
+
param($wordToComplete, $commandAst, $cursorPosition)
|
|
62
|
+
|
|
63
|
+
$commandElements = $commandAst.CommandElements
|
|
64
|
+
$command = @(
|
|
65
|
+
'${name}'
|
|
66
|
+
for ($i = 1; $i -lt $commandElements.Count; $i++) {
|
|
67
|
+
$element = $commandElements[$i]
|
|
68
|
+
if ($element -isnot [StringConstantExpressionAst] -or
|
|
69
|
+
$element.StringConstantType -ne [StringConstantType]::BareWord -or
|
|
70
|
+
$element.Value.StartsWith('-') -or
|
|
71
|
+
$element.Value -eq $wordToComplete) {
|
|
72
|
+
break
|
|
73
|
+
}
|
|
74
|
+
$element.Value
|
|
75
|
+
}) -join ';'
|
|
76
|
+
|
|
77
|
+
$completions = @(switch ($command) {
|
|
78
|
+
'${name}' {
|
|
79
|
+
${Object.entries(commands).map(([_, command]) => getCompletionValue(command)).join("\n ")}
|
|
80
|
+
break
|
|
81
|
+
}
|
|
82
|
+
${Object.entries(commands).map(([commandName, command]) => `'${name};${commandName.split(" ").join(";")}' {
|
|
83
|
+
${getCompletionFlag(command)}
|
|
84
|
+
break
|
|
85
|
+
}`).join("\n ")}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
89
|
+
Sort-Object -Property ListItemText
|
|
90
|
+
}`;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const completionMap = {
|
|
94
|
+
bash: getBashCompletion,
|
|
95
|
+
pwsh: getPwshCompletion
|
|
96
|
+
};
|
|
97
|
+
const completionsPlugin = (options = {}) => clerc.definePlugin({
|
|
98
|
+
setup(cli) {
|
|
99
|
+
const { command = true } = options;
|
|
100
|
+
if (command) {
|
|
101
|
+
cli = cli.command("completions", "Print shell completions to stdout", {
|
|
102
|
+
flags: {
|
|
103
|
+
shell: {
|
|
104
|
+
description: "Shell type",
|
|
105
|
+
type: String,
|
|
106
|
+
default: ""
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}).on("completions", (ctx) => {
|
|
110
|
+
if (!cli._name) {
|
|
111
|
+
throw new Error("CLI name is not defined!");
|
|
112
|
+
}
|
|
113
|
+
const shell = String(ctx.parameters[0] || ctx.flags.shell);
|
|
114
|
+
if (!shell) {
|
|
115
|
+
throw new Error("Missing shell name");
|
|
116
|
+
}
|
|
117
|
+
if (shell in completionMap) {
|
|
118
|
+
console.log(completionMap[shell](ctx));
|
|
119
|
+
} else {
|
|
120
|
+
throw new Error(`No such shell: ${shell}`);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return cli;
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
exports.completionsPlugin = completionsPlugin;
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { definePlugin } from 'clerc';
|
|
2
|
+
import { gracefulFlagName, mustArray } 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)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`];
|
|
41
|
+
if (flag == null ? void 0 : flag.alias) {
|
|
42
|
+
const arrayAlias = mustArray(flag.alias);
|
|
43
|
+
gen.push(
|
|
44
|
+
...arrayAlias.map((n) => `[CompletionResult]::new('${gracefulFlagName(n)}', '${n}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return gen.join("\n ");
|
|
48
|
+
}).join("\n ");
|
|
49
|
+
};
|
|
50
|
+
const getPwshCompletion = (ctx) => {
|
|
51
|
+
const { cli } = ctx;
|
|
52
|
+
const { _name: name, _commands: commands } = cli;
|
|
53
|
+
return `using namespace System.Management.Automation
|
|
54
|
+
using namespace System.Management.Automation.Language
|
|
55
|
+
|
|
56
|
+
Register-ArgumentCompleter -Native -CommandName '${name}' -ScriptBlock {
|
|
57
|
+
param($wordToComplete, $commandAst, $cursorPosition)
|
|
58
|
+
|
|
59
|
+
$commandElements = $commandAst.CommandElements
|
|
60
|
+
$command = @(
|
|
61
|
+
'${name}'
|
|
62
|
+
for ($i = 1; $i -lt $commandElements.Count; $i++) {
|
|
63
|
+
$element = $commandElements[$i]
|
|
64
|
+
if ($element -isnot [StringConstantExpressionAst] -or
|
|
65
|
+
$element.StringConstantType -ne [StringConstantType]::BareWord -or
|
|
66
|
+
$element.Value.StartsWith('-') -or
|
|
67
|
+
$element.Value -eq $wordToComplete) {
|
|
68
|
+
break
|
|
69
|
+
}
|
|
70
|
+
$element.Value
|
|
71
|
+
}) -join ';'
|
|
72
|
+
|
|
73
|
+
$completions = @(switch ($command) {
|
|
74
|
+
'${name}' {
|
|
75
|
+
${Object.entries(commands).map(([_, command]) => getCompletionValue(command)).join("\n ")}
|
|
76
|
+
break
|
|
77
|
+
}
|
|
78
|
+
${Object.entries(commands).map(([commandName, command]) => `'${name};${commandName.split(" ").join(";")}' {
|
|
79
|
+
${getCompletionFlag(command)}
|
|
80
|
+
break
|
|
81
|
+
}`).join("\n ")}
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
85
|
+
Sort-Object -Property ListItemText
|
|
86
|
+
}`;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const completionMap = {
|
|
90
|
+
bash: getBashCompletion,
|
|
91
|
+
pwsh: getPwshCompletion
|
|
92
|
+
};
|
|
93
|
+
const completionsPlugin = (options = {}) => definePlugin({
|
|
94
|
+
setup(cli) {
|
|
95
|
+
const { command = true } = options;
|
|
96
|
+
if (command) {
|
|
97
|
+
cli = cli.command("completions", "Print shell completions to stdout", {
|
|
98
|
+
flags: {
|
|
99
|
+
shell: {
|
|
100
|
+
description: "Shell type",
|
|
101
|
+
type: String,
|
|
102
|
+
default: ""
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}).on("completions", (ctx) => {
|
|
106
|
+
if (!cli._name) {
|
|
107
|
+
throw new Error("CLI name is not defined!");
|
|
108
|
+
}
|
|
109
|
+
const shell = String(ctx.parameters[0] || ctx.flags.shell);
|
|
110
|
+
if (!shell) {
|
|
111
|
+
throw new Error("Missing shell name");
|
|
112
|
+
}
|
|
113
|
+
if (shell in completionMap) {
|
|
114
|
+
console.log(completionMap[shell](ctx));
|
|
115
|
+
} else {
|
|
116
|
+
throw new Error(`No such shell: ${shell}`);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return cli;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
export { completionsPlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/plugin-completions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc plugin completions",
|
|
6
6
|
"keywords": [
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"clerc": "*"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@clerc/utils": "0.
|
|
47
|
-
"clerc": "0.
|
|
46
|
+
"@clerc/utils": "0.5.0",
|
|
47
|
+
"clerc": "0.5.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "puild",
|