Skip to content

Commit 60278c4

Browse files
committed
Separate adapter builds into helper function.
1 parent 62ec239 commit 60278c4

File tree

3 files changed

+50
-34
lines changed

3 files changed

+50
-34
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { spawn } from "child_process";
2+
import { program } from "commander";
3+
import { yellow, bgRed, bold } from "colorette";
4+
5+
export async function adapterBuild(framework: string, projectRoot: string) {
6+
// TODO(#382): We are using the latest framework adapter versions, but in the future
7+
// we should parse the framework version and use the matching adapter version.
8+
const adapterName = `@apphosting/adapter-${framework}`;
9+
const packumentResponse = await fetch(`https://registry.npmjs.org/${adapterName}`);
10+
if (!packumentResponse.ok) throw new Error(`Something went wrong fetching ${adapterName}`);
11+
const packument = await packumentResponse.json();
12+
const adapterVersion = packument?.["dist-tags"]?.["latest"];
13+
if (!adapterVersion) {
14+
throw new Error(`Could not find 'latest' dist-tag for ${adapterName}`);
15+
}
16+
// TODO(#382): should check for existence of adapter in app's package.json and use that version instead.
17+
18+
console.log(" 🔥", bgRed(` ${adapterName}@${yellow(bold(adapterVersion))} `), "\n");
19+
20+
const buildCommand = `apphosting-adapter-${framework}-build`;
21+
await new Promise<void>((resolve, reject) => {
22+
const child = spawn("npx", ["-y", "-p", `${adapterName}@${adapterVersion}`, buildCommand], {
23+
cwd: projectRoot,
24+
shell: true,
25+
stdio: "inherit",
26+
});
27+
28+
child.on("exit", (code) => {
29+
if (code !== 0) {
30+
reject(new Error(`framework adapter build failed with error code ${code}.`));
31+
}
32+
resolve();
33+
});
34+
});
35+
}

packages/@apphosting/build/src/bin/localbuild.ts

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,23 @@
22
import { spawn } from "child_process";
33
import { program } from "commander";
44
import { yellow, bgRed, bold } from "colorette";
5+
import { SupportedFrameworks } from "@apphosting/common";
6+
import { adapterBuild } from "../adapter-builds.js";
57

6-
// TODO(#382): add framework option later or incorporate micro-discovery.
7-
// TODO(#382): parse apphosting.yaml for environment variables / secrets.
8-
// TODO(#382): parse apphosting.yaml for runConfig and include in buildSchema
8+
// TODO(#382): parse apphosting.yaml for environment variables / secrets needed during build time.
99
// TODO(#382): Support custom build and run commands (parse and pass run command to build schema).
10-
program
11-
.argument("<projectRoot>", "path to the project's root directory")
12-
.action(async (projectRoot: string) => {
13-
const framework = "nextjs";
14-
// TODO(#382): We are using the latest framework adapter versions, but in the future
15-
// we should parse the framework version and use the matching adapter version.
16-
const adapterName = `@apphosting/adapter-nextjs`;
17-
const packumentResponse = await fetch(`https://registry.npmjs.org/${adapterName}`);
18-
if (!packumentResponse.ok) throw new Error(`Something went wrong fetching ${adapterName}`);
19-
const packument = await packumentResponse.json();
20-
const adapterVersion = packument?.["dist-tags"]?.["latest"];
21-
if (!adapterVersion) {
22-
throw new Error(`Could not find 'latest' dist-tag for ${adapterName}`);
23-
}
24-
// TODO(#382): should check for existence of adapter in app's package.json and use that version instead.
2510

26-
console.log(" 🔥", bgRed(` ${adapterName}@${yellow(bold(adapterVersion))} `), "\n");
11+
// TODO(#382): parse apphosting.yaml for environment variables / secrets needed during runtime to include in the bunde.yaml
12+
// TODO(#382): parse apphosting.yaml for runConfig to include in bundle.yaml
2713

28-
const buildCommand = `apphosting-adapter-${framework}-build`;
29-
await new Promise<void>((resolve, reject) => {
30-
const child = spawn("npx", ["-y", "-p", `${adapterName}@${adapterVersion}`, buildCommand], {
31-
cwd: projectRoot,
32-
shell: true,
33-
stdio: "inherit",
34-
});
14+
program
15+
.argument("<projectRoot>", "path to the project's root directory")
16+
.argument("<framework>", "the framework to build for")
17+
.action(async (projectRoot: string, framework: string) => {
3518

36-
child.on("exit", (code) => {
37-
if (code !== 0) {
38-
reject(new Error(`framework adapter build failed with error code ${code}.`));
39-
}
40-
resolve();
41-
});
42-
});
43-
// TODO(#382): parse bundle.yaml and apphosting.yaml and output a buildschema somewhere.
19+
if (SupportedFrameworks.includes(framework)) {
20+
adapterBuild(framework, projectRoot)
21+
}
4422
});
4523

4624
program.parse();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { spawn } from "child_process";
22
import * as fs from "node:fs";
33
import * as path from "node:path";
44

5+
// List of apphosting supported frameworks.
6+
export const SupportedFrameworks = ['nextjs', 'angular'];
7+
58
// Output bundle metadata specifications to be written to bundle.yaml
69
export interface OutputBundleConfig {
710
version: "v1";

0 commit comments

Comments
 (0)