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 <nate@skypack.dev>
This commit is contained in:
parent
b5ed099eaf
commit
d0777ad3af
3 changed files with 49 additions and 3 deletions
5
.changeset/strange-avocados-double.md
Normal file
5
.changeset/strange-avocados-double.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add `astro docs` command which opens the Astro docs in your preferred browser.
|
|
@ -12,12 +12,13 @@ import add from '../core/add/index.js';
|
||||||
import devServer from '../core/dev/index.js';
|
import devServer from '../core/dev/index.js';
|
||||||
import preview from '../core/preview/index.js';
|
import preview from '../core/preview/index.js';
|
||||||
import { check } from './check.js';
|
import { check } from './check.js';
|
||||||
|
import { openInBrowser } from './open.js';
|
||||||
import { loadConfig } from '../core/config.js';
|
import { loadConfig } from '../core/config.js';
|
||||||
import { printHelp, formatErrorMessage, formatConfigErrorMessage } from '../core/messages.js';
|
import { printHelp, formatErrorMessage, formatConfigErrorMessage } from '../core/messages.js';
|
||||||
import { createSafeError } from '../core/util.js';
|
import { createSafeError } from '../core/util.js';
|
||||||
|
|
||||||
type Arguments = yargs.Arguments;
|
type Arguments = yargs.Arguments;
|
||||||
type CLICommand = 'help' | 'version' | 'add' | 'dev' | 'build' | 'preview' | 'reload' | 'check';
|
type CLICommand = 'help' | 'version' | 'add' | 'docs' | 'dev' | 'build' | 'preview' | 'reload' | 'check';
|
||||||
|
|
||||||
/** Display --help flag */
|
/** Display --help flag */
|
||||||
function printAstroHelp() {
|
function printAstroHelp() {
|
||||||
|
@ -26,6 +27,7 @@ function printAstroHelp() {
|
||||||
headline: 'Futuristic web development tool.',
|
headline: 'Futuristic web development tool.',
|
||||||
commands: [
|
commands: [
|
||||||
['add', 'Add an integration to your configuration.'],
|
['add', 'Add an integration to your configuration.'],
|
||||||
|
['docs', 'Launch Astro\'s Doc site directly from the terminal. '],
|
||||||
['dev', 'Run Astro in development mode.'],
|
['dev', 'Run Astro in development mode.'],
|
||||||
['build', 'Build a pre-compiled production-ready site.'],
|
['build', 'Build a pre-compiled production-ready site.'],
|
||||||
['preview', 'Preview your build locally before deploying.'],
|
['preview', 'Preview your build locally before deploying.'],
|
||||||
|
@ -58,11 +60,10 @@ async function printVersion() {
|
||||||
function resolveCommand(flags: Arguments): CLICommand {
|
function resolveCommand(flags: Arguments): CLICommand {
|
||||||
const cmd = flags._[2] as string;
|
const cmd = flags._[2] as string;
|
||||||
if (cmd === 'add') return 'add';
|
if (cmd === 'add') return 'add';
|
||||||
|
|
||||||
if (flags.version) return 'version';
|
if (flags.version) return 'version';
|
||||||
else if (flags.help) return 'help';
|
else if (flags.help) return 'help';
|
||||||
|
|
||||||
const supportedCommands = new Set(['dev', 'build', 'preview', 'check']);
|
const supportedCommands = new Set(['dev', 'build', 'preview', 'check', 'docs']);
|
||||||
if (supportedCommands.has(cmd)) {
|
if (supportedCommands.has(cmd)) {
|
||||||
return cmd as CLICommand;
|
return cmd as CLICommand;
|
||||||
}
|
}
|
||||||
|
@ -145,6 +146,14 @@ export async function cli(args: string[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 'docs': {
|
||||||
|
try {
|
||||||
|
return await openInBrowser('https://docs.astro.build/')
|
||||||
|
} catch (err) {
|
||||||
|
return throwAndExit(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
throw new Error(`Error running ${cmd}`);
|
throw new Error(`Error running ${cmd}`);
|
||||||
}
|
}
|
||||||
|
|
32
packages/astro/src/cli/open.ts
Normal file
32
packages/astro/src/cli/open.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import type { ExecaChildProcess } from 'execa';
|
||||||
|
import { execa } from 'execa';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Credit: Azhar22
|
||||||
|
* @see https://github.com/azhar22k/ourl/blob/master/index.js
|
||||||
|
*/
|
||||||
|
const getPlatformSpecificCommand = (): [string]|[string, string[]] => {
|
||||||
|
const isGitPod = Boolean(process.env.GITPOD_REPO_ROOT);
|
||||||
|
const platform = isGitPod ? 'gitpod' : process.platform;
|
||||||
|
|
||||||
|
switch (platform) {
|
||||||
|
case 'android':
|
||||||
|
case 'linux':
|
||||||
|
return ['xdg-open'];
|
||||||
|
case 'darwin':
|
||||||
|
return ['open'];
|
||||||
|
case 'win32':
|
||||||
|
return ['cmd', ['/c', 'start']];
|
||||||
|
case 'gitpod':
|
||||||
|
return ['/ide/bin/remote-cli/gitpod-code', ['--openExternal']];
|
||||||
|
default:
|
||||||
|
throw new Error(
|
||||||
|
`It looks like your platform ("${platform}") isn't supported!\nTo view Astro's docs, please visit https://docs.astro.build`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function openInBrowser(url: string): Promise<ExecaChildProcess> {
|
||||||
|
const [command, args = []] = getPlatformSpecificCommand();
|
||||||
|
return execa(command, [...args, encodeURI(url)]);
|
||||||
|
};
|
Loading…
Add table
Reference in a new issue