Skip to content

Commit a4a74ab

Browse files
feat(cli): add help flags to various commands (#6394)
Co-authored-by: Happydev <[email protected]>
1 parent b087b83 commit a4a74ab

File tree

8 files changed

+128
-19
lines changed

8 files changed

+128
-19
lines changed

.changeset/old-rivers-remember.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Add `--help` to various commands: `check`, `sync`, `dev`, `preview`, and `build`

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
import { AstroCheck, DiagnosticSeverity } from '@astrojs/language-server';
33
import type { AstroSettings } from '../../@types/astro';
44
import type { LogOptions } from '../../core/logger/core.js';
5-
65
import glob from 'fast-glob';
76
import * as fs from 'fs';
87
import { bold, dim, red, yellow } from 'kleur/colors';
98
import { createRequire } from 'module';
109
import ora from 'ora';
1110
import { fileURLToPath, pathToFileURL } from 'url';
1211
import { printDiagnostic } from './print.js';
12+
import { Arguments } from 'yargs-parser';
13+
import { printHelp } from '../../core/messages.js';
1314

1415
interface Result {
1516
errors: number;
@@ -18,11 +19,25 @@ interface Result {
1819
hints: number;
1920
}
2021

21-
export async function check(settings: AstroSettings, { logging }: { logging: LogOptions }) {
22+
export async function check(
23+
settings: AstroSettings,
24+
{ logging, flags }: { logging: LogOptions; flags: Arguments }
25+
) {
26+
if (flags.help || flags.h) {
27+
printHelp({
28+
commandName: 'astro check',
29+
usage: '[...flags]',
30+
tables: {
31+
Flags: [['--help (-h)', 'See all available flags.']],
32+
},
33+
description: `Runs diagnostics against your project and reports errors to the console.`,
34+
});
35+
return;
36+
}
2237
console.log(bold('astro check'));
2338

2439
const { syncCli } = await import('../../core/sync/index.js');
25-
const syncRet = await syncCli(settings, { logging, fs });
40+
const syncRet = await syncCli(settings, { logging, fs, flags });
2641
// early exit on sync failure
2742
if (syncRet === 1) return syncRet;
2843

packages/astro/src/cli/index.ts

+28-10
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,18 @@ async function printVersion() {
7575
/** Determine which command the user requested */
7676
function resolveCommand(flags: Arguments): CLICommand {
7777
const cmd = flags._[2] as string;
78-
if (cmd === 'add') return 'add';
79-
if (cmd === 'sync') return 'sync';
80-
if (cmd === 'telemetry') return 'telemetry';
8178
if (flags.version) return 'version';
82-
else if (flags.help) return 'help';
8379

84-
const supportedCommands = new Set(['dev', 'build', 'preview', 'check', 'docs']);
80+
const supportedCommands = new Set([
81+
'add',
82+
'sync',
83+
'telemetry',
84+
'dev',
85+
'build',
86+
'preview',
87+
'check',
88+
'docs',
89+
]);
8590
if (supportedCommands.has(cmd)) {
8691
return cmd as CLICommand;
8792
}
@@ -144,6 +149,16 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
144149
}
145150
case 'docs': {
146151
telemetry.record(event.eventCliSession(cmd));
152+
if (flags.help || flags.h) {
153+
printHelp({
154+
commandName: 'astro docs',
155+
tables: {
156+
Flags: [['--help (-h)', 'See all available flags.']],
157+
},
158+
description: `Launches the Astro Docs website directly from the terminal.`,
159+
});
160+
return;
161+
}
147162
return await openInBrowser('https://docs.astro.build/');
148163
}
149164
case 'telemetry': {
@@ -203,26 +218,29 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
203218
case 'build': {
204219
const { default: build } = await import('../core/build/index.js');
205220

206-
return await build(settings, { ...flags, logging, telemetry, teardownCompiler: true });
221+
return await build(settings, { flags, logging, telemetry, teardownCompiler: true });
207222
}
208223

209224
case 'check': {
210-
const ret = await check(settings, { logging });
225+
const ret = await check(settings, { logging, flags });
211226
return process.exit(ret);
212227
}
213228

214229
case 'sync': {
215230
const { syncCli } = await import('../core/sync/index.js');
216231

217-
const ret = await syncCli(settings, { logging, fs });
232+
const ret = await syncCli(settings, { logging, fs, flags });
218233
return process.exit(ret);
219234
}
220235

221236
case 'preview': {
222237
const { default: preview } = await import('../core/preview/index.js');
223238

224-
const server = await preview(settings, { logging, telemetry });
225-
return await server.closed(); // keep alive until the server is closed
239+
const server = await preview(settings, { logging, telemetry, flags });
240+
if (server) {
241+
return await server.closed(); // keep alive until the server is closed
242+
}
243+
return;
226244
}
227245
}
228246

packages/astro/src/cli/telemetry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface TelemetryOptions {
1010
export async function update(subcommand: string, { flags, telemetry }: TelemetryOptions) {
1111
const isValid = ['enable', 'disable', 'reset'].includes(subcommand);
1212

13-
if (flags.help || !isValid) {
13+
if (flags.help || flags.h || !isValid) {
1414
msg.printHelp({
1515
commandName: 'astro telemetry',
1616
usage: '[command]',

packages/astro/src/core/build/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { collectPagesData } from './page-data.js';
2121
import { staticBuild, viteBuild } from './static-build.js';
2222
import { StaticBuildOptions } from './types.js';
2323
import { getTimeStat } from './util.js';
24+
import { printHelp } from '../messages.js';
25+
import yargs from 'yargs-parser';
2426

2527
export interface BuildOptions {
2628
mode?: RuntimeMode;
@@ -31,11 +33,27 @@ export interface BuildOptions {
3133
* building once, but may cause a performance hit if building multiple times in a row.
3234
*/
3335
teardownCompiler?: boolean;
36+
flags?: yargs.Arguments;
3437
}
3538

3639
/** `astro build` */
3740
export default async function build(settings: AstroSettings, options: BuildOptions): Promise<void> {
3841
applyPolyfill();
42+
if (options.flags?.help || options.flags?.h) {
43+
printHelp({
44+
commandName: 'astro build',
45+
usage: '[...flags]',
46+
tables: {
47+
Flags: [
48+
['--drafts', `Include Markdown draft pages in the build.`],
49+
['--help (-h)', 'See all available flags.'],
50+
],
51+
},
52+
description: `Builds your site for deployment.`,
53+
});
54+
return;
55+
}
56+
3957
const builder = new AstroBuilder(settings, options);
4058
await builder.run();
4159
}

packages/astro/src/core/dev/dev.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import { info, LogOptions, warn } from '../logger/core.js';
1010
import * as msg from '../messages.js';
1111
import { startContainer } from './container.js';
1212
import { createContainerWithAutomaticRestart } from './restart.js';
13+
import { printHelp } from '../messages.js';
14+
import { cyan } from 'kleur/colors';
1315

1416
export interface DevOptions {
1517
configFlag: string | undefined;
1618
configFlagPath: string | undefined;
17-
flags: yargs.Arguments | undefined;
19+
flags?: yargs.Arguments;
1820
logging: LogOptions;
1921
telemetry: AstroTelemetry;
2022
handleConfigError: (error: Error) => void;
@@ -32,7 +34,26 @@ export interface DevServer {
3234
export default async function dev(
3335
settings: AstroSettings,
3436
options: DevOptions
35-
): Promise<DevServer> {
37+
): Promise<DevServer | undefined> {
38+
if (options.flags?.help || options.flags?.h) {
39+
printHelp({
40+
commandName: 'astro dev',
41+
usage: '[...flags]',
42+
tables: {
43+
Flags: [
44+
['--port', `Specify which port to run on. Defaults to 3000.`],
45+
['--host', `Listen on all addresses, including LAN and public addresses.`],
46+
['--host <custom-address>', `Expose on a network IP address at <custom-address>`],
47+
['--help (-h)', 'See all available flags.'],
48+
],
49+
},
50+
description: `Check ${cyan(
51+
'https://docs.astro.build/en/reference/cli-reference/#astro-dev'
52+
)} for more information.`,
53+
});
54+
return;
55+
}
56+
3657
const devStart = performance.now();
3758
await options.telemetry.record([]);
3859

packages/astro/src/core/preview/index.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,35 @@ import { runHookConfigDone, runHookConfigSetup } from '../../integrations/index.
66
import type { LogOptions } from '../logger/core';
77
import createStaticPreviewServer from './static-preview-server.js';
88
import { getResolvedHostForHttpServer } from './util.js';
9+
import type { Arguments } from 'yargs-parser';
10+
import { printHelp } from '../messages.js';
11+
import { cyan } from 'kleur/colors';
912

1013
interface PreviewOptions {
1114
logging: LogOptions;
1215
telemetry: AstroTelemetry;
16+
flags?: Arguments;
1317
}
1418

1519
/** The primary dev action */
1620
export default async function preview(
1721
_settings: AstroSettings,
18-
{ logging }: PreviewOptions
19-
): Promise<PreviewServer> {
22+
{ logging, flags }: PreviewOptions
23+
): Promise<PreviewServer | undefined> {
24+
if (flags?.help || flags?.h) {
25+
printHelp({
26+
commandName: 'astro preview',
27+
usage: '[...flags]',
28+
tables: {
29+
Flags: [['--help (-h)', 'See all available flags.']],
30+
},
31+
description: `Starts a local server to serve your static dist/ directory. Check ${cyan(
32+
'https://docs.astro.build/en/reference/cli-reference/#astro-preview'
33+
)} for more information.`,
34+
});
35+
return;
36+
}
37+
2038
const settings = await runHookConfigSetup({
2139
settings: _settings,
2240
command: 'preview',

packages/astro/src/core/sync/index.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@ import { getTimeStat } from '../build/util.js';
1111
import { createVite } from '../create-vite.js';
1212
import { AstroError, AstroErrorData } from '../errors/index.js';
1313
import { info, LogOptions } from '../logger/core.js';
14+
import { printHelp } from '../messages.js';
15+
import { Arguments } from 'yargs-parser';
1416

1517
type ProcessExit = 0 | 1;
1618

1719
export async function syncCli(
1820
settings: AstroSettings,
19-
{ logging, fs }: { logging: LogOptions; fs: typeof fsMod }
21+
{ logging, fs, flags }: { logging: LogOptions; fs: typeof fsMod; flags?: Arguments }
2022
): Promise<ProcessExit> {
23+
if (flags?.help || flags?.h) {
24+
printHelp({
25+
commandName: 'astro sync',
26+
usage: '[...flags]',
27+
tables: {
28+
Flags: [['--help (-h)', 'See all available flags.']],
29+
},
30+
description: `Generates TypeScript types for all Astro modules.`,
31+
});
32+
return 0;
33+
}
34+
2135
const resolvedSettings = await runHookConfigSetup({
2236
settings,
2337
logging,

0 commit comments

Comments
 (0)