-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathmagento.ts
99 lines (93 loc) · 2.76 KB
/
magento.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
96
97
98
99
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;
default: unknown;
}
>;
options:
| []
| Record<
string,
{
name: string;
accept_value: boolean;
shortcut: string;
is_value_required: boolean;
is_multiple: boolean;
description: string;
default: unknown;
}
>;
};
}[];
}
const getEnvConfig = async (executeShellCommand) => {
const command = "php -r 'print(json_encode(require \"app/etc/env.php\"));'";
const out = await executeShellCommand(command);
return JSON.parse(out);
};
const getCacheTypes = async (executeShellCommand) => {
const env = await getEnvConfig(executeShellCommand);
return Object.keys(env.cache_types);
};
const completionSpec: Fig.Spec = {
name: "magento",
description: "Open-source E-commerce",
generateSpec: async (tokens, executeShellCommand) => {
const { stdout } = await executeShellCommand({
command: "bin/magento",
args: ["list", "--format=json", "--raw"],
});
const magento = JSON.parse(stdout) as BinConsoleJSON;
const cacheTypes = await getCacheTypes(executeShellCommand);
return {
name: "magento",
description: "Open-source E-commerce",
subcommands: magento.commands.map((command) => {
return {
name: command.name,
description: command.description,
args: Object.values(command.definition.arguments).map((argument) => {
const suggestions = [];
if (
command.name.startsWith("cache:") &&
argument.name === "types"
) {
suggestions.push(...cacheTypes);
}
return {
name: argument.name,
description: argument.description,
isOptional: !argument.is_required,
default: argument.default?.toString() ?? "",
isVariadic: argument.is_array,
suggestions,
};
}),
options: Object.values(command.definition.options).map((option) => {
return {
name: [option.name, ...(option.shortcut.split("|") || [])],
description: option.description,
isRequired: option.is_value_required,
requiresEquals: option.accept_value,
};
}),
};
}),
};
},
};
export default completionSpec;