@breadc/complete 0.8.3 → 0.8.5

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/README.md CHANGED
@@ -8,6 +8,28 @@
8
8
  npm i @breadc/complete
9
9
  ```
10
10
 
11
+ Add this plugin to your CLI script.
12
+
13
+ ```ts
14
+ import breadc from 'breadc'
15
+ import complete from '@breadc/complete'
16
+
17
+ const cli = breadc('echo', { version: '1.0.0', plugins: [complete()] })
18
+ .option('--host <host>', { default: 'localhost' })
19
+ .option('--port <port>', { default: '3000', cast: p => +p })
20
+
21
+ cli
22
+ .command('[message]', 'Say something!')
23
+ .action((message, option) => {
24
+ const host = option.host
25
+ const port = option.port
26
+ console.log(`Host: ${host}`)
27
+ console.log(`Port: ${port}`)
28
+ })
29
+
30
+ cli.run(process.argv.slice(2)).catch(err => console.error(err))
31
+ ```
32
+
11
33
  ## License
12
34
 
13
35
  MIT License © 2023 [XLor](https://github.com/yjl9903)
package/dist/index.cjs CHANGED
@@ -1,6 +1,135 @@
1
1
  'use strict';
2
2
 
3
- function Complete() {
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const breadc = require('breadc');
6
+
7
+ const generatePowershell = (breadc, commands, globalOptions) => {
8
+ const bin = breadc.name;
9
+ const subcommands = generateSubcommands(breadc, commands, globalOptions);
10
+ const template = `using namespace System.Management.Automation
11
+ using namespace System.Management.Automation.Language
12
+ Register-ArgumentCompleter -Native -CommandName '${bin}' -ScriptBlock {
13
+ param($wordToComplete, $commandAst, $cursorPosition)
14
+ $commandElements = $commandAst.CommandElements
15
+ $command = @(
16
+ '${bin}'
17
+ for ($i = 1; $i -lt $commandElements.Count; $i++) {
18
+ $element = $commandElements[$i]
19
+ if ($element -isnot [StringConstantExpressionAst] -or
20
+ $element.StringConstantType -ne [StringConstantType]::BareWord -or
21
+ $element.Value.StartsWith('-') -or
22
+ $element.Value -eq $wordToComplete) {
23
+ break
24
+ }
25
+ $element.Value
26
+ }) -join ';'
27
+ $completions = @(switch ($command) {${subcommands}
28
+ })
29
+ $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
30
+ Sort-Object -Property ListItemText
31
+ }`;
32
+ return template;
33
+ };
34
+ function generateSubcommands(breadc, commands, globalOptions) {
35
+ const cases = [];
36
+ cases.push(
37
+ [
38
+ "",
39
+ `'${breadc.name}' {`,
40
+ ...commands.map(
41
+ (c) => ` [CompletionResult]::new('${c._arguments[0].name}', '${c._arguments[0].name}', [CompletionResultType]::ParameterValue, '${c.description}')`
42
+ ),
43
+ ...globalOptions.map(
44
+ (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')`
45
+ ),
46
+ ` break`,
47
+ `}`
48
+ ].map((t) => " " + t).join("\n")
49
+ );
50
+ for (const command of commands) {
51
+ const args = command._arguments.filter((a) => a.type === "const").map((a) => a.name).join(";");
52
+ cases.push(
53
+ [
54
+ "",
55
+ `'${breadc.name};${args}' {`,
56
+ ...command._options.map(
57
+ (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')`
58
+ ),
59
+ ...globalOptions.map(
60
+ (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')`
61
+ ),
62
+ ` break`,
63
+ `}`
64
+ ].map((t) => " " + t).join("\n")
65
+ );
66
+ }
67
+ return cases.join("\n");
68
+ }
69
+
70
+ function generate(shell, breadc$1, allCommands, globalOptions) {
71
+ if (shell === "powershell") {
72
+ return generatePowershell(breadc$1, allCommands, globalOptions);
73
+ } else {
74
+ throw new breadc.ParseError(`Unsupport shell ${shell}`);
75
+ }
76
+ }
77
+
78
+ function complete() {
79
+ return {
80
+ onInit(breadc, allCommands, globalOptions) {
81
+ globalOptions.push(
82
+ makeCompleteOption(breadc, allCommands, globalOptions)
83
+ );
84
+ }
85
+ };
86
+ }
87
+ function makeCompleteOption(breadc$1, allCommands, globalOptions) {
88
+ const command = {
89
+ async callback(result) {
90
+ const shell = detectShellType(result.options["shell"]);
91
+ const text = generate(shell, breadc$1, allCommands, globalOptions);
92
+ console.log(text);
93
+ return text;
94
+ },
95
+ format: "-c, --complete <shell>",
96
+ description: "Export shell complete script",
97
+ _arguments: [],
98
+ _options: [],
99
+ // @ts-ignore
100
+ option: void 0,
101
+ // @ts-ignore
102
+ alias: void 0,
103
+ // @ts-ignore
104
+ action: void 0
105
+ };
106
+ const node = breadc.makeTreeNode({
107
+ command,
108
+ next() {
109
+ return false;
110
+ }
111
+ });
112
+ const option = {
113
+ format: "-c, --complete <shell>",
114
+ name: "complete",
115
+ short: "c",
116
+ type: "string",
117
+ initial: "",
118
+ order: 999999999 - 1,
119
+ description: "Export shell complete script",
120
+ action() {
121
+ return node;
122
+ }
123
+ };
124
+ return option;
125
+ }
126
+ function detectShellType(shell) {
127
+ if (!shell) {
128
+ return "powershell";
129
+ } else {
130
+ return shell;
131
+ }
4
132
  }
5
133
 
6
- module.exports = Complete;
134
+ exports.complete = complete;
135
+ exports.default = complete;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- declare function Complete(): void;
1
+ import { Plugin } from 'breadc';
2
2
 
3
- export { Complete as default };
3
+ declare function complete(): Plugin;
4
+
5
+ export { complete, complete as default };
package/dist/index.mjs CHANGED
@@ -1,4 +1,130 @@
1
- function Complete() {
1
+ import { ParseError, makeTreeNode } from 'breadc';
2
+
3
+ const generatePowershell = (breadc, commands, globalOptions) => {
4
+ const bin = breadc.name;
5
+ const subcommands = generateSubcommands(breadc, commands, globalOptions);
6
+ const template = `using namespace System.Management.Automation
7
+ using namespace System.Management.Automation.Language
8
+ Register-ArgumentCompleter -Native -CommandName '${bin}' -ScriptBlock {
9
+ param($wordToComplete, $commandAst, $cursorPosition)
10
+ $commandElements = $commandAst.CommandElements
11
+ $command = @(
12
+ '${bin}'
13
+ for ($i = 1; $i -lt $commandElements.Count; $i++) {
14
+ $element = $commandElements[$i]
15
+ if ($element -isnot [StringConstantExpressionAst] -or
16
+ $element.StringConstantType -ne [StringConstantType]::BareWord -or
17
+ $element.Value.StartsWith('-') -or
18
+ $element.Value -eq $wordToComplete) {
19
+ break
20
+ }
21
+ $element.Value
22
+ }) -join ';'
23
+ $completions = @(switch ($command) {${subcommands}
24
+ })
25
+ $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
26
+ Sort-Object -Property ListItemText
27
+ }`;
28
+ return template;
29
+ };
30
+ function generateSubcommands(breadc, commands, globalOptions) {
31
+ const cases = [];
32
+ cases.push(
33
+ [
34
+ "",
35
+ `'${breadc.name}' {`,
36
+ ...commands.map(
37
+ (c) => ` [CompletionResult]::new('${c._arguments[0].name}', '${c._arguments[0].name}', [CompletionResultType]::ParameterValue, '${c.description}')`
38
+ ),
39
+ ...globalOptions.map(
40
+ (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')`
41
+ ),
42
+ ` break`,
43
+ `}`
44
+ ].map((t) => " " + t).join("\n")
45
+ );
46
+ for (const command of commands) {
47
+ const args = command._arguments.filter((a) => a.type === "const").map((a) => a.name).join(";");
48
+ cases.push(
49
+ [
50
+ "",
51
+ `'${breadc.name};${args}' {`,
52
+ ...command._options.map(
53
+ (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')`
54
+ ),
55
+ ...globalOptions.map(
56
+ (o) => ` [CompletionResult]::new('--${o.name}', '${o.name}', [CompletionResultType]::ParameterName, '${o.description}')`
57
+ ),
58
+ ` break`,
59
+ `}`
60
+ ].map((t) => " " + t).join("\n")
61
+ );
62
+ }
63
+ return cases.join("\n");
64
+ }
65
+
66
+ function generate(shell, breadc, allCommands, globalOptions) {
67
+ if (shell === "powershell") {
68
+ return generatePowershell(breadc, allCommands, globalOptions);
69
+ } else {
70
+ throw new ParseError(`Unsupport shell ${shell}`);
71
+ }
72
+ }
73
+
74
+ function complete() {
75
+ return {
76
+ onInit(breadc, allCommands, globalOptions) {
77
+ globalOptions.push(
78
+ makeCompleteOption(breadc, allCommands, globalOptions)
79
+ );
80
+ }
81
+ };
82
+ }
83
+ function makeCompleteOption(breadc, allCommands, globalOptions) {
84
+ const command = {
85
+ async callback(result) {
86
+ const shell = detectShellType(result.options["shell"]);
87
+ const text = generate(shell, breadc, allCommands, globalOptions);
88
+ console.log(text);
89
+ return text;
90
+ },
91
+ format: "-c, --complete <shell>",
92
+ description: "Export shell complete script",
93
+ _arguments: [],
94
+ _options: [],
95
+ // @ts-ignore
96
+ option: void 0,
97
+ // @ts-ignore
98
+ alias: void 0,
99
+ // @ts-ignore
100
+ action: void 0
101
+ };
102
+ const node = makeTreeNode({
103
+ command,
104
+ next() {
105
+ return false;
106
+ }
107
+ });
108
+ const option = {
109
+ format: "-c, --complete <shell>",
110
+ name: "complete",
111
+ short: "c",
112
+ type: "string",
113
+ initial: "",
114
+ order: 999999999 - 1,
115
+ description: "Export shell complete script",
116
+ action() {
117
+ return node;
118
+ }
119
+ };
120
+ return option;
121
+ }
122
+ function detectShellType(shell) {
123
+ if (!shell) {
124
+ return "powershell";
125
+ } else {
126
+ return shell;
127
+ }
2
128
  }
3
129
 
4
- export { Complete as default };
130
+ export { complete, complete as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@breadc/complete",
3
- "version": "0.8.3",
3
+ "version": "0.8.5",
4
4
  "description": "Autocompletion generation support for Breadc",
5
5
  "keywords": [
6
6
  "breadc",
@@ -37,12 +37,14 @@
37
37
  "omelette": "^0.4.17"
38
38
  },
39
39
  "devDependencies": {
40
- "@types/node": "^18.11.18",
41
- "vitest": "0.28.3",
42
- "breadc": "0.8.3"
40
+ "@types/node": "^18.11.19",
41
+ "vitest": "0.28.4",
42
+ "@breadc/color": "0.8.5",
43
+ "breadc": "0.8.5"
43
44
  },
44
45
  "peerDependencies": {
45
- "breadc": "0.8.3"
46
+ "@breadc/color": "0.8.5",
47
+ "breadc": "0.8.5"
46
48
  },
47
49
  "scripts": {
48
50
  "build": "unbuild",