Skip to content

Commit c4fc56e

Browse files
committed
Generates command types from contributions.json
1 parent e5e5ed6 commit c4fc56e

File tree

9 files changed

+1006
-548
lines changed

9 files changed

+1006
-548
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20099,6 +20099,8 @@
2009920099
"extract:contributions": "node --experimental-strip-types ./scripts/generateContributions.mts --extract",
2010020100
"// Generates contributions in contributions.json into package.json": "//",
2010120101
"generate:contributions": "node --experimental-strip-types ./scripts/generateContributions.mts",
20102+
"// Generates command types from contributions.json into src/constants.commands.generated.ts": "//",
20103+
"generate:commandTypes": "node --experimental-strip-types ./scripts/generateCommandTypes.mts",
2010220104
"// Generates docs/telemetry-events.md": "//",
2010320105
"generate:docs:telemetry": "node ./scripts/generateTelemetryDocs.mjs",
2010420106
"generate:emoji": "node ./scripts/generateEmojiShortcodeMap.mjs",

scripts/generateCommandTypes.mts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { exec } from 'child_process';
2+
import type { ContributionsJson } from './contributions/models';
3+
import { readFileSync, writeFileSync } from 'fs';
4+
import * as path from 'path';
5+
import { fileURLToPath } from 'url';
6+
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = path.join(path.dirname(__filename), '..');
9+
const relativeFilename = path.relative(__dirname, __filename).replace(/\\/g, '/');
10+
11+
/** Generates command types from the commands in `contributions.json` */
12+
function generateCommandTypesFromContributions(): void {
13+
console.log("Generating command types into 'constants.commands.generated.ts' from contributions.json...");
14+
15+
const contributions: ContributionsJson = JSON.parse(
16+
readFileSync(path.join(__dirname, 'contributions.json'), 'utf8'),
17+
);
18+
19+
const commands: string[] = [];
20+
const kbCommands: string[] = [];
21+
const paletteCommands: string[] = [];
22+
23+
for (const [id, command] of Object.entries(contributions.commands)) {
24+
if (command.commandPalette) {
25+
paletteCommands.push(id);
26+
} else {
27+
commands.push(id);
28+
}
29+
}
30+
31+
for (const kb of contributions.keybindings) {
32+
kbCommands.push(kb.command);
33+
}
34+
35+
const contents = `// This file is generated by (vscode-gitlens)/${relativeFilename}
36+
// Do not edit this file directly
37+
38+
export type ContributedCommands = ContributedKeybindingCommands | ContributedPaletteCommands | ${commands
39+
.sort()
40+
.map(c => `'${c}'`)
41+
.join(' | ')};
42+
43+
export type ContributedPaletteCommands = ${paletteCommands
44+
.sort()
45+
.map(c => `'${c}'`)
46+
.join(' | ')};
47+
48+
export type ContributedKeybindingCommands = ${kbCommands
49+
.filter(c => c.startsWith('gitlens.'))
50+
.sort()
51+
.map(c => `'${c}'`)
52+
.join(' | ')};
53+
54+
`;
55+
56+
const file = path.join(__dirname, 'src', 'constants.commands.generated.ts');
57+
writeFileSync(file, contents, 'utf8');
58+
// run prettier on the generated file
59+
exec(`pnpm prettier --write ${file}`);
60+
61+
console.log("Generated 'constants.commands.generated.ts' from contributions.json");
62+
}
63+
64+
generateCommandTypesFromContributions();

0 commit comments

Comments
 (0)