@clerc/plugin-completions 1.0.0-beta.1 → 1.0.0-beta.10
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 +16 -1
- package/dist/index.js +21 -11
- package/package.json +4 -8
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
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
|
-
* Whether to register the `install
|
|
21
|
+
* Whether to register the `completions install` and `completions uninstall` commands.
|
|
7
22
|
* @default true
|
|
8
23
|
*/
|
|
9
24
|
managementCommands?: boolean;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import { definePlugin, resolveCommand } from "@clerc/core";
|
|
1
|
+
import { DOUBLE_DASH, definePlugin, 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 ? {
|
|
@@ -63,12 +68,15 @@ async function getCompletion(cli, env) {
|
|
|
63
68
|
const remainingArgs = inputArgv.slice(matchedParts.length);
|
|
64
69
|
prefix = `${[command.name, ...remainingArgs].join(" ")} `;
|
|
65
70
|
} else prefix = inputArgv.length > 0 ? `${inputArgv.join(" ")} ` : "";
|
|
66
|
-
for (const
|
|
67
|
-
|
|
68
|
-
if (
|
|
69
|
-
name
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
for (const command$1 of cli._commands.values()) {
|
|
72
|
+
if (command$1.completions?.show === false) continue;
|
|
73
|
+
if (command$1.name.startsWith(prefix)) {
|
|
74
|
+
const nextWord = command$1.name.slice(prefix.length).split(" ")[0];
|
|
75
|
+
if (nextWord) candidates.push({
|
|
76
|
+
name: nextWord,
|
|
77
|
+
description: command$1.description
|
|
78
|
+
});
|
|
79
|
+
}
|
|
72
80
|
}
|
|
73
81
|
const uniqueCandidates = /* @__PURE__ */ new Map();
|
|
74
82
|
for (const c of candidates) uniqueCandidates.set(c.name, c);
|
|
@@ -81,6 +89,7 @@ const completionsPlugin = (options = {}) => definePlugin({ setup: (cli) => {
|
|
|
81
89
|
const { managementCommands = true } = options;
|
|
82
90
|
if (managementCommands) {
|
|
83
91
|
cli.command("completions install", "Install shell completions", {
|
|
92
|
+
help: { group: "completions" },
|
|
84
93
|
flags: { shell: {
|
|
85
94
|
description: "Shell type",
|
|
86
95
|
type: String
|
|
@@ -96,11 +105,12 @@ const completionsPlugin = (options = {}) => definePlugin({ setup: (cli) => {
|
|
|
96
105
|
shell
|
|
97
106
|
});
|
|
98
107
|
});
|
|
99
|
-
cli.command("completions uninstall", "Uninstall shell completions").on("completions uninstall", async () => {
|
|
108
|
+
cli.command("completions uninstall", "Uninstall shell completions", { help: { group: "completions" } }).on("completions uninstall", async () => {
|
|
100
109
|
await tabtab.uninstall({ name: cli._name });
|
|
101
110
|
});
|
|
102
111
|
}
|
|
103
112
|
cli.command("completions", "Generate completions script", {
|
|
113
|
+
help: { group: "completions" },
|
|
104
114
|
flags: { shell: {
|
|
105
115
|
description: "Shell type",
|
|
106
116
|
type: String
|
|
@@ -117,7 +127,7 @@ const completionsPlugin = (options = {}) => definePlugin({ setup: (cli) => {
|
|
|
117
127
|
});
|
|
118
128
|
console.log(script);
|
|
119
129
|
});
|
|
120
|
-
cli.command("completion-server", "Handle completions").on("completion-server", async () => {
|
|
130
|
+
cli.command("completion-server", "Handle completions", { help: { show: false } }).on("completion-server", async () => {
|
|
121
131
|
const env = tabtab.parseEnv(process.env);
|
|
122
132
|
if (!env.complete) return;
|
|
123
133
|
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.10",
|
|
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.1"
|
|
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.10",
|
|
52
|
+
"@clerc/utils": "1.0.0-beta.10"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@clerc/core": "*"
|
|
56
|
-
},
|
|
57
|
-
"scripts": {
|
|
58
|
-
"build": "tsdown",
|
|
59
|
-
"watch": "tsdown --watch"
|
|
60
56
|
}
|
|
61
57
|
}
|