116 lines
3.9 KiB
JavaScript
116 lines
3.9 KiB
JavaScript
import { detectAgenticEnvironment } from "am-i-vibing";
|
|
import colors from "piccolore";
|
|
import devServer from "../../core/dev/index.js";
|
|
import { pathToFileURL } from "node:url";
|
|
import { checkExistingServer, removeLockFile, writeLockFile } from "../../core/dev/lockfile.js";
|
|
import { resolveRoot } from "../../core/config/config.js";
|
|
import { printHelp } from "../../core/messages/runtime.js";
|
|
import { createLoggerFromFlags, flagsToAstroInlineConfig } from "../flags.js";
|
|
function isRunByAgent() {
|
|
try {
|
|
return detectAgenticEnvironment().type === "agent";
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
async function dev({ flags }) {
|
|
if (flags.help || flags.h) {
|
|
printHelp({
|
|
commandName: "astro dev",
|
|
usage: "[command] [...flags]",
|
|
tables: {
|
|
Commands: [
|
|
["stop", "Stop a running background dev server."],
|
|
["status", "Check if a dev server is running."],
|
|
["logs [--follow]", "View logs from a background dev server."]
|
|
],
|
|
Flags: [
|
|
["--background", "Start the dev server as a background process."],
|
|
["--mode", `Specify the mode of the project. Defaults to "development".`],
|
|
["--port", `Specify which port to run on. Defaults to 4321.`],
|
|
["--host", `Listen on all addresses, including LAN and public addresses.`],
|
|
["--host <custom-address>", `Expose on a network IP address at <custom-address>`],
|
|
["--open", "Automatically open the app in the browser on server start"],
|
|
["--force", "Clear the content layer cache, forcing a full rebuild."],
|
|
[
|
|
"--allowed-hosts",
|
|
"Specify a comma-separated list of allowed hosts or allow any hostname."
|
|
],
|
|
["--help (-h)", "See all available flags."]
|
|
]
|
|
},
|
|
description: `Check ${colors.cyan(
|
|
"https://docs.astro.build/en/reference/cli-reference/#astro-dev"
|
|
)} for more information.`
|
|
});
|
|
return;
|
|
}
|
|
const agentDetected = !process.env.ASTRO_DEV_BACKGROUND && isRunByAgent();
|
|
if (agentDetected) {
|
|
flags.json = true;
|
|
}
|
|
const logger = createLoggerFromFlags(flags);
|
|
const subcommand = flags._[3]?.toString();
|
|
if (subcommand === "stop") {
|
|
const { stop } = await import("./stop.js");
|
|
await stop({ flags, logger });
|
|
return;
|
|
}
|
|
if (subcommand === "status") {
|
|
const { status } = await import("./status.js");
|
|
await status({ flags, logger });
|
|
return;
|
|
}
|
|
if (subcommand === "logs") {
|
|
const { logs } = await import("./logs.js");
|
|
await logs({ flags, logger });
|
|
return;
|
|
}
|
|
if (flags.background || agentDetected) {
|
|
const { background } = await import("./background.js");
|
|
await background({ flags, logger });
|
|
return;
|
|
}
|
|
if (subcommand) {
|
|
logger.error(
|
|
"SKIP_FORMAT",
|
|
`Unknown command: astro dev ${subcommand}
|
|
|
|
Run \`astro dev --help\` to see available commands.`
|
|
);
|
|
process.exit(1);
|
|
}
|
|
const root = pathToFileURL(resolveRoot(flags.root) + "/");
|
|
const existingServer = checkExistingServer(root);
|
|
if (existingServer) {
|
|
const message = [
|
|
"Another astro dev server is already running.",
|
|
"",
|
|
` URL: ${existingServer.url}`,
|
|
` PID: ${existingServer.pid}`,
|
|
"",
|
|
`Run \`astro dev stop\` to stop it, or use \`astro dev --force\` to replace it.`
|
|
].join("\n");
|
|
throw new Error(message);
|
|
}
|
|
const inlineConfig = flagsToAstroInlineConfig(flags);
|
|
const server = await devServer(inlineConfig);
|
|
const serverUrl = new URL(server.resolvedUrls.local[0]).origin;
|
|
writeLockFile(root, {
|
|
pid: process.pid,
|
|
port: server.address.port,
|
|
url: serverUrl,
|
|
urls: server.resolvedUrls,
|
|
background: !!process.env.ASTRO_DEV_BACKGROUND,
|
|
startedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
});
|
|
const originalStop = server.stop.bind(server);
|
|
server.stop = async () => {
|
|
removeLockFile(root);
|
|
await originalStop();
|
|
};
|
|
return server;
|
|
}
|
|
export {
|
|
dev
|
|
};
|