|
| 1 | +import type { Logger } from '../../../core/logger/core.js'; |
| 2 | +import { defineCommand } from '../../domain/command.js'; |
| 3 | +import type { CommandExecutor, PlatformProvider } from '../definitions.js'; |
| 4 | +import type { Platform } from '../domains/platform.js'; |
| 5 | + |
| 6 | +interface Options { |
| 7 | + url: string; |
| 8 | + platformProvider: PlatformProvider; |
| 9 | + logger: Logger; |
| 10 | + commandExecutor: CommandExecutor; |
| 11 | +} |
| 12 | + |
| 13 | +function getExecInputForPlatform( |
| 14 | + platform: Platform, |
| 15 | +): [command: string, args?: Array<string>] | null { |
| 16 | + switch (platform) { |
| 17 | + case 'android': |
| 18 | + case 'linux': |
| 19 | + return ['xdg-open']; |
| 20 | + case 'darwin': |
| 21 | + return ['open']; |
| 22 | + case 'win32': |
| 23 | + return ['cmd', ['/c', 'start']]; |
| 24 | + case 'gitpod': |
| 25 | + return ['/ide/bin/remote-cli/gitpod-code', ['--openExternal']]; |
| 26 | + case 'aix': |
| 27 | + case 'freebsd': |
| 28 | + case 'haiku': |
| 29 | + case 'openbsd': |
| 30 | + case 'sunos': |
| 31 | + case 'cygwin': |
| 32 | + case 'netbsd': |
| 33 | + default: |
| 34 | + return null; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export const openDocsCommand = defineCommand({ |
| 39 | + help: { |
| 40 | + commandName: 'astro docs', |
| 41 | + tables: { |
| 42 | + Flags: [['--help (-h)', 'See all available flags.']], |
| 43 | + }, |
| 44 | + description: `Launches the Astro Docs website directly from the terminal.`, |
| 45 | + }, |
| 46 | + async run({ url, platformProvider, logger, commandExecutor }: Options) { |
| 47 | + const platform = platformProvider.get(); |
| 48 | + const input = getExecInputForPlatform(platform); |
| 49 | + if (!input) { |
| 50 | + logger.error( |
| 51 | + 'SKIP_FORMAT', |
| 52 | + `It looks like your platform ("${platform}") isn't supported!\nTo view Astro's docs, please visit ${url}`, |
| 53 | + ); |
| 54 | + return; |
| 55 | + } |
| 56 | + const [command, args = []] = input; |
| 57 | + await commandExecutor.execute(command, [...args, encodeURI(url)]); |
| 58 | + }, |
| 59 | +}); |
0 commit comments