Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: astro info command #7432

Merged
merged 8 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Just adding something that will become a heading here, since I think this will end up in the blog post draft, and the existing line would be too long.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussing with Chris, I'm not sure about updating the changeset here! 😅

This is a good changeset for the changelog, but not for the blogpost. So, I'm not sure whether to do any optimizing here at all, since there's no good choice.

As a default, I'll NOT suggest headings and stuff, only edit for typos etc., and let Matthew battle that when it comes blog post time! In which case, this one is fine as is!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No bother :) fee free to merge any suggestion you deem right


```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.