easycli-autocomplete 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Avijit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # @easycli/autocomplete
2
+
3
+ Automatic shell completion generator for EasyCLI applications.
4
+
5
+ ## Features
6
+
7
+ - **Multi-Shell Support**: Generates scripts for Bash, Zsh, and Fish.
8
+ - **Context Aware**: Autocompletes commands, subcommands, and flags.
9
+ - **Fast**: Static script generation for zero-latency completion.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ pnpm add @easycli/autocomplete
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { generateCompletion } from "@easycli/autocomplete";
21
+
22
+ // Get your CLI config (usually from defineCLI)
23
+ const config = {
24
+ name: "my-cli",
25
+ commands: { ... }
26
+ };
27
+
28
+ // Generate script for Bash
29
+ const script = generateCompletion("bash", config);
30
+
31
+ console.log(script);
32
+ ```
33
+
34
+ ## Integration
35
+
36
+ Typically, you would add a `completion` command to your CLI:
37
+
38
+ ```ts
39
+ import { defineCommand } from "@easycli/core";
40
+ import { generateCompletion } from "@easycli/autocomplete";
41
+
42
+ export const completion = defineCommand({
43
+ description: "Generate completion script",
44
+ args: {
45
+ shell: {
46
+ type: "enum",
47
+ values: ["bash", "zsh", "fish"]
48
+ }
49
+ },
50
+ run({ args, ctx }) {
51
+ // Assuming you have access to the full CLI config
52
+ const script = generateCompletion(args.shell, ctx.config);
53
+ console.log(script);
54
+ }
55
+ });
56
+ ```
@@ -0,0 +1,47 @@
1
+ interface CommandDef {
2
+ description?: string;
3
+ commands?: Record<string, CommandDef>;
4
+ flags?: Record<string, {
5
+ alias?: string;
6
+ description?: string;
7
+ }>;
8
+ }
9
+ interface CLIConfig {
10
+ name: string;
11
+ commands: Record<string, CommandDef>;
12
+ }
13
+ /**
14
+ * Generates Bash completion script.
15
+ *
16
+ * @param config - The CLI configuration including commands.
17
+ * @returns The Bash completion script.
18
+ */
19
+ declare function generateBashCompletion(config: CLIConfig): string;
20
+ /**
21
+ * Generates Zsh completion script.
22
+ *
23
+ * @param config - The CLI configuration including commands.
24
+ * @returns The Zsh completion script.
25
+ */
26
+ declare function generateZshCompletion(config: CLIConfig): string;
27
+ /**
28
+ * Generates Fish completion script.
29
+ *
30
+ * @param config - The CLI configuration including commands.
31
+ * @returns The Fish completion script.
32
+ */
33
+ declare function generateFishCompletion(config: CLIConfig): string;
34
+ /**
35
+ * Supported shell types for autocomplete generation.
36
+ */
37
+ type ShellType = "bash" | "zsh" | "fish";
38
+ /**
39
+ * Generates an autocomplete script for the specified shell.
40
+ *
41
+ * @param shell - The target shell ("bash", "zsh", or "fish").
42
+ * @param config - The CLI configuration including commands.
43
+ * @returns The generated shell script as a string.
44
+ */
45
+ declare function generateCompletion(shell: ShellType, config: CLIConfig): string;
46
+
47
+ export { type ShellType, generateBashCompletion, generateCompletion, generateFishCompletion, generateZshCompletion };
package/dist/index.js ADDED
@@ -0,0 +1,105 @@
1
+ // src/generator.ts
2
+ function generateBashCompletion(config) {
3
+ const lines = [];
4
+ const name = config.name;
5
+ lines.push(`_${name}_completions() {`);
6
+ lines.push(' local cur="${COMP_WORDS[COMP_CWORD]}"');
7
+ lines.push(' local prev="${COMP_WORDS[COMP_CWORD-1]}"');
8
+ lines.push("");
9
+ lines.push(" case $prev ;;");
10
+ lines.push(` ${name})`);
11
+ lines.push(
12
+ ` COMPREPLY=($(compgen -W "${Object.keys(config.commands).join(" ")}" -- "$cur"))`
13
+ );
14
+ lines.push(" return 0");
15
+ lines.push(" ;;");
16
+ for (const [cmdName, cmd] of Object.entries(config.commands)) {
17
+ if (cmd.commands) {
18
+ lines.push(` ${cmdName})`);
19
+ lines.push(
20
+ ` COMPREPLY=($(compgen -W "${Object.keys(cmd.commands).join(" ")}" -- "$cur"))`
21
+ );
22
+ lines.push(" return 0");
23
+ lines.push(" ;;");
24
+ }
25
+ }
26
+ lines.push(" esac");
27
+ lines.push("");
28
+ lines.push(' COMPREPLY=($(compgen -W "--help --version" -- "$cur"))');
29
+ lines.push("}");
30
+ lines.push("");
31
+ lines.push(`complete -F _${name}_completions ${name}`);
32
+ return lines.join("\n");
33
+ }
34
+ function generateZshCompletion(config) {
35
+ const lines = [];
36
+ const name = config.name;
37
+ lines.push(`#compdef ${name}`);
38
+ lines.push("");
39
+ lines.push(`_${name}() {`);
40
+ lines.push(" local -a commands");
41
+ lines.push(" commands=(");
42
+ for (const [cmdName, cmd] of Object.entries(config.commands)) {
43
+ const desc = cmd.description ?? cmdName;
44
+ lines.push(` '${cmdName}:${desc}'`);
45
+ }
46
+ lines.push(" )");
47
+ lines.push("");
48
+ lines.push(" _arguments -C \\");
49
+ lines.push(" '--help[Show help]' \\");
50
+ lines.push(" '--version[Show version]' \\");
51
+ lines.push(" '1:command:->commands' \\");
52
+ lines.push(" '*::arg:->args'");
53
+ lines.push("");
54
+ lines.push(" case $state in");
55
+ lines.push(" commands)");
56
+ lines.push(" _describe 'command' commands");
57
+ lines.push(" ;;");
58
+ lines.push(" esac");
59
+ lines.push("}");
60
+ lines.push("");
61
+ lines.push(`compdef _${name} ${name}`);
62
+ return lines.join("\n");
63
+ }
64
+ function generateFishCompletion(config) {
65
+ const lines = [];
66
+ const name = config.name;
67
+ lines.push(
68
+ `complete -c ${name} -f -n "__fish_use_subcommand" -a "--help" -d "Show help"`
69
+ );
70
+ lines.push(
71
+ `complete -c ${name} -f -n "__fish_use_subcommand" -a "--version" -d "Show version"`
72
+ );
73
+ for (const [cmdName, cmd] of Object.entries(config.commands)) {
74
+ const desc = cmd.description ?? cmdName;
75
+ lines.push(
76
+ `complete -c ${name} -f -n "__fish_use_subcommand" -a "${cmdName}" -d "${desc}"`
77
+ );
78
+ if (cmd.commands) {
79
+ for (const [subName, subCmd] of Object.entries(cmd.commands)) {
80
+ const subDesc = subCmd.description ?? subName;
81
+ lines.push(
82
+ `complete -c ${name} -f -n "__fish_seen_subcommand_from ${cmdName}" -a "${subName}" -d "${subDesc}"`
83
+ );
84
+ }
85
+ }
86
+ }
87
+ return lines.join("\n");
88
+ }
89
+ function generateCompletion(shell, config) {
90
+ switch (shell) {
91
+ case "bash":
92
+ return generateBashCompletion(config);
93
+ case "zsh":
94
+ return generateZshCompletion(config);
95
+ case "fish":
96
+ return generateFishCompletion(config);
97
+ }
98
+ }
99
+ export {
100
+ generateBashCompletion,
101
+ generateCompletion,
102
+ generateFishCompletion,
103
+ generateZshCompletion
104
+ };
105
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/generator.ts"],"sourcesContent":["interface CommandDef {\r\n description?: string;\r\n commands?: Record<string, CommandDef>;\r\n flags?: Record<string, { alias?: string; description?: string }>;\r\n}\r\n\r\ninterface CLIConfig {\r\n name: string;\r\n commands: Record<string, CommandDef>;\r\n}\r\n\r\n/**\r\n * Generates Bash completion script.\r\n *\r\n * @param config - The CLI configuration including commands.\r\n * @returns The Bash completion script.\r\n */\r\nexport function generateBashCompletion(config: CLIConfig): string {\r\n const lines: string[] = [];\r\n const name = config.name;\r\n\r\n lines.push(`_${name}_completions() {`);\r\n lines.push(' local cur=\"${COMP_WORDS[COMP_CWORD]}\"');\r\n lines.push(' local prev=\"${COMP_WORDS[COMP_CWORD-1]}\"');\r\n lines.push(\"\");\r\n lines.push(\" case $prev ;;\");\r\n lines.push(` ${name})`);\r\n lines.push(\r\n ` COMPREPLY=($(compgen -W \"${Object.keys(config.commands).join(\" \")}\" -- \"$cur\"))`\r\n );\r\n lines.push(\" return 0\");\r\n lines.push(\" ;;\");\r\n\r\n for (const [cmdName, cmd] of Object.entries(config.commands)) {\r\n if (cmd.commands) {\r\n lines.push(` ${cmdName})`);\r\n lines.push(\r\n ` COMPREPLY=($(compgen -W \"${Object.keys(cmd.commands).join(\" \")}\" -- \"$cur\"))`\r\n );\r\n lines.push(\" return 0\");\r\n lines.push(\" ;;\");\r\n }\r\n }\r\n\r\n lines.push(\" esac\");\r\n lines.push(\"\");\r\n lines.push(' COMPREPLY=($(compgen -W \"--help --version\" -- \"$cur\"))');\r\n lines.push(\"}\");\r\n lines.push(\"\");\r\n lines.push(`complete -F _${name}_completions ${name}`);\r\n\r\n return lines.join(\"\\n\");\r\n}\r\n\r\n/**\r\n * Generates Zsh completion script.\r\n *\r\n * @param config - The CLI configuration including commands.\r\n * @returns The Zsh completion script.\r\n */\r\nexport function generateZshCompletion(config: CLIConfig): string {\r\n const lines: string[] = [];\r\n const name = config.name;\r\n\r\n lines.push(`#compdef ${name}`);\r\n lines.push(\"\");\r\n lines.push(`_${name}() {`);\r\n lines.push(\" local -a commands\");\r\n lines.push(\" commands=(\");\r\n\r\n for (const [cmdName, cmd] of Object.entries(config.commands)) {\r\n const desc = cmd.description ?? cmdName;\r\n lines.push(` '${cmdName}:${desc}'`);\r\n }\r\n\r\n lines.push(\" )\");\r\n lines.push(\"\");\r\n lines.push(\" _arguments -C \\\\\");\r\n lines.push(\" '--help[Show help]' \\\\\");\r\n lines.push(\" '--version[Show version]' \\\\\");\r\n lines.push(\" '1:command:->commands' \\\\\");\r\n lines.push(\" '*::arg:->args'\");\r\n lines.push(\"\");\r\n lines.push(\" case $state in\");\r\n lines.push(\" commands)\");\r\n lines.push(\" _describe 'command' commands\");\r\n lines.push(\" ;;\");\r\n lines.push(\" esac\");\r\n lines.push(\"}\");\r\n lines.push(\"\");\r\n lines.push(`compdef _${name} ${name}`);\r\n\r\n return lines.join(\"\\n\");\r\n}\r\n\r\n/**\r\n * Generates Fish completion script.\r\n *\r\n * @param config - The CLI configuration including commands.\r\n * @returns The Fish completion script.\r\n */\r\nexport function generateFishCompletion(config: CLIConfig): string {\r\n const lines: string[] = [];\r\n const name = config.name;\r\n\r\n lines.push(\r\n `complete -c ${name} -f -n \"__fish_use_subcommand\" -a \"--help\" -d \"Show help\"`\r\n );\r\n lines.push(\r\n `complete -c ${name} -f -n \"__fish_use_subcommand\" -a \"--version\" -d \"Show version\"`\r\n );\r\n\r\n for (const [cmdName, cmd] of Object.entries(config.commands)) {\r\n const desc = cmd.description ?? cmdName;\r\n lines.push(\r\n `complete -c ${name} -f -n \"__fish_use_subcommand\" -a \"${cmdName}\" -d \"${desc}\"`\r\n );\r\n\r\n if (cmd.commands) {\r\n for (const [subName, subCmd] of Object.entries(cmd.commands)) {\r\n const subDesc = subCmd.description ?? subName;\r\n lines.push(\r\n `complete -c ${name} -f -n \"__fish_seen_subcommand_from ${cmdName}\" -a \"${subName}\" -d \"${subDesc}\"`\r\n );\r\n }\r\n }\r\n }\r\n\r\n return lines.join(\"\\n\");\r\n}\r\n\r\n/**\r\n * Supported shell types for autocomplete generation.\r\n */\r\nexport type ShellType = \"bash\" | \"zsh\" | \"fish\";\r\n\r\n/**\r\n * Generates an autocomplete script for the specified shell.\r\n *\r\n * @param shell - The target shell (\"bash\", \"zsh\", or \"fish\").\r\n * @param config - The CLI configuration including commands.\r\n * @returns The generated shell script as a string.\r\n */\r\nexport function generateCompletion(\r\n shell: ShellType,\r\n config: CLIConfig\r\n): string {\r\n switch (shell) {\r\n case \"bash\":\r\n return generateBashCompletion(config);\r\n case \"zsh\":\r\n return generateZshCompletion(config);\r\n case \"fish\":\r\n return generateFishCompletion(config);\r\n }\r\n}\r\n"],"mappings":";AAiBO,SAAS,uBAAuB,QAA2B;AAChE,QAAM,QAAkB,CAAC;AACzB,QAAM,OAAO,OAAO;AAEpB,QAAM,KAAK,IAAI,IAAI,kBAAkB;AACrC,QAAM,KAAK,yCAAyC;AACpD,QAAM,KAAK,4CAA4C;AACvD,QAAM,KAAK,EAAE;AACb,QAAM,KAAK,iBAAiB;AAC5B,QAAM,KAAK,OAAO,IAAI,GAAG;AACzB,QAAM;AAAA,IACJ,kCAAkC,OAAO,KAAK,OAAO,QAAQ,EAAE,KAAK,GAAG,CAAC;AAAA,EAC1E;AACA,QAAM,KAAK,gBAAgB;AAC3B,QAAM,KAAK,UAAU;AAErB,aAAW,CAAC,SAAS,GAAG,KAAK,OAAO,QAAQ,OAAO,QAAQ,GAAG;AAC5D,QAAI,IAAI,UAAU;AAChB,YAAM,KAAK,OAAO,OAAO,GAAG;AAC5B,YAAM;AAAA,QACJ,kCAAkC,OAAO,KAAK,IAAI,QAAQ,EAAE,KAAK,GAAG,CAAC;AAAA,MACvE;AACA,YAAM,KAAK,gBAAgB;AAC3B,YAAM,KAAK,UAAU;AAAA,IACvB;AAAA,EACF;AAEA,QAAM,KAAK,QAAQ;AACnB,QAAM,KAAK,EAAE;AACb,QAAM,KAAK,0DAA0D;AACrE,QAAM,KAAK,GAAG;AACd,QAAM,KAAK,EAAE;AACb,QAAM,KAAK,gBAAgB,IAAI,gBAAgB,IAAI,EAAE;AAErD,SAAO,MAAM,KAAK,IAAI;AACxB;AAQO,SAAS,sBAAsB,QAA2B;AAC/D,QAA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QAAQ,IAAI,QAAQ,GAAG;AAC5D,cAAM,UAAU,OAAO,eAAe;AACtC,cAAM;AAAA,UACJ,eAAe,IAAI,uCAAuC,OAAO,SAAS,OAAO,SAAS,OAAO;AAAA,QACnG;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO,MAAM,KAAK,IAAI;AACxB;AAcO,SAAS,mBACd,OACA,QACQ;AACR,UAAQ,OAAO;AAAA,IACb,KAAK;AACH,aAAO,uBAAuB,MAAM;AAAA,IACtC,KAAK;AACH,aAAO,sBAAsB,MAAM;AAAA,IACrC,KAAK;AACH,aAAO,uBAAuB,MAAM;AAAA,EACxC;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "easycli-autocomplete",
3
+ "version": "0.0.1",
4
+ "description": "Shell autocomplete generator for EasyCLI",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "devDependencies": {
19
+ "tsup": "^8.0.1",
20
+ "typescript": "^5.3.3",
21
+ "rimraf": "^5.0.5"
22
+ },
23
+ "scripts": {
24
+ "build": "tsup",
25
+ "dev": "tsup --watch",
26
+ "clean": "rimraf dist",
27
+ "typecheck": "tsc --noEmit"
28
+ }
29
+ }