-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathcommands.build.ts
More file actions
158 lines (131 loc) · 5.23 KB
/
Copy pathcommands.build.ts
File metadata and controls
158 lines (131 loc) · 5.23 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { readFile, writeFile } from "fs/promises";
import { parseDocument as parseYamlDocument, Scalar, stringify as stringifyYaml, YAMLMap, YAMLSeq } from "yaml";
import { Builder } from "../../../meta";
/**
* Keeps `commands.yaml` in sync with the commands defined in each file.
*/
export async function build(builder: Builder) {
const [commandsDocSource, commandModules] = await Promise.all([
readFile(`${__dirname}/commands.yaml`, { encoding: "utf-8" }),
builder.getCommandModules(),
]);
const commandsDoc = parseYamlDocument(commandsDocSource),
commandDocByName = Object.fromEntries(
(commandsDoc.contents as YAMLMap<Scalar<string>, YAMLMap>).items.map((item) =>
[item.key.value, item.value!])),
toRemove = new Set(Object.keys(commandDocByName));
let anonymousCommandsDoc = commandsDoc.get("anonymous") as YAMLSeq<YAMLMap> | undefined;
if (anonymousCommandsDoc === undefined) {
anonymousCommandsDoc = new YAMLSeq();
} else {
// Re-insert at the end.
commandsDoc.delete("anonymous");
}
const anonymousCommandByCommands =
Object.fromEntries(anonymousCommandsDoc.items.map((item) =>
[item.get("commands", false) as string, item])),
anonymousToRemove = Object.fromEntries(
anonymousCommandsDoc.items.map((item, i) => [item.get("commands", false) as string, i]));
// Add all commands.
const allCommands = commandModules.flatMap((module) => [
...(module.additional as (Builder.AdditionalCommand & { doc: undefined })[]),
...module.functions.flatMap((f) => [
...(f.additional as (Builder.AdditionalCommand & { doc: undefined })[]),
{
qualifiedIdentifier: f.qualifiedName,
keys: f.properties["keys"],
title: f.summary,
commands: undefined,
doc: f.doc,
},
]),
]);
for (const command of allCommands) {
const commandId = command.qualifiedIdentifier,
keys = command.keys?.replace(/\), /g, ")\n"),
existingCommandDoc = commandId !== undefined
? commandDocByName[commandId]
: anonymousCommandByCommands[command.commands!];
if (existingCommandDoc === undefined) {
const value = {} as Record<string, {}>;
if (command.title !== undefined) {
value["title"] = { en: command.title };
}
if (command.commands !== undefined) {
value["commands"] = literalString(command.commands!);
}
if (keys !== undefined) {
value["keys"] = { qwerty: literalString(keys) };
}
if (command.doc !== undefined) {
value["doc"] = { en: literalString(command.doc) };
}
if (commandId !== undefined) {
commandsDoc.add(commandsDoc.createPair(commandId, value));
} else {
anonymousCommandsDoc.add(commandsDoc.createNode(value));
}
continue;
}
if (commandId !== undefined) {
toRemove.delete(commandId);
} else {
delete anonymousToRemove[command.commands!];
}
if (command.title !== existingCommandDoc.getIn(["title", "en"], false) as string) {
if (command.title === undefined) {
existingCommandDoc.delete("title");
} else {
existingCommandDoc.setIn(["title", "en"], command.title);
}
}
if (keys !== existingCommandDoc.getIn(["keys", "qwerty"], false) as string) {
if (keys === undefined) {
existingCommandDoc.delete("keys");
} else {
existingCommandDoc.setIn(["keys", "qwerty"], literalString(keys));
}
}
if (command.commands !== existingCommandDoc.get("commands", false) as string) {
if (command.commands === undefined) {
existingCommandDoc.delete("commands");
} else {
existingCommandDoc.set("commands", literalString(command.commands));
}
}
if (command.doc !== existingCommandDoc.get("doc", false) as string) {
if (command.doc === undefined) {
existingCommandDoc.delete("doc");
} else {
existingCommandDoc.setIn(["doc", "en"], literalString(command.doc));
}
}
}
// Delete old commands.
for (const commandId of toRemove) {
commandsDoc.delete(commandId);
}
// Delete old anonymous commands. Since indices are sorted in
// `anonymousToRemove`, we can start deletion from the end and avoid
// recomputing indices.
for (const commandsIndex of Object.values(anonymousToRemove).reverse()) {
anonymousCommandsDoc.delete(commandsIndex);
}
// Sort commands by name.
(commandsDoc.contents as YAMLMap.Parsed<Scalar.Parsed>).items
.sort((a, b) => (a.key.value as string).localeCompare(b.key.value as string));
// Sort anonymous commands by `commands` and `args`.
anonymousCommandsDoc.items
.sort((a, b) => (a.get("commands", false) as string).localeCompare(b.get("commands", false) as string));
// Add anonymous commands at the end.
commandsDoc.addIn(["anonymous"], anonymousCommandsDoc);
// Normalize whitespace.
const contents = stringifyYaml(commandsDoc, { lineWidth: 100 })
.replace(/(?<!\n)\n([.\w]+|(?: {2})+(?:- |keys:|commands:|doc:))/g, "\n\n$1");
if (commandsDocSource !== contents) {
await writeFile(`${__dirname}/commands.yaml`, contents);
}
}
function literalString(s: string) {
return Object.assign(new Scalar(s), { type: Scalar.BLOCK_LITERAL });
}