Skip to content

Commit

Permalink
feat: astro rage command
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jun 20, 2023
1 parent 1589eaa commit a07280d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
18 changes: 18 additions & 0 deletions .changeset/happy-donuts-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'astro': minor
---

New command `astro rage`, useful to share debugging information about your current environment

```shell
astro rage
```

Output

```
Astro version v2.6.6
Package manager pnpm
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 @@ -167,6 +167,7 @@
"vfile": "^5.3.2",
"vite": "^4.3.1",
"vitefu": "^0.2.4",
"which-pm": "^2.0.0",
"yargs-parser": "^21.0.1",
"zod": "^3.20.6"
},
Expand Down
75 changes: 69 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 type { AstroIntegration } from '../@types/astro';

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

/** Display --help flag */
Expand All @@ -50,6 +52,7 @@ function printAstroHelp() {
['preview', 'Preview your build locally.'],
['sync', 'Generate content collection types.'],
['telemetry', 'Configure telemetry settings.'],
['rage', 'Emit useful information about Astro current setup. Useful for debugging.'],
],
'Global Flags': [
['--config <path>', 'Specify your config file.'],
Expand All @@ -71,6 +74,62 @@ async function printVersion() {
console.log(` ${colors.bgGreen(colors.black(` astro `))} ${colors.green(`v${ASTRO_VERSION}`)}`);
}

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

function isIntegration(integration: any): integration is AstroIntegration {
if (typeof integration === 'object') {
return true;
}
return false;
}

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: 'rage',
logging,
});
if (userConfig.adapter?.name) {
adapter = userConfig.adapter.name;
}
if (userConfig.integrations) {
for (const integration of userConfig.integrations) {
if (isIntegration(integration)) {
integrations.push(integration.name);
}
}
}
} catch (_e) {}
console.log();
printRow('Astro version', `v${ASTRO_VERSION}`);
printRow('Package manager', packageManager.name);
printRow('Adapter', adapter);
let integrationsString = 'None';
if (integrations.length === 0) {
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 +144,7 @@ function resolveCommand(flags: Arguments): CLICommand {
'preview',
'check',
'docs',
'rage',
]);
if (supportedCommands.has(cmd)) {
return cmd as CLICommand;
Expand Down Expand Up @@ -116,21 +176,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 'rage': {
await printRage({ 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 a07280d

Please sign in to comment.