@breadc/complete 0.9.7 → 1.0.0-beta.2
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.mts +5 -4
- package/dist/index.mjs +6 -116
- package/package.json +8 -18
- package/dist/index.cjs +0 -124
- package/dist/index.d.cts +0 -5
- package/dist/index.d.ts +0 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ActionMiddleware } from "@breadc/core";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
//#region src/complete.d.ts
|
|
4
|
+
declare function complete(): ActionMiddleware;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { complete };
|
package/dist/index.mjs
CHANGED
|
@@ -1,119 +1,9 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
1
|
+
//#region src/complete.ts
|
|
74
2
|
function complete() {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
makeCompleteOption(breadc, allCommands, globalOptions)
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
function makeCompleteOption(breadc, allCommands, globalOptions) {
|
|
84
|
-
const node = makeTreeNode({
|
|
85
|
-
next() {
|
|
86
|
-
return false;
|
|
87
|
-
},
|
|
88
|
-
finish() {
|
|
89
|
-
return (result) => {
|
|
90
|
-
const shell = detectShellType(result.options["shell"]);
|
|
91
|
-
const text = generate(shell, breadc, allCommands, globalOptions);
|
|
92
|
-
console.log(text);
|
|
93
|
-
return text;
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
const option = {
|
|
98
|
-
format: "-c, --complete <shell>",
|
|
99
|
-
name: "complete",
|
|
100
|
-
short: "c",
|
|
101
|
-
type: "string",
|
|
102
|
-
initial: "",
|
|
103
|
-
order: 999999999 - 1,
|
|
104
|
-
description: "Export shell complete script",
|
|
105
|
-
parse() {
|
|
106
|
-
return node;
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
return option;
|
|
110
|
-
}
|
|
111
|
-
function detectShellType(shell) {
|
|
112
|
-
if (!shell) {
|
|
113
|
-
return "powershell";
|
|
114
|
-
} else {
|
|
115
|
-
return shell;
|
|
116
|
-
}
|
|
3
|
+
return (context, next) => {
|
|
4
|
+
return next();
|
|
5
|
+
};
|
|
117
6
|
}
|
|
118
7
|
|
|
119
|
-
|
|
8
|
+
//#endregion
|
|
9
|
+
export { complete };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@breadc/complete",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
4
|
"description": "Autocompletion generation support for Breadc",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"breadc",
|
|
@@ -24,36 +24,26 @@
|
|
|
24
24
|
"type": "module",
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
27
|
-
"
|
|
28
|
-
"import": "./dist/index.mjs"
|
|
29
|
-
"types": "./dist/index.d.ts"
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
|
+
"import": "./dist/index.mjs"
|
|
30
29
|
}
|
|
31
30
|
},
|
|
32
|
-
"main": "dist/index.cjs",
|
|
33
31
|
"module": "dist/index.mjs",
|
|
34
|
-
"types": "dist/index.d.
|
|
32
|
+
"types": "dist/index.d.mts",
|
|
35
33
|
"files": [
|
|
36
34
|
"dist"
|
|
37
35
|
],
|
|
38
36
|
"dependencies": {
|
|
39
37
|
"omelette": "^0.4.17"
|
|
40
38
|
},
|
|
41
|
-
"devDependencies": {
|
|
42
|
-
"@types/node": "^20.8.7",
|
|
43
|
-
"typescript": "^5.2.2",
|
|
44
|
-
"vitest": "0.34.6",
|
|
45
|
-
"@breadc/color": "0.9.7",
|
|
46
|
-
"breadc": "0.9.7"
|
|
47
|
-
},
|
|
48
39
|
"peerDependencies": {
|
|
49
|
-
"breadc": "0.
|
|
50
|
-
"@breadc/
|
|
40
|
+
"@breadc/color": "1.0.0-beta.2",
|
|
41
|
+
"@breadc/core": "1.0.0-beta.2"
|
|
51
42
|
},
|
|
52
43
|
"scripts": {
|
|
53
|
-
"build": "
|
|
54
|
-
"format": "prettier --write src/**/*.ts test/*.ts",
|
|
55
|
-
"test": "vitest",
|
|
44
|
+
"build": "tsdown",
|
|
56
45
|
"test:ci": "vitest --run",
|
|
46
|
+
"test": "vitest",
|
|
57
47
|
"typecheck": "tsc --noEmit"
|
|
58
48
|
}
|
|
59
49
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
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 node = breadc.makeTreeNode({
|
|
89
|
-
next() {
|
|
90
|
-
return false;
|
|
91
|
-
},
|
|
92
|
-
finish() {
|
|
93
|
-
return (result) => {
|
|
94
|
-
const shell = detectShellType(result.options["shell"]);
|
|
95
|
-
const text = generate(shell, breadc$1, allCommands, globalOptions);
|
|
96
|
-
console.log(text);
|
|
97
|
-
return text;
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
const option = {
|
|
102
|
-
format: "-c, --complete <shell>",
|
|
103
|
-
name: "complete",
|
|
104
|
-
short: "c",
|
|
105
|
-
type: "string",
|
|
106
|
-
initial: "",
|
|
107
|
-
order: 999999999 - 1,
|
|
108
|
-
description: "Export shell complete script",
|
|
109
|
-
parse() {
|
|
110
|
-
return node;
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
return option;
|
|
114
|
-
}
|
|
115
|
-
function detectShellType(shell) {
|
|
116
|
-
if (!shell) {
|
|
117
|
-
return "powershell";
|
|
118
|
-
} else {
|
|
119
|
-
return shell;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
exports.complete = complete;
|
|
124
|
-
exports.default = complete;
|
package/dist/index.d.cts
DELETED