Skip to content

Commit

Permalink
astro adapter poc
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuangwang committed May 22, 2024
1 parent fcc84ce commit 660d420
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
41 changes: 24 additions & 17 deletions src/frameworks/astro/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { sync as spawnSync, spawn } from "cross-spawn";
import { copy, existsSync } from "fs-extra";
import { readFile } from "fs/promises";
import { join } from "path";
import { BuildResult, Discovery, FrameworkType, SupportLevel } from "../interfaces";
import { load } from "js-yaml";
import { BuildResult, Discovery, FrameworkType, SupportLevel, BundleConfig } from "../interfaces";
import { FirebaseError } from "../../error";
import { readJSON, simpleProxy, warnIfCustomBuildScript, getNodeModuleBin } from "../utils";
import { getAstroVersion, getBootstrapScript, getConfig } from "./utils";
Expand All @@ -24,32 +26,37 @@ export async function discover(dir: string): Promise<Discovery | undefined> {

const DEFAULT_BUILD_SCRIPT = ["astro build"];

let bundleConfig: BundleConfig;
async function getBundleConfigs(cwd: string): Promise<BundleConfig> {
if (bundleConfig) {
return bundleConfig;
}

const fileContents = await readFile(join(cwd, ".apphosting", "bundle.yaml"), "utf8");
return load(fileContents);

Check warning on line 36 in src/frameworks/astro/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe return of an `any` typed value
}

export async function build(cwd: string): Promise<BuildResult> {

Check warning on line 39 in src/frameworks/astro/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const cli = getNodeModuleBin("astro", cwd);
await warnIfCustomBuildScript(cwd, name, DEFAULT_BUILD_SCRIPT);
const { output, adapter } = await getConfig(cwd);
const wantsBackend = output !== "static";
if (wantsBackend && adapter?.name !== "@astrojs/node") {
throw new FirebaseError(
"Deploying an Astro application with SSR on Firebase Hosting requires the @astrojs/node adapter in middleware mode. https://docs.astro.build/en/guides/integrations-guide/node/",
);
}
const build = spawnSync(cli, ["build"], { cwd, stdio: "inherit" });
const build = spawnSync("npx", ["@apphosting/adapter-astro"], { cwd, stdio: "inherit" });
if (build.status !== 0) throw new FirebaseError("Unable to build your Astro app");
return { wantsBackend };
const bundleConfigs = await getBundleConfigs(cwd);
return { wantsBackend: bundleConfigs.serverDirectory != null };
}

export async function ɵcodegenPublicDirectory(root: string, dest: string) {

Check warning on line 47 in src/frameworks/astro/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function

Check warning on line 47 in src/frameworks/astro/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const { outDir, output } = await getConfig(root);
// output: "server" in astro.config builds "client" and "server" folders, otherwise assets are in top-level outDir
const assetPath = join(root, outDir, output !== "static" ? "client" : "");
await copy(assetPath, dest);
const bundleConfigs = await getBundleConfigs(root);
for (const assetPath of bundleConfigs.staticAssets) {
await copy(assetPath, dest);
}
}

export async function ɵcodegenFunctionsDirectory(sourceDir: string, destDir: string) {

Check warning on line 54 in src/frameworks/astro/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function

Check warning on line 54 in src/frameworks/astro/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const { outDir } = await getConfig(sourceDir);
const bundleConfigs = await getBundleConfigs(sourceDir);
const packageJson = await readJSON(join(sourceDir, "package.json"));

Check warning on line 56 in src/frameworks/astro/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
await copy(join(sourceDir, outDir, "server"), join(destDir));
if (bundleConfigs.serverDirectory != null) {
await copy(join(sourceDir, bundleConfigs.serverDirectory), join(destDir));
}
return {
packageJson,

Check warning on line 61 in src/frameworks/astro/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
bootstrapScript: getBootstrapScript(),
Expand Down
6 changes: 1 addition & 5 deletions src/frameworks/astro/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { dirname, join, relative } from "path";
import { dirname, join } from "path";
import { findDependency } from "../utils";
import { gte } from "semver";
import { fileURLToPath } from "url";

const { dynamicImport } = require(true && "../../dynamicImport");

Expand Down Expand Up @@ -29,10 +28,7 @@ export async function getConfig(cwd: string) {
config = astroConfig;
}
return {
outDir: relative(cwd, fileURLToPath(config.outDir)),
publicDir: relative(cwd, fileURLToPath(config.publicDir)),
output: config.output,
adapter: config.adapter,
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/frameworks/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export interface BuildResult {
i18n?: boolean;
baseUrl?: string;
}
export interface BundleConfig {
staticAssets: string[];
serverDirectory?: string;
}

export type RequestHandler = (
req: IncomingMessage,
Expand Down

0 comments on commit 660d420

Please sign in to comment.