Compare commits
1 commit
main
...
cli-dynami
Author | SHA1 | Date | |
---|---|---|---|
|
4db2a9c9a8 |
2 changed files with 36 additions and 21 deletions
|
@ -3,22 +3,9 @@ import fs from 'fs';
|
||||||
import * as colors from 'kleur/colors';
|
import * as colors from 'kleur/colors';
|
||||||
import type { Arguments as Flags } from 'yargs-parser';
|
import type { Arguments as Flags } from 'yargs-parser';
|
||||||
import yargs from 'yargs-parser';
|
import yargs from 'yargs-parser';
|
||||||
import { ZodError } from 'zod';
|
|
||||||
import {
|
|
||||||
createSettings,
|
|
||||||
openConfig,
|
|
||||||
resolveConfigPath,
|
|
||||||
resolveFlags,
|
|
||||||
} from '../core/config/index.js';
|
|
||||||
import { ASTRO_VERSION } from '../core/constants.js';
|
import { ASTRO_VERSION } from '../core/constants.js';
|
||||||
import { collectErrorMetadata } from '../core/errors/dev/index.js';
|
|
||||||
import { createSafeError } from '../core/errors/index.js';
|
|
||||||
import { debug, error, info, type LogOptions } from '../core/logger/core.js';
|
import { debug, error, info, type LogOptions } from '../core/logger/core.js';
|
||||||
import { enableVerboseLogging, nodeLogDestination } from '../core/logger/node.js';
|
import { enableVerboseLogging, nodeLogDestination } from '../core/logger/node.js';
|
||||||
import { formatConfigErrorMessage, formatErrorMessage, printHelp } from '../core/messages.js';
|
|
||||||
import * as event from '../events/index.js';
|
|
||||||
import { eventConfigError, eventError, telemetry } from '../events/index.js';
|
|
||||||
import { openInBrowser } from './open.js';
|
|
||||||
|
|
||||||
type Arguments = yargs.Arguments;
|
type Arguments = yargs.Arguments;
|
||||||
type CLICommand =
|
type CLICommand =
|
||||||
|
@ -35,7 +22,8 @@ type CLICommand =
|
||||||
| 'telemetry';
|
| 'telemetry';
|
||||||
|
|
||||||
/** Display --help flag */
|
/** Display --help flag */
|
||||||
function printAstroHelp() {
|
async function printAstroHelp() {
|
||||||
|
const { printHelp } = await import('../core/messages.js');
|
||||||
printHelp({
|
printHelp({
|
||||||
commandName: 'astro',
|
commandName: 'astro',
|
||||||
usage: '[command] [...flags]',
|
usage: '[command] [...flags]',
|
||||||
|
@ -96,13 +84,20 @@ async function handleConfigError(
|
||||||
e: any,
|
e: any,
|
||||||
{ cmd, cwd, flags, logging }: { cmd: string; cwd?: string; flags?: Flags; logging: LogOptions }
|
{ cmd, cwd, flags, logging }: { cmd: string; cwd?: string; flags?: Flags; logging: LogOptions }
|
||||||
) {
|
) {
|
||||||
|
const { resolveConfigPath } = await import('../core/config/index.js');
|
||||||
const path = await resolveConfigPath({ cwd, flags, fs });
|
const path = await resolveConfigPath({ cwd, flags, fs });
|
||||||
error(logging, 'astro', `Unable to load ${path ? colors.bold(path) : 'your Astro config'}\n`);
|
error(logging, 'astro', `Unable to load ${path ? colors.bold(path) : 'your Astro config'}\n`);
|
||||||
|
|
||||||
|
const { ZodError } = await import('zod');
|
||||||
|
const { formatConfigErrorMessage, formatErrorMessage } = await import('../core/messages.js');
|
||||||
if (e instanceof ZodError) {
|
if (e instanceof ZodError) {
|
||||||
console.error(formatConfigErrorMessage(e) + '\n');
|
console.error(formatConfigErrorMessage(e) + '\n');
|
||||||
} else if (e instanceof Error) {
|
} else if (e instanceof Error) {
|
||||||
|
const { collectErrorMetadata } = await import('../core/errors/dev/index.js');
|
||||||
console.error(formatErrorMessage(collectErrorMetadata(e)) + '\n');
|
console.error(formatErrorMessage(collectErrorMetadata(e)) + '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { eventConfigError, telemetry } = await import('../events/index.js');
|
||||||
const telemetryPromise = telemetry.record(eventConfigError({ cmd, err: e, isFatal: true }));
|
const telemetryPromise = telemetry.record(eventConfigError({ cmd, err: e, isFatal: true }));
|
||||||
await telemetryPromise.catch((err2: Error) =>
|
await telemetryPromise.catch((err2: Error) =>
|
||||||
debug('telemetry', `record() error: ${err2.message}`)
|
debug('telemetry', `record() error: ${err2.message}`)
|
||||||
|
@ -119,7 +114,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case 'help':
|
case 'help':
|
||||||
printAstroHelp();
|
await printAstroHelp();
|
||||||
return process.exit(0);
|
return process.exit(0);
|
||||||
case 'version':
|
case 'version':
|
||||||
await printVersion();
|
await printVersion();
|
||||||
|
@ -146,13 +141,19 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
case 'add': {
|
case 'add': {
|
||||||
const { default: add } = await import('../core/add/index.js');
|
const { default: add } = await import('../core/add/index.js');
|
||||||
|
|
||||||
telemetry.record(event.eventCliSession(cmd));
|
import('../events/index.js').then(({ eventCliSession, telemetry }) => {
|
||||||
|
telemetry.record(eventCliSession(cmd));
|
||||||
|
});
|
||||||
|
|
||||||
const packages = flags._.slice(3) as string[];
|
const packages = flags._.slice(3) as string[];
|
||||||
return await add(packages, { cwd: root, flags, logging, telemetry });
|
return await add(packages, { cwd: root, flags, logging });
|
||||||
}
|
}
|
||||||
case 'docs': {
|
case 'docs': {
|
||||||
telemetry.record(event.eventCliSession(cmd));
|
import('../events/index.js').then(({ eventCliSession, telemetry }) => {
|
||||||
|
telemetry.record(eventCliSession(cmd));
|
||||||
|
});
|
||||||
if (flags.help || flags.h) {
|
if (flags.help || flags.h) {
|
||||||
|
const { printHelp } = await import('../core/messages.js');
|
||||||
printHelp({
|
printHelp({
|
||||||
commandName: 'astro docs',
|
commandName: 'astro docs',
|
||||||
tables: {
|
tables: {
|
||||||
|
@ -162,6 +163,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const { openInBrowser } = await import('./open.js');
|
||||||
return await openInBrowser('https://docs.astro.build/');
|
return await openInBrowser('https://docs.astro.build/');
|
||||||
}
|
}
|
||||||
case 'telemetry': {
|
case 'telemetry': {
|
||||||
|
@ -170,6 +172,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
// Do not track session start, since the user may be trying to enable,
|
// Do not track session start, since the user may be trying to enable,
|
||||||
// disable, or modify telemetry settings.
|
// disable, or modify telemetry settings.
|
||||||
const subcommand = flags._[3]?.toString();
|
const subcommand = flags._[3]?.toString();
|
||||||
|
const { telemetry } = await import('../events/index.js');
|
||||||
return await telemetryHandler.update(subcommand, { flags, telemetry });
|
return await telemetryHandler.update(subcommand, { flags, telemetry });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,6 +182,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
process.env.NODE_ENV = cmd === 'dev' ? 'development' : 'production';
|
process.env.NODE_ENV = cmd === 'dev' ? 'development' : 'production';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { openConfig } = await import('../core/config/index.js');
|
||||||
let { astroConfig: initialAstroConfig, userConfig: initialUserConfig } = await openConfig({
|
let { astroConfig: initialAstroConfig, userConfig: initialUserConfig } = await openConfig({
|
||||||
cwd: root,
|
cwd: root,
|
||||||
flags,
|
flags,
|
||||||
|
@ -189,7 +193,10 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
return {} as any;
|
return {} as any;
|
||||||
});
|
});
|
||||||
if (!initialAstroConfig) return;
|
if (!initialAstroConfig) return;
|
||||||
telemetry.record(event.eventCliSession(cmd, initialUserConfig, flags));
|
import('../events/index.js').then(({ eventCliSession, telemetry }) => {
|
||||||
|
telemetry.record(eventCliSession(cmd, initialUserConfig, flags));
|
||||||
|
});
|
||||||
|
const { createSettings } = await import('../core/config/index.js');
|
||||||
let settings = createSettings(initialAstroConfig, root);
|
let settings = createSettings(initialAstroConfig, root);
|
||||||
|
|
||||||
// Common CLI Commands:
|
// Common CLI Commands:
|
||||||
|
@ -198,6 +205,8 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case 'dev': {
|
case 'dev': {
|
||||||
const { default: devServer } = await import('../core/dev/index.js');
|
const { default: devServer } = await import('../core/dev/index.js');
|
||||||
|
const { telemetry } = await import('../events/index.js');
|
||||||
|
const { resolveFlags, resolveConfigPath } = await import('../core/config/index.js');
|
||||||
|
|
||||||
const configFlag = resolveFlags(flags).config;
|
const configFlag = resolveFlags(flags).config;
|
||||||
const configFlagPath = configFlag
|
const configFlagPath = configFlag
|
||||||
|
@ -220,6 +229,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
|
|
||||||
case 'build': {
|
case 'build': {
|
||||||
const { default: build } = await import('../core/build/index.js');
|
const { default: build } = await import('../core/build/index.js');
|
||||||
|
const { telemetry } = await import('../events/index.js');
|
||||||
|
|
||||||
return await build(settings, {
|
return await build(settings, {
|
||||||
flags,
|
flags,
|
||||||
|
@ -255,6 +265,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
|
|
||||||
case 'preview': {
|
case 'preview': {
|
||||||
const { default: preview } = await import('../core/preview/index.js');
|
const { default: preview } = await import('../core/preview/index.js');
|
||||||
|
const { telemetry } = await import('../events/index.js');
|
||||||
|
|
||||||
const server = await preview(settings, { logging, telemetry, flags });
|
const server = await preview(settings, { logging, telemetry, flags });
|
||||||
if (server) {
|
if (server) {
|
||||||
|
@ -288,6 +299,11 @@ async function throwAndExit(cmd: string, err: unknown) {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { collectErrorMetadata } = await import('../core/errors/dev/index.js');
|
||||||
|
const { createSafeError } = await import('../core/errors/index.js');
|
||||||
|
const { eventError, telemetry } = await import('../events/index.js');
|
||||||
|
const { formatErrorMessage } = await import('../core/messages.js');
|
||||||
|
|
||||||
const errorWithMetadata = collectErrorMetadata(createSafeError(err));
|
const errorWithMetadata = collectErrorMetadata(createSafeError(err));
|
||||||
telemetryPromise = telemetry.record(eventError({ cmd, err: errorWithMetadata, isFatal: true }));
|
telemetryPromise = telemetry.record(eventError({ cmd, err: errorWithMetadata, isFatal: true }));
|
||||||
errorMessage = formatErrorMessage(errorWithMetadata);
|
errorMessage = formatErrorMessage(errorWithMetadata);
|
||||||
|
|
|
@ -30,7 +30,6 @@ import { wrapDefaultExport } from './wrapper.js';
|
||||||
export interface AddOptions {
|
export interface AddOptions {
|
||||||
logging: LogOptions;
|
logging: LogOptions;
|
||||||
flags: yargs.Arguments;
|
flags: yargs.Arguments;
|
||||||
telemetry: AstroTelemetry;
|
|
||||||
cwd?: string;
|
cwd?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +82,7 @@ async function getRegistry(): Promise<string> {
|
||||||
return stdout || 'https://registry.npmjs.org';
|
return stdout || 'https://registry.npmjs.org';
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function add(names: string[], { cwd, flags, logging, telemetry }: AddOptions) {
|
export default async function add(names: string[], { cwd, flags, logging }: AddOptions) {
|
||||||
applyPolyfill();
|
applyPolyfill();
|
||||||
if (flags.help || names.length === 0) {
|
if (flags.help || names.length === 0) {
|
||||||
printHelp({
|
printHelp({
|
||||||
|
|
Loading…
Reference in a new issue