@clerc/plugin-completions 1.0.0-beta.2 → 1.0.0-beta.20
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.d.ts +15 -0
- package/dist/index.js +45 -24
- package/package.json +4 -8
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { Plugin } from "@clerc/core";
|
|
2
2
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
|
+
declare module "@clerc/core" {
|
|
5
|
+
interface CommandCustomOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Completions options for the command.
|
|
8
|
+
*/
|
|
9
|
+
completions?: {
|
|
10
|
+
/**
|
|
11
|
+
* Whether to show the command in completions output.
|
|
12
|
+
*
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
show?: boolean;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
4
19
|
interface CompletionsPluginOptions {
|
|
5
20
|
/**
|
|
6
21
|
* Whether to register the `completions install` and `completions uninstall` commands.
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import { definePlugin, resolveCommand } from "@clerc/core";
|
|
1
|
+
import { DOUBLE_DASH, Types, definePlugin, normalizeFlagValue, resolveCommand } from "@clerc/core";
|
|
2
2
|
import tabtab, { getShellFromEnv } from "@pnpm/tabtab";
|
|
3
|
-
import { formatFlagName, toArray } from "@clerc/utils";
|
|
4
3
|
|
|
4
|
+
//#region ../utils/src/index.ts
|
|
5
|
+
const toArray = (a) => Array.isArray(a) ? a : [a];
|
|
6
|
+
const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
|
|
7
|
+
const formatFlagName = (n) => n.length <= 1 ? `-${n}` : `--${kebabCase(n)}`;
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
5
10
|
//#region src/complete.ts
|
|
6
11
|
function splitCommand(cmd) {
|
|
7
12
|
const args = [];
|
|
@@ -33,7 +38,7 @@ function splitCommand(cmd) {
|
|
|
33
38
|
}
|
|
34
39
|
async function getCompletion(cli, env) {
|
|
35
40
|
const inputArgv = splitCommand(env.partial.slice(0, env.partial.length - env.lastPartial.length)).slice(1);
|
|
36
|
-
if (inputArgv.includes(
|
|
41
|
+
if (inputArgv.includes(DOUBLE_DASH)) return [];
|
|
37
42
|
const [command, commandName] = resolveCommand(cli._commands, inputArgv);
|
|
38
43
|
if (env.lastPartial.startsWith("-")) {
|
|
39
44
|
const flags = command ? {
|
|
@@ -42,15 +47,16 @@ async function getCompletion(cli, env) {
|
|
|
42
47
|
} : cli._globalFlags;
|
|
43
48
|
const candidates$1 = [];
|
|
44
49
|
for (const [name, def] of Object.entries(flags)) {
|
|
50
|
+
const normalized = normalizeFlagValue(def);
|
|
45
51
|
candidates$1.push({
|
|
46
52
|
name: formatFlagName(name),
|
|
47
|
-
description:
|
|
53
|
+
description: normalized.description
|
|
48
54
|
});
|
|
49
|
-
if (
|
|
50
|
-
const aliases = toArray(
|
|
55
|
+
if (normalized.alias) {
|
|
56
|
+
const aliases = toArray(normalized.alias);
|
|
51
57
|
for (const alias of aliases) candidates$1.push({
|
|
52
58
|
name: formatFlagName(alias),
|
|
53
|
-
description:
|
|
59
|
+
description: normalized.description
|
|
54
60
|
});
|
|
55
61
|
}
|
|
56
62
|
}
|
|
@@ -63,12 +69,15 @@ async function getCompletion(cli, env) {
|
|
|
63
69
|
const remainingArgs = inputArgv.slice(matchedParts.length);
|
|
64
70
|
prefix = `${[command.name, ...remainingArgs].join(" ")} `;
|
|
65
71
|
} else prefix = inputArgv.length > 0 ? `${inputArgv.join(" ")} ` : "";
|
|
66
|
-
for (const
|
|
67
|
-
|
|
68
|
-
if (
|
|
69
|
-
name
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
for (const command$1 of cli._commands.values()) {
|
|
73
|
+
if (command$1.completions?.show === false) continue;
|
|
74
|
+
if (command$1.name.startsWith(prefix)) {
|
|
75
|
+
const nextWord = command$1.name.slice(prefix.length).split(" ")[0];
|
|
76
|
+
if (nextWord) candidates.push({
|
|
77
|
+
name: nextWord,
|
|
78
|
+
description: command$1.description
|
|
79
|
+
});
|
|
80
|
+
}
|
|
72
81
|
}
|
|
73
82
|
const uniqueCandidates = /* @__PURE__ */ new Map();
|
|
74
83
|
for (const c of candidates) uniqueCandidates.set(c.name, c);
|
|
@@ -79,45 +88,57 @@ async function getCompletion(cli, env) {
|
|
|
79
88
|
//#region src/index.ts
|
|
80
89
|
const completionsPlugin = (options = {}) => definePlugin({ setup: (cli) => {
|
|
81
90
|
const { managementCommands = true } = options;
|
|
91
|
+
cli.store.help?.addGroup({ commands: [["completions", "Completions"]] });
|
|
92
|
+
const supportedShellEnum = Types.Enum(...tabtab.SUPPORTED_SHELLS);
|
|
82
93
|
if (managementCommands) {
|
|
83
94
|
cli.command("completions install", "Install shell completions", {
|
|
95
|
+
help: { group: "completions" },
|
|
84
96
|
flags: { shell: {
|
|
85
97
|
description: "Shell type",
|
|
86
|
-
type:
|
|
98
|
+
type: supportedShellEnum
|
|
87
99
|
} },
|
|
88
|
-
parameters: [
|
|
100
|
+
parameters: [{
|
|
101
|
+
key: "[shell]",
|
|
102
|
+
description: "Shell type",
|
|
103
|
+
type: supportedShellEnum
|
|
104
|
+
}]
|
|
89
105
|
}).on("completions install", async (ctx) => {
|
|
90
106
|
const shell = ctx.parameters.shell ?? ctx.flags.shell;
|
|
91
107
|
if (!shell) throw new Error("Please specify the shell type via the --shell flag or the [shell] parameter.");
|
|
92
108
|
if (!tabtab.SUPPORTED_SHELLS.includes(shell)) throw new Error(`Unsupported shell: ${shell}. Supported shells are: ${tabtab.SUPPORTED_SHELLS.join(", ")}.`);
|
|
93
109
|
await tabtab.install({
|
|
94
|
-
name: cli.
|
|
95
|
-
completer: cli.
|
|
110
|
+
name: cli._scriptName,
|
|
111
|
+
completer: cli._scriptName,
|
|
96
112
|
shell
|
|
97
113
|
});
|
|
98
114
|
});
|
|
99
|
-
cli.command("completions uninstall", "Uninstall shell completions").on("completions uninstall", async () => {
|
|
100
|
-
await tabtab.uninstall({ name: cli.
|
|
115
|
+
cli.command("completions uninstall", "Uninstall shell completions", { help: { group: "completions" } }).on("completions uninstall", async () => {
|
|
116
|
+
await tabtab.uninstall({ name: cli._scriptName });
|
|
101
117
|
});
|
|
102
118
|
}
|
|
103
119
|
cli.command("completions", "Generate completions script", {
|
|
120
|
+
help: { group: "completions" },
|
|
104
121
|
flags: { shell: {
|
|
105
122
|
description: "Shell type",
|
|
106
|
-
type:
|
|
123
|
+
type: supportedShellEnum
|
|
107
124
|
} },
|
|
108
|
-
parameters: [
|
|
125
|
+
parameters: [{
|
|
126
|
+
key: "[shell]",
|
|
127
|
+
description: "Shell type",
|
|
128
|
+
type: supportedShellEnum
|
|
129
|
+
}]
|
|
109
130
|
}).on("completions", async (ctx) => {
|
|
110
131
|
const shell = ctx.parameters.shell ?? ctx.flags.shell;
|
|
111
132
|
if (!shell) throw new Error("Please specify the shell type via the --shell flag or the [shell] parameter.");
|
|
112
133
|
if (!tabtab.SUPPORTED_SHELLS.includes(shell)) throw new Error(`Unsupported shell: ${shell}. Supported shells are: ${tabtab.SUPPORTED_SHELLS.join(", ")}.`);
|
|
113
134
|
const script = await tabtab.getCompletionScript({
|
|
114
|
-
name: cli.
|
|
115
|
-
completer: cli.
|
|
135
|
+
name: cli._scriptName,
|
|
136
|
+
completer: cli._scriptName,
|
|
116
137
|
shell
|
|
117
138
|
});
|
|
118
139
|
console.log(script);
|
|
119
140
|
});
|
|
120
|
-
cli.command("completion-server", "Handle completions", { help: {
|
|
141
|
+
cli.command("completion-server", "Handle completions", { help: { show: false } }).on("completion-server", async () => {
|
|
121
142
|
const env = tabtab.parseEnv(process.env);
|
|
122
143
|
if (!env.complete) return;
|
|
123
144
|
const shell = getShellFromEnv(process.env);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/plugin-completions",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.20",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc plugin completions",
|
|
@@ -45,17 +45,13 @@
|
|
|
45
45
|
"access": "public"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@pnpm/tabtab": "^0.5.4"
|
|
49
|
-
"@clerc/utils": "1.0.0-beta.2"
|
|
48
|
+
"@pnpm/tabtab": "^0.5.4"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
52
|
-
"@clerc/core": "1.0.0-beta.
|
|
51
|
+
"@clerc/core": "1.0.0-beta.20",
|
|
52
|
+
"@clerc/utils": "1.0.0-beta.20"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@clerc/core": "*"
|
|
56
|
-
},
|
|
57
|
-
"scripts": {
|
|
58
|
-
"build": "tsdown",
|
|
59
|
-
"watch": "tsdown --watch"
|
|
60
56
|
}
|
|
61
57
|
}
|