-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathdrush.ts
101 lines (89 loc) · 2.65 KB
/
drush.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
100
101
interface DrushArgument {
name: string;
is_required: boolean;
is_array: boolean;
description: string;
default: null | string | Array<string>;
}
interface DrushOption {
name: string;
shortcut: string;
accept_value: boolean;
is_value_required: boolean;
is_multiple: boolean; // isRepeatable in fig
description: string;
default: null | boolean; // not supported by fig
}
interface DrushCommandDefinition {
arguments: Record<string, DrushArgument>;
options: Record<string, DrushOption>;
}
interface DrushCommand {
name: string;
usage: string[];
description: string;
help: string;
definition: DrushCommandDefinition;
}
interface DrushListOutput {
commands: DrushCommand[];
}
const completionSpec: Fig.Spec = {
name: "drush",
description:
"Drush is a command line shell and Unix scripting interface for Drupal",
generateSpec: async (tokens, executeShellCommand) => {
const { stdout: jsonList } = await executeShellCommand({
command: "drush",
// eslint-disable-next-line @withfig/fig-linter/no-useless-arrays
args: ["--format=json"],
});
const subcommands: Fig.Subcommand[] = [];
try {
const data: DrushListOutput = JSON.parse(jsonList);
for (const command of data.commands) {
subcommands.push({
name: command.name,
description: command.description,
args: Object.keys(command.definition.arguments).map((argKey) => {
const arg = command.definition.arguments[argKey];
const argDefault = arg.default
? Array.isArray(arg.default)
? arg.default[0]
: arg.default
: undefined;
return {
name: arg.name,
description: arg.description,
isOptional: !arg.is_required,
default: argDefault,
isVariadic: arg.is_array,
};
}),
options: Object.keys(command.definition.options).map((optionKey) => {
const option = command.definition.options[optionKey];
const names = [option.name];
const shortCut = option.shortcut;
if (shortCut.trim().length > 0) {
names.push(shortCut);
}
return {
name: names,
description: option.description,
isRequired: option.is_value_required,
args: option.accept_value ? {} : undefined,
isRepeatable: option.is_multiple,
};
}),
});
}
} catch (err) {
console.error(err);
}
return {
name: "drush",
subcommands,
};
},
};
export default completionSpec;