/
bin-console.ts
95 lines (88 loc) · 2.34 KB
/
bin-console.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
interface BinConsoleJSON {
commands: {
name: string;
description: string;
hidden: boolean;
help: string;
definition: {
arguments:
| []
| Record<
string,
{
name: string;
is_required: boolean;
description: string;
is_array: boolean;
}
>;
options:
| []
| Record<
string,
{
name: string;
accept_value: boolean;
shortcut: string;
is_value_required: boolean;
is_multiple: boolean;
description: string;
}
>;
};
}[];
}
const completionSpec: Fig.Spec = {
name: "bin-console",
description: "Symfony bin/console command",
generateSpec: async (_, executeShellCommand) => {
const { stdout } = await executeShellCommand({
command: "php",
args: ["bin/console", "list", "--format=json"],
});
let subcommands = [];
try {
const commandDefinitions = JSON.parse(stdout) as BinConsoleJSON;
subcommands = commandDefinitions.commands.map((command) => ({
name: command.name,
description: command.description,
icon: "https://cdn.iconscout.com/icon/free/png-128/symfony-282493.png",
hidden: command.hidden || false,
args: Object.values(command.definition.arguments).map((argument) => {
return {
name: argument.name,
description: argument.description,
isOptional: !argument.is_required,
};
}),
options: Object.values(command.definition.options).map((option) => {
const name = [option.name];
if (option.shortcut !== "") {
name.push(option.shortcut);
}
const args: Fig.Arg[] = [];
if (option.accept_value) {
args.push({
name: "arg",
isVariadic: option.is_multiple,
isOptional: !option.is_value_required,
});
}
return {
name,
args,
description: option.description,
};
}),
}));
} catch (err) {
// ignore
console.error(err);
}
return {
name: "bin-console",
subcommands,
};
},
};
export default completionSpec;