Skip to content

Commit d0777ad

Browse files
aFuzzyBearNate Moore
and
Nate Moore
authored
Astro add docs (#2958)
* So This works 😎 * need to add to the cli next * Renamed Files and Export Applied creditation to where I found the 'inspiration' for this application. * applied `astro docs` to cli * Trying to add to CLI, Not working 🤷‍♂️ * Converted into async method, * 🎆🎆 It works!!! 🥳🎉🥳 Embarrasing as it is I totally missed the part where logic was to be in. * Moved `docs` cmd to `supportedCommands` * refactor: cleanup docs command * chore: add changeset * chore: rename browser to open Co-authored-by: Nate Moore <[email protected]>
1 parent b5ed099 commit d0777ad

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

.changeset/strange-avocados-double.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Add `astro docs` command which opens the Astro docs in your preferred browser.

packages/astro/src/cli/index.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import add from '../core/add/index.js';
1212
import devServer from '../core/dev/index.js';
1313
import preview from '../core/preview/index.js';
1414
import { check } from './check.js';
15+
import { openInBrowser } from './open.js';
1516
import { loadConfig } from '../core/config.js';
1617
import { printHelp, formatErrorMessage, formatConfigErrorMessage } from '../core/messages.js';
1718
import { createSafeError } from '../core/util.js';
1819

1920
type Arguments = yargs.Arguments;
20-
type CLICommand = 'help' | 'version' | 'add' | 'dev' | 'build' | 'preview' | 'reload' | 'check';
21+
type CLICommand = 'help' | 'version' | 'add' | 'docs' | 'dev' | 'build' | 'preview' | 'reload' | 'check';
2122

2223
/** Display --help flag */
2324
function printAstroHelp() {
@@ -26,6 +27,7 @@ function printAstroHelp() {
2627
headline: 'Futuristic web development tool.',
2728
commands: [
2829
['add', 'Add an integration to your configuration.'],
30+
['docs', 'Launch Astro\'s Doc site directly from the terminal. '],
2931
['dev', 'Run Astro in development mode.'],
3032
['build', 'Build a pre-compiled production-ready site.'],
3133
['preview', 'Preview your build locally before deploying.'],
@@ -58,11 +60,10 @@ async function printVersion() {
5860
function resolveCommand(flags: Arguments): CLICommand {
5961
const cmd = flags._[2] as string;
6062
if (cmd === 'add') return 'add';
61-
6263
if (flags.version) return 'version';
6364
else if (flags.help) return 'help';
6465

65-
const supportedCommands = new Set(['dev', 'build', 'preview', 'check']);
66+
const supportedCommands = new Set(['dev', 'build', 'preview', 'check', 'docs']);
6667
if (supportedCommands.has(cmd)) {
6768
return cmd as CLICommand;
6869
}
@@ -145,6 +146,14 @@ export async function cli(args: string[]) {
145146
}
146147
}
147148

149+
case 'docs': {
150+
try {
151+
return await openInBrowser('https://docs.astro.build/')
152+
} catch (err) {
153+
return throwAndExit(err)
154+
}
155+
}
156+
148157
default: {
149158
throw new Error(`Error running ${cmd}`);
150159
}

packages/astro/src/cli/open.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { ExecaChildProcess } from 'execa';
2+
import { execa } from 'execa';
3+
4+
/**
5+
* Credit: Azhar22
6+
* @see https://github.com/azhar22k/ourl/blob/master/index.js
7+
*/
8+
const getPlatformSpecificCommand = (): [string]|[string, string[]] => {
9+
const isGitPod = Boolean(process.env.GITPOD_REPO_ROOT);
10+
const platform = isGitPod ? 'gitpod' : process.platform;
11+
12+
switch (platform) {
13+
case 'android':
14+
case 'linux':
15+
return ['xdg-open'];
16+
case 'darwin':
17+
return ['open'];
18+
case 'win32':
19+
return ['cmd', ['/c', 'start']];
20+
case 'gitpod':
21+
return ['/ide/bin/remote-cli/gitpod-code', ['--openExternal']];
22+
default:
23+
throw new Error(
24+
`It looks like your platform ("${platform}") isn't supported!\nTo view Astro's docs, please visit https://docs.astro.build`
25+
);
26+
}
27+
};
28+
29+
export async function openInBrowser(url: string): Promise<ExecaChildProcess> {
30+
const [command, args = []] = getPlatformSpecificCommand();
31+
return execa(command, [...args, encodeURI(url)]);
32+
};

0 commit comments

Comments
 (0)