|
| 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