File size: 4,905 Bytes
c44600e | 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { readFile, writeFile } from 'node:fs/promises';
import type { KeyBinding } from '../packages/cli/src/ui/key/keyBindings.js';
import {
commandCategories,
commandDescriptions,
defaultKeyBindingConfig,
Command,
getPlatformUndoBindings,
getPlatformRedoBindings,
} from '../packages/cli/src/ui/key/keyBindings.js';
import {
formatWithPrettier,
injectBetweenMarkers,
normalizeForCompare,
} from './utils/autogen.js';
const START_MARKER = '<!-- KEYBINDINGS-AUTOGEN:START -->';
const END_MARKER = '<!-- KEYBINDINGS-AUTOGEN:END -->';
const OUTPUT_RELATIVE_PATH = ['docs', 'reference', 'keyboard-shortcuts.md'];
import { formatKeyBinding } from '../packages/cli/src/ui/key/keybindingUtils.js';
export interface KeybindingDocCommand {
command: string;
description: string;
bindings: readonly KeyBinding[];
}
export interface KeybindingDocSection {
title: string;
commands: readonly KeybindingDocCommand[];
}
export async function main(argv = process.argv.slice(2)) {
const checkOnly = argv.includes('--check');
const repoRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'..',
);
const docPath = path.join(repoRoot, ...OUTPUT_RELATIVE_PATH);
const sections = buildDefaultDocSections();
const generatedBlock = renderDocumentation(sections);
const currentDoc = await readFile(docPath, 'utf8');
const injectedDoc = injectBetweenMarkers({
document: currentDoc,
startMarker: START_MARKER,
endMarker: END_MARKER,
newContent: generatedBlock,
paddingBefore: '\n\n',
paddingAfter: '\n',
});
const updatedDoc = await formatWithPrettier(injectedDoc, docPath);
if (normalizeForCompare(updatedDoc) === normalizeForCompare(currentDoc)) {
if (!checkOnly) {
console.log('Keybinding documentation already up to date.');
}
return;
}
if (checkOnly) {
console.error(
'Keybinding documentation is out of date. Run `npm run docs:keybindings` to regenerate.',
);
process.exitCode = 1;
return;
}
await writeFile(docPath, updatedDoc, 'utf8');
console.log('Keybinding documentation regenerated.');
}
export function buildDefaultDocSections(): readonly KeybindingDocSection[] {
return commandCategories.map((category) => ({
title: category.title,
commands: category.commands.map((command) => {
// For UNDO and REDO, we want to show all platform variants in the docs
if (command === Command.UNDO) {
return {
command: command,
description: commandDescriptions[command],
bindings: getMergedPlatformBindings(getPlatformUndoBindings),
};
}
if (command === Command.REDO) {
return {
command: command,
description: commandDescriptions[command],
bindings: getMergedPlatformBindings(getPlatformRedoBindings),
};
}
return {
command: command,
description: commandDescriptions[command],
bindings: defaultKeyBindingConfig.get(command) ?? [],
};
}),
}));
}
function getMergedPlatformBindings(
getBindings: (platform: string) => readonly KeyBinding[],
): readonly KeyBinding[] {
const win32 = getBindings('win32');
const darwin = getBindings('darwin');
const linux = getBindings('linux');
const all = [...win32, ...darwin, ...linux];
const seen = new Set<string>();
const unique: KeyBinding[] = [];
for (const b of all) {
const key = `${b.name}-${b.ctrl}-${b.shift}-${b.alt}-${b.cmd}`;
if (!seen.has(key)) {
seen.add(key);
unique.push(b);
}
}
return unique;
}
export function renderDocumentation(
sections: readonly KeybindingDocSection[],
): string {
const renderedSections = sections.map((section) => {
const rows = section.commands.map((command) => {
const formattedBindings = formatBindings(command.bindings);
const keysCell = formattedBindings.join('<br />');
return `| \`${command.command}\` | ${command.description} | ${keysCell} |`;
});
return [
`#### ${section.title}`,
'',
'| Command | Action | Keys |',
'| --- | --- | --- |',
...rows,
].join('\n');
});
return renderedSections.join('\n\n');
}
function formatBindings(bindings: readonly KeyBinding[]): string[] {
const seen = new Set<string>();
const results: string[] = [];
for (const binding of bindings) {
const label = formatKeyBinding(binding, 'default');
if (label && !seen.has(label)) {
seen.add(label);
results.push(`\`${label}\``);
}
}
return results;
}
if (process.argv[1]) {
const entryUrl = pathToFileURL(path.resolve(process.argv[1])).href;
if (entryUrl === import.meta.url) {
await main();
}
}
|