Skip to content

Commit

Permalink
feat: astro info command (#7432)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
ematipico and sarah11918 authored Jul 5, 2023
1 parent c490eb6 commit 6e9c295
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
20 changes: 20 additions & 0 deletions .changeset/happy-donuts-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'astro': minor
---

Adds a new command `astro info`, useful for sharing debugging information about your current environment when you need help!

```shell
astro info
```

Output

```
Astro version v2.6.6
Package manager pnpm
Platform darwin
Architecture arm64
Adapter @astrojs/vercel/serverless
Integrations None
```
1 change: 1 addition & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"vfile": "^5.3.7",
"vite": "^4.3.9",
"vitefu": "^0.2.4",
"which-pm": "^2.0.0",
"yargs-parser": "^21.1.1",
"zod": "^3.20.6"
},
Expand Down
69 changes: 63 additions & 6 deletions packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { formatConfigErrorMessage, formatErrorMessage, printHelp } from '../core
import * as event from '../events/index.js';
import { eventConfigError, eventError, telemetry } from '../events/index.js';
import { openInBrowser } from './open.js';
import { arch, platform } from 'node:os';

type Arguments = yargs.Arguments;
type CLICommand =
Expand All @@ -32,6 +33,7 @@ type CLICommand =
| 'reload'
| 'sync'
| 'check'
| 'info'
| 'telemetry';

/** Display --help flag */
Expand All @@ -47,6 +49,7 @@ function printAstroHelp() {
['check', 'Check your project for errors.'],
['dev', 'Start the development server.'],
['docs', 'Open documentation in your web browser.'],
['info', 'List info about your current Astro setup.'],
['preview', 'Preview your build locally.'],
['sync', 'Generate content collection types.'],
['telemetry', 'Configure telemetry settings.'],
Expand All @@ -71,6 +74,56 @@ async function printVersion() {
console.log(` ${colors.bgGreen(colors.black(` astro `))} ${colors.green(`v${ASTRO_VERSION}`)}`);
}

async function printInfo({
cwd,
flags,
logging,
}: {
cwd?: string;
flags?: Flags;
logging: LogOptions;
}) {
const whichPm = await import('which-pm');
const packageManager = await whichPm.default(process.cwd());
let adapter = "Couldn't determine.";
let integrations = [];

const MAX_PADDING = 25;
function printRow(label: string, value: string) {
const padding = MAX_PADDING - label.length;
console.log(`${colors.bold(label)}` + ' '.repeat(padding) + `${colors.green(value)}`);
}

try {
const { userConfig } = await openConfig({
cwd,
flags,
cmd: 'info',
logging,
});
if (userConfig.adapter?.name) {
adapter = userConfig.adapter.name;
}
if (userConfig.integrations) {
integrations = (userConfig?.integrations ?? [])
.filter(Boolean)
.flat()
.map((i: any) => i?.name);
}
} catch (_e) {}
console.log();
printRow('Astro version', `v${ASTRO_VERSION}`);
printRow('Package manager', packageManager.name);
printRow('Platform', platform());
printRow('Architecture', arch());
printRow('Adapter', adapter);
let integrationsString = "None or couldn't determine.";
if (integrations.length > 0) {
integrationsString = integrations.join(', ');
}
printRow('Integrations', integrationsString);
}

/** Determine which command the user requested */
function resolveCommand(flags: Arguments): CLICommand {
const cmd = flags._[2] as string;
Expand All @@ -85,6 +138,7 @@ function resolveCommand(flags: Arguments): CLICommand {
'preview',
'check',
'docs',
'info',
]);
if (supportedCommands.has(cmd)) {
return cmd as CLICommand;
Expand Down Expand Up @@ -116,21 +170,24 @@ async function handleConfigError(
**/
async function runCommand(cmd: string, flags: yargs.Arguments) {
const root = flags.root;

// logLevel
let logging: LogOptions = {
dest: nodeLogDestination,
level: 'info',
};
switch (cmd) {
case 'help':
printAstroHelp();
return process.exit(0);
case 'version':
await printVersion();
return process.exit(0);
case 'info': {
await printInfo({ cwd: root, flags, logging });
return process.exit(0);
}
}

// logLevel
let logging: LogOptions = {
dest: nodeLogDestination,
level: 'info',
};
if (flags.verbose) {
logging.level = 'debug';
enableVerboseLogging();
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6e9c295

Please sign in to comment.