-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen_wrapper.js
68 lines (56 loc) · 2.42 KB
/
gen_wrapper.js
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
'use strict';
const genWrapperUtil = require('./gen_wrapper_util');
const defaultKeybindingsLoader = require('./default_keybindings_loader');
const PackageJsonPath = './package.json';
const ConfigPath = 'generator/config.json';
const error = genWrapperUtil.error;
const warn = genWrapperUtil.warn;
function checkExclusion(exclusion, commands) {
for (const command of exclusion) {
if (!commands.has(command)) {
warn('No matching command:', command);
}
}
}
function checkAwaitOptions(awaitOptions) {
for (const awaitOption of awaitOptions.values()) {
if (!genWrapperUtil.isValidAwaitOption(awaitOption)) {
error('Invalid awaitOption found:', awaitOption);
process.exit(1);
}
}
}
async function main() {
const packageJson = await genWrapperUtil.readJSON(PackageJsonPath);
const config = await genWrapperUtil.readJSON(ConfigPath);
const exclusion = new Set(config['exclusion'] || []);
const awaitOptions = new Map(config['awaitOptions'] || []);
checkAwaitOptions(awaitOptions);
const baseKeybindings = await defaultKeybindingsLoader.loadBaseKeybindings(config['baseKeybindings'] || []);
const commands = new Set(baseKeybindings.flatMap(item => item.keybindings).map(keybinding => keybinding.command));
checkExclusion(exclusion, commands);
// combine the three sets of default keybindings of VS Code for Windows, Linux, and macOS.
const combined = defaultKeybindingsLoader.combineBaseKeybingings(baseKeybindings);
const wrappers = combined.flatMap(
keybinding => {
if (exclusion.has(keybinding.command) || keybinding.command === '') {
// make a keybinding of a direct call for the excluded command
keybinding.when = genWrapperUtil.makeWrapperWhen(keybinding);
return [ keybinding ];
} else {
// make a wrapper keybinding (indirect call) to enable recording of the command
const awaitOption = awaitOptions.get(keybinding.command) || '';
const wrappers = genWrapperUtil.makeWrapper(keybinding, awaitOption);
return wrappers;
}
}
);
packageJson['contributes']['keybindings'] = (
[]
.concat(config['header'] || [])
.concat(wrappers)
.concat(config['footer'] || [])
);
await genWrapperUtil.writeJSON(PackageJsonPath, packageJson);
}
main();