Skip to content

Commit cba8cc1

Browse files
refactor(cli): docs (#14602)
1 parent bed4199 commit cba8cc1

File tree

13 files changed

+264
-65
lines changed

13 files changed

+264
-65
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { Platform } from './domains/platform.js';
2+
3+
export interface PlatformProvider {
4+
get: () => Platform;
5+
}
6+
7+
export interface CommandExecutor {
8+
execute: (
9+
command: string,
10+
args?: Array<string>,
11+
options?: {
12+
cwd?: string;
13+
env: Record<string, string | undefined>;
14+
},
15+
) => Promise<{ stdout: string }>;
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Platform = NodeJS.Platform | 'gitpod';

packages/astro/src/cli/docs/index.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { PlatformProvider } from '../definitions.js';
2+
3+
export function createProcessPlatformProvider(): PlatformProvider {
4+
const platform = Boolean(process.env.GITPOD_REPO_ROOT) ? 'gitpod' : process.platform;
5+
return {
6+
get() {
7+
return platform;
8+
},
9+
};
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { NonZeroExitError, x } from 'tinyexec';
2+
import type { CommandExecutor } from '../definitions.js';
3+
4+
export function createTinyexecCommandExecutor(): CommandExecutor {
5+
return {
6+
async execute(command, args, options) {
7+
return await x(command, args, {
8+
throwOnError: true,
9+
nodeOptions: {
10+
cwd: options?.cwd,
11+
env: options?.env,
12+
},
13+
}).then(
14+
(o) => o,
15+
(e) => {
16+
if (e instanceof NonZeroExitError) {
17+
const fullCommand = args?.length
18+
? `${command} ${args.map((a) => (a.includes(' ') ? `"${a}"` : a)).join(' ')}`
19+
: command;
20+
const message = `The command \`${fullCommand}\` exited with code ${e.exitCode}`;
21+
const newError = new Error(message, e.cause ? { cause: e.cause } : undefined);
22+
(newError as any).stderr = e.output?.stderr;
23+
(newError as any).stdout = e.output?.stdout;
24+
throw newError;
25+
}
26+
throw e;
27+
},
28+
);
29+
},
30+
};
31+
}

packages/astro/src/cli/docs/open.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/astro/src/cli/exec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NonZeroExitError, type Options, x } from 'tinyexec';
22

33
/**
44
* Improve tinyexec error logging and set `throwOnError` to `true` by default
5+
* @deprecated use CommandExecutor instead
56
*/
67
export function exec(command: string, args?: string[], options?: Partial<Options>) {
78
return x(command, args, {

packages/astro/src/cli/index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,29 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
102102
import('./create-key/infra/crypto-key-generator.js'),
103103
import('./create-key/core/create-key.js'),
104104
]);
105+
105106
const keyGenerator = createCryptoKeyGenerator();
106107
return await runner.run(createKeyCommand, { logger, keyGenerator });
107108
}
108109
case 'docs': {
109-
const { docs } = await import('./docs/index.js');
110-
await docs({ flags });
111-
return;
110+
const [
111+
{ createTinyexecCommandExecutor },
112+
{ createProcessPlatformProvider },
113+
{ openDocsCommand },
114+
] = await Promise.all([
115+
import('./docs/infra/tinyexec-command-executor.js'),
116+
import('./docs/infra/process-platform-provider.js'),
117+
import('./docs/core/open-docs.js'),
118+
]);
119+
const commandExecutor = createTinyexecCommandExecutor();
120+
const platformProvider = createProcessPlatformProvider();
121+
122+
return await runner.run(openDocsCommand, {
123+
url: 'https://docs.astro.build/',
124+
logger,
125+
commandExecutor,
126+
platformProvider,
127+
});
112128
}
113129
case 'telemetry': {
114130
// Do not track session start, since the user may be trying to enable,
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { AstroVersionProvider } from '../definitions.js';
22

33
export function createBuildTimeAstroVersionProvider(): AstroVersionProvider {
4+
const version = process.env.PACKAGE_VERSION ?? '';
45
return {
56
getVersion() {
6-
return process.env.PACKAGE_VERSION ?? '';
7+
return version;
78
},
89
};
910
}

0 commit comments

Comments
 (0)