diff --git a/packages/astro/src/core/add/babel.ts b/packages/astro/src/cli/add/babel.ts similarity index 100% rename from packages/astro/src/core/add/babel.ts rename to packages/astro/src/cli/add/babel.ts diff --git a/packages/astro/src/core/add/imports.ts b/packages/astro/src/cli/add/imports.ts similarity index 100% rename from packages/astro/src/core/add/imports.ts rename to packages/astro/src/cli/add/imports.ts diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/cli/add/index.ts similarity index 97% rename from packages/astro/src/core/add/index.ts rename to packages/astro/src/cli/add/index.ts index 9c7c701af..f9ab4e6f9 100644 --- a/packages/astro/src/core/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -9,35 +9,36 @@ import preferredPM from 'preferred-pm'; import prompts from 'prompts'; import { fileURLToPath, pathToFileURL } from 'url'; import type yargs from 'yargs-parser'; -import { loadTSConfig, resolveConfigPath } from '../config/index.js'; +import { loadTSConfig, resolveConfigPath } from '../../core/config/index.js'; import { defaultTSConfig, presets, updateTSConfigForFramework, type frameworkWithTSSettings, -} from '../config/tsconfig.js'; -import { debug, info, type LogOptions } from '../logger/core.js'; -import * as msg from '../messages.js'; -import { printHelp } from '../messages.js'; -import { appendForwardSlash } from '../path.js'; -import { apply as applyPolyfill } from '../polyfill.js'; -import { parseNpmName } from '../util.js'; +} from '../../core/config/tsconfig.js'; +import { debug, info, type LogOptions } from '../../core/logger/core.js'; +import * as msg from '../../core/messages.js'; +import { printHelp } from '../../core/messages.js'; +import { appendForwardSlash } from '../../core/path.js'; +import { apply as applyPolyfill } from '../../core/polyfill.js'; +import { parseNpmName } from '../../core/util.js'; import { generate, parse, t, visit } from './babel.js'; import { ensureImport } from './imports.js'; import { wrapDefaultExport } from './wrapper.js'; -export interface AddOptions { +interface AddOptions { logging: LogOptions; flags: yargs.Arguments; cwd?: string; } -export interface IntegrationInfo { +interface IntegrationInfo { id: string; packageName: string; dependencies: [name: string, version: string][]; type: 'integration' | 'adapter'; } + const ALIASES = new Map([ ['solid', 'solid-js'], ['tailwindcss', 'tailwind'], @@ -85,7 +86,7 @@ async function getRegistry(): Promise { } } -export default async function add(names: string[], { cwd, flags, logging }: AddOptions) { +export async function add(names: string[], { cwd, flags, logging }: AddOptions) { applyPolyfill(); if (flags.help || names.length === 0) { printHelp({ diff --git a/packages/astro/src/core/add/wrapper.ts b/packages/astro/src/cli/add/wrapper.ts similarity index 100% rename from packages/astro/src/core/add/wrapper.ts rename to packages/astro/src/cli/add/wrapper.ts diff --git a/packages/astro/src/cli/docs/index.ts b/packages/astro/src/cli/docs/index.ts new file mode 100644 index 000000000..cd6325577 --- /dev/null +++ b/packages/astro/src/cli/docs/index.ts @@ -0,0 +1,22 @@ +import type yargs from 'yargs-parser'; +import { printHelp } from '../../core/messages.js'; +import { openInBrowser } from './open.js'; + +interface DocsOptions { + flags: yargs.Arguments; +} + +export async function docs({ flags }: DocsOptions) { + if (flags.help || flags.h) { + printHelp({ + commandName: 'astro docs', + tables: { + Flags: [['--help (-h)', 'See all available flags.']], + }, + description: `Launches the Astro Docs website directly from the terminal.`, + }); + return; + } + + return await openInBrowser('https://docs.astro.build/'); +} diff --git a/packages/astro/src/cli/open.ts b/packages/astro/src/cli/docs/open.ts similarity index 100% rename from packages/astro/src/cli/open.ts rename to packages/astro/src/cli/docs/open.ts diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index e4358ca24..60a8f3c32 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -1,7 +1,6 @@ /* eslint-disable no-console */ import fs from 'fs'; import * as colors from 'kleur/colors'; -import { arch, platform } from 'node:os'; import type { Arguments as Flags } from 'yargs-parser'; import yargs from 'yargs-parser'; import { ZodError } from 'zod'; @@ -19,7 +18,6 @@ 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 CLICommand = @@ -69,62 +67,11 @@ function printAstroHelp() { } /** Display --version flag */ -async function printVersion() { +function printVersion() { console.log(); 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(); - const packageManagerName = packageManager?.name ?? "Couldn't determine."; - printRow('Astro version', `v${ASTRO_VERSION}`); - printRow('Package manager', packageManagerName); - 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; @@ -171,24 +118,40 @@ async function handleConfigError( **/ async function runCommand(cmd: string, flags: yargs.Arguments) { const root = flags.root; - // logLevel - let logging: LogOptions = { - dest: nodeLogDestination, - level: 'info', - }; + + // These commands can run directly without parsing the user config. switch (cmd) { case 'help': printAstroHelp(); - return process.exit(0); + return; case 'version': - await printVersion(); - return process.exit(0); + printVersion(); + return; case 'info': { - await printInfo({ cwd: root, flags, logging }); - return process.exit(0); + const { printInfo } = await import('./info/index.js'); + await printInfo({ cwd: root, flags }); + return; + } + case 'docs': { + telemetry.record(event.eventCliSession(cmd)); + const { docs } = await import('./docs/index.js'); + await docs({ flags }); + return; + } + case 'telemetry': { + // Do not track session start, since the user may be trying to enable, + // disable, or modify telemetry settings. + const { update } = await import('./telemetry/index.js'); + const subcommand = flags._[3]?.toString(); + await update(subcommand, { flags }); + return; } } + const logging: LogOptions = { + dest: nodeLogDestination, + level: 'info', + }; if (flags.verbose) { logging.level = 'debug'; enableVerboseLogging(); @@ -196,39 +159,15 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { logging.level = 'silent'; } - // Special CLI Commands: "add", "docs", "telemetry" - // These commands run before the user's config is parsed, and may have other special - // conditions that should be handled here, before the others. - // + // These commands can also be run directly without parsing the user config, + // but they rely on parsing the `logging` first. switch (cmd) { case 'add': { - const { default: add } = await import('../core/add/index.js'); - telemetry.record(event.eventCliSession(cmd)); + const { add } = await import('./add/index.js'); const packages = flags._.slice(3) as string[]; - return await add(packages, { cwd: root, flags, logging }); - } - case 'docs': { - telemetry.record(event.eventCliSession(cmd)); - if (flags.help || flags.h) { - printHelp({ - commandName: 'astro docs', - tables: { - Flags: [['--help (-h)', 'See all available flags.']], - }, - description: `Launches the Astro Docs website directly from the terminal.`, - }); - return; - } - return await openInBrowser('https://docs.astro.build/'); - } - case 'telemetry': { - const telemetryHandler = await import('./telemetry.js'); - - // Do not track session start, since the user may be trying to enable, - // disable, or modify telemetry settings. - const subcommand = flags._[3]?.toString(); - return await telemetryHandler.update(subcommand, { flags }); + await add(packages, { cwd: root, flags, logging }); + return; } } @@ -241,7 +180,6 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { cwd: root, flags, cmd, - logging, }).catch(async (e) => { await handleConfigError(e, { cmd, cwd: root, flags, logging }); return {} as any; diff --git a/packages/astro/src/cli/info/index.ts b/packages/astro/src/cli/info/index.ts new file mode 100644 index 000000000..ed2ffd289 --- /dev/null +++ b/packages/astro/src/cli/info/index.ts @@ -0,0 +1,53 @@ +/* eslint-disable no-console */ +import { arch, platform } from 'node:os'; +import * as colors from 'kleur/colors'; +import whichPm from 'which-pm'; +import type yargs from 'yargs-parser'; +import { openConfig } from '../../core/config/index.js'; +import { ASTRO_VERSION } from '../../core/constants.js'; + +interface InfoOptions { + cwd?: string; + flags: yargs.Arguments; +} + +export async function printInfo({ cwd, flags }: InfoOptions) { + const packageManager = await whichPm(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', + }); + 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(); + const packageManagerName = packageManager?.name ?? "Couldn't determine."; + printRow('Astro version', `v${ASTRO_VERSION}`); + printRow('Package manager', packageManagerName); + 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); +} diff --git a/packages/astro/src/cli/telemetry.ts b/packages/astro/src/cli/telemetry/index.ts similarity index 87% rename from packages/astro/src/cli/telemetry.ts rename to packages/astro/src/cli/telemetry/index.ts index 7a5bfd469..5ff71f5bf 100644 --- a/packages/astro/src/cli/telemetry.ts +++ b/packages/astro/src/cli/telemetry/index.ts @@ -1,9 +1,9 @@ /* eslint-disable no-console */ import type yargs from 'yargs-parser'; -import * as msg from '../core/messages.js'; -import { telemetry } from '../events/index.js'; +import * as msg from '../../core/messages.js'; +import { telemetry } from '../../events/index.js'; -export interface TelemetryOptions { +interface TelemetryOptions { flags: yargs.Arguments; } diff --git a/packages/astro/src/config/index.ts b/packages/astro/src/config/index.ts index bdd3c492f..53183912a 100644 --- a/packages/astro/src/config/index.ts +++ b/packages/astro/src/config/index.ts @@ -34,10 +34,7 @@ export function getViteConfig(inlineConfig: UserConfig) { dest: nodeLogDestination, level: 'info', }; - const { astroConfig: config } = await openConfig({ - cmd, - logging, - }); + const { astroConfig: config } = await openConfig({ cmd }); const settings = createSettings(config, inlineConfig.root); await runHookConfigSetup({ settings, command: cmd, logging }); const viteConfig = await createVite( diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index 46fea5159..4395dceae 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -7,7 +7,6 @@ import path from 'path'; import { fileURLToPath, pathToFileURL } from 'url'; import { mergeConfig as mergeViteConfig } from 'vite'; import { AstroError, AstroErrorData } from '../errors/index.js'; -import type { LogOptions } from '../logger/core.js'; import { arraify, isObject, isURL } from '../util.js'; import { createRelativeSchema } from './schema.js'; import { loadConfigWithVite } from './vite-load.js'; @@ -163,7 +162,6 @@ interface LoadConfigOptions { flags?: Flags; cmd: string; validate?: boolean; - logging: LogOptions; /** Invalidate when reloading a previously loaded config */ isRestart?: boolean; fsMod?: typeof fs; diff --git a/packages/astro/src/core/dev/restart.ts b/packages/astro/src/core/dev/restart.ts index 6e4c0b16f..d96cc0b50 100644 --- a/packages/astro/src/core/dev/restart.ts +++ b/packages/astro/src/core/dev/restart.ts @@ -87,7 +87,6 @@ export async function restartContainer({ cwd: resolvedRoot, flags, cmd: 'dev', - logging, isRestart: true, fsMod: container.fs, });