Skip to content

Commit 8eb0113

Browse files
authored
Move all local build logic into a callable function. (#403)
Move all local build logic into a callable function
1 parent fd276b3 commit 8eb0113

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

package-lock.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
#! /usr/bin/env node
2-
import { SupportedFrameworks } from "@apphosting/common";
3-
import { adapterBuild } from "../index.js";
2+
import { localBuild } from "../index.js";
43
import { program } from "commander";
54

65
program
76
.argument("<projectRoot>", "path to the project's root directory")
87
.option("--framework <framework>")
98
.action(async (projectRoot, opts) => {
10-
// TODO(#382): support other apphosting.*.yaml files.
11-
12-
// TODO(#382): parse apphosting.yaml for environment variables / secrets needed during build time.
13-
if (opts.framework && SupportedFrameworks.includes(opts.framework)) {
14-
// TODO(#382): Skip this if there's a custom build command in apphosting.yaml.
15-
await adapterBuild(projectRoot, opts.framework);
16-
}
17-
18-
// TODO(#382): Parse apphosting.yaml to set custom run command in bundle.yaml
19-
// TODO(#382): parse apphosting.yaml for environment variables / secrets needed during runtime to include in the bunde.yaml
20-
// TODO(#382): parse apphosting.yaml for runConfig to include in bundle.yaml
9+
await localBuild(projectRoot, opts.framework);
2110
});
2211

2312
program.parse();

packages/@apphosting/build/src/index.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
import promiseSpawn from "@npmcli/promise-spawn";
2+
import { readFileSync, existsSync } from "fs";
3+
import { join } from "path";
24
import { yellow, bgRed, bold } from "colorette";
5+
import { OutputBundleConfig } from "@apphosting/common";
6+
import { SupportedFrameworks, Framework } from "@apphosting/common";
7+
import { parse as parseYaml } from "yaml";
8+
9+
export async function localBuild(
10+
projectRoot: string,
11+
framework?: string,
12+
): Promise<OutputBundleConfig> {
13+
if (framework && SupportedFrameworks.includes(framework as Framework)) {
14+
// TODO(#382): Skip this if there's a custom build command in apphosting.yaml.
15+
await adapterBuild(projectRoot, framework);
16+
const bundleYamlPath = join(projectRoot, ".apphosting", "bundle.yaml");
17+
if (!existsSync(bundleYamlPath)) {
18+
throw new Error(`Cannot load ${bundleYamlPath} from given path, it doesn't exist`);
19+
}
20+
return parseYaml(readFileSync(bundleYamlPath, "utf8")) as OutputBundleConfig;
21+
}
22+
throw new Error("framework not supported");
23+
}
24+
25+
export async function adapterBuild(
26+
projectRoot: string,
27+
framework: string,
28+
): Promise<OutputBundleConfig> {
29+
// TODO(#382): support other apphosting.*.yaml files.
30+
// TODO(#382): parse apphosting.yaml for environment variables / secrets needed during build time.
331

4-
export async function adapterBuild(projectRoot: string, framework: string) {
532
// TODO(#382): We are using the latest framework adapter versions, but in the future
633
// we should parse the framework version and use the matching adapter version.
734
const adapterName = `@apphosting/adapter-${framework}`;
@@ -30,4 +57,14 @@ export async function adapterBuild(projectRoot: string, framework: string) {
3057
shell: true,
3158
stdio: "inherit",
3259
});
60+
61+
const bundleYamlPath = join(projectRoot, ".apphosting", "bundle.yaml");
62+
if (!existsSync(bundleYamlPath)) {
63+
throw new Error(`Cannot load ${bundleYamlPath} from given path, it doesn't exist`);
64+
}
65+
return parseYaml(readFileSync(bundleYamlPath, "utf8")) as OutputBundleConfig;
66+
67+
// TODO(#382): Parse apphosting.yaml to set custom run command in bundle.yaml
68+
// TODO(#382): parse apphosting.yaml for runConfig to include in bundle.yaml
69+
// TODO(#382): parse apphosting.yaml for environment variables / secrets needed during runtime to include in the bundle.yaml
3370
}

packages/@apphosting/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from "node:path";
44

55
// List of apphosting supported frameworks.
66
export const SupportedFrameworks = ["nextjs", "angular"] as const;
7+
export type Framework = (typeof SupportedFrameworks)[number];
78

89
// **** OutputBundleConfig interfaces ****
910

0 commit comments

Comments
 (0)