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