diff --git a/package-lock.json b/package-lock.json index cd97d2cc..473c771a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25652,18 +25652,17 @@ } }, "packages/@apphosting/build": { - "version": "0.1.1", + "version": "0.1.3", "license": "Apache-2.0", "dependencies": { - "@apphosting/discover": "*", + "@apphosting/common": "*", "colorette": "^2.0.20", "commander": "^11.1.0", "npm-pick-manifest": "^9.0.0", "ts-node": "^10.9.1" }, "bin": { - "apphosting-local-build": "dist/bin/localbuild.js", - "build": "dist/bin/build.js" + "apphosting-local-build": "dist/bin/localbuild.js" }, "devDependencies": { "@types/commander": "*", @@ -25742,7 +25741,7 @@ } }, "packages/@apphosting/common": { - "version": "0.0.7", + "version": "0.0.8", "license": "Apache-2.0" }, "packages/@apphosting/create": { diff --git a/packages/@apphosting/build/src/bin/localbuild.ts b/packages/@apphosting/build/src/bin/localbuild.ts index 0082c105..89d613fc 100644 --- a/packages/@apphosting/build/src/bin/localbuild.ts +++ b/packages/@apphosting/build/src/bin/localbuild.ts @@ -1,23 +1,12 @@ #! /usr/bin/env node -import { SupportedFrameworks } from "@apphosting/common"; -import { adapterBuild } from "../index.js"; +import { localBuild } from "../index.js"; import { program } from "commander"; program .argument("", "path to the project's root directory") .option("--framework ") .action(async (projectRoot, opts) => { - // TODO(#382): support other apphosting.*.yaml files. - - // TODO(#382): parse apphosting.yaml for environment variables / secrets needed during build time. - if (opts.framework && SupportedFrameworks.includes(opts.framework)) { - // TODO(#382): Skip this if there's a custom build command in apphosting.yaml. - await adapterBuild(projectRoot, opts.framework); - } - - // TODO(#382): Parse apphosting.yaml to set custom run command in bundle.yaml - // TODO(#382): parse apphosting.yaml for environment variables / secrets needed during runtime to include in the bunde.yaml - // TODO(#382): parse apphosting.yaml for runConfig to include in bundle.yaml + await localBuild(projectRoot, opts.framework); }); program.parse(); diff --git a/packages/@apphosting/build/src/index.ts b/packages/@apphosting/build/src/index.ts index 07a0b428..1690d863 100644 --- a/packages/@apphosting/build/src/index.ts +++ b/packages/@apphosting/build/src/index.ts @@ -1,7 +1,34 @@ import promiseSpawn from "@npmcli/promise-spawn"; +import { readFileSync, existsSync } from "fs"; +import { join } from "path"; import { yellow, bgRed, bold } from "colorette"; +import { OutputBundleConfig } from "@apphosting/common"; +import { SupportedFrameworks, Framework } from "@apphosting/common"; +import { parse as parseYaml } from "yaml"; + +export async function localBuild( + projectRoot: string, + framework?: string, +): Promise { + if (framework && SupportedFrameworks.includes(framework as Framework)) { + // TODO(#382): Skip this if there's a custom build command in apphosting.yaml. + await adapterBuild(projectRoot, framework); + const bundleYamlPath = join(projectRoot, ".apphosting", "bundle.yaml"); + if (!existsSync(bundleYamlPath)) { + throw new Error(`Cannot load ${bundleYamlPath} from given path, it doesn't exist`); + } + return parseYaml(readFileSync(bundleYamlPath, "utf8")) as OutputBundleConfig; + } + throw new Error("framework not supported"); +} + +export async function adapterBuild( + projectRoot: string, + framework: string, +): Promise { + // TODO(#382): support other apphosting.*.yaml files. + // TODO(#382): parse apphosting.yaml for environment variables / secrets needed during build time. -export async function adapterBuild(projectRoot: string, framework: string) { // TODO(#382): We are using the latest framework adapter versions, but in the future // we should parse the framework version and use the matching adapter version. const adapterName = `@apphosting/adapter-${framework}`; @@ -30,4 +57,14 @@ export async function adapterBuild(projectRoot: string, framework: string) { shell: true, stdio: "inherit", }); + + const bundleYamlPath = join(projectRoot, ".apphosting", "bundle.yaml"); + if (!existsSync(bundleYamlPath)) { + throw new Error(`Cannot load ${bundleYamlPath} from given path, it doesn't exist`); + } + return parseYaml(readFileSync(bundleYamlPath, "utf8")) as OutputBundleConfig; + + // TODO(#382): Parse apphosting.yaml to set custom run command in bundle.yaml + // TODO(#382): parse apphosting.yaml for runConfig to include in bundle.yaml + // TODO(#382): parse apphosting.yaml for environment variables / secrets needed during runtime to include in the bundle.yaml } diff --git a/packages/@apphosting/common/src/index.ts b/packages/@apphosting/common/src/index.ts index a11f239e..93ee0ac2 100644 --- a/packages/@apphosting/common/src/index.ts +++ b/packages/@apphosting/common/src/index.ts @@ -4,6 +4,7 @@ import * as path from "node:path"; // List of apphosting supported frameworks. export const SupportedFrameworks = ["nextjs", "angular"] as const; +export type Framework = (typeof SupportedFrameworks)[number]; // **** OutputBundleConfig interfaces ****