-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathlocalbuilds.ts
More file actions
90 lines (83 loc) · 3.1 KB
/
localbuilds.ts
File metadata and controls
90 lines (83 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import * as path from "path";
import { BuildConfig, Env } from "../gcp/apphosting";
import { localBuild as localAppHostingBuild } from "@apphosting/build";
import { EnvMap } from "./yaml";
/**
* Triggers a local build of your App Hosting codebase.
*
* This function orchestrates the build process using the App Hosting build adapter.
* It detects the framework (though currently defaults/assumes 'nextjs' in some contexts),
* generates the necessary build artifacts, and returns metadata about the build.
* @param projectRoot - The root directory of the project to build.
* @param framework - The framework to use for the build (e.g., 'nextjs').
* @return A promise that resolves to the build output, including:
* - `outputFiles`: Paths to the generated build artifacts.
* - `annotations`: Metadata annotations relating to the build.
* - `buildConfig`: Configuration derived from the build process (e.g. run commands, environment variables).
*/
export async function localBuild(
projectRoot: string,
framework: string,
env: EnvMap = {},
): Promise<{
outputFiles: string[];
annotations: Record<string, string>;
buildConfig: BuildConfig;
}> {
// We need to inject the environment variables into the process.env
// because the build adapter uses them to build the app.
// We'll restore the original process.env after the build is done.
const originalEnv = { ...process.env };
const projectNodeModules = path.join(projectRoot, "node_modules");
const newNodePath = process.env.NODE_PATH
? `${process.env.NODE_PATH}${path.delimiter}${projectNodeModules}`
: projectNodeModules;
const addedEnv = toProcessEnv(env);
for (const [key, value] of Object.entries(addedEnv)) {
process.env[key] = value;
}
const originalNodePath = process.env.NODE_PATH;
process.env.NODE_PATH = newNodePath;
let apphostingBuildOutput;
try {
apphostingBuildOutput = await localAppHostingBuild(projectRoot, framework);
} finally {
for (const key in process.env) {
if (!(key in originalEnv)) {
delete process.env[key];
}
}
for (const [key, value] of Object.entries(originalEnv)) {
process.env[key] = value;
}
if (originalNodePath !== undefined) {
process.env.NODE_PATH = originalNodePath;
} else {
delete process.env.NODE_PATH;
}
}
const annotations: Record<string, string> = Object.fromEntries(
Object.entries(apphostingBuildOutput.metadata).map(([key, value]) => [key, String(value)]),
);
const discoveredEnv: Env[] | undefined =
apphostingBuildOutput.runConfig.environmentVariables?.map(
({ variable, value, availability }) => ({
variable,
value,
availability,
}),
);
return {
outputFiles: apphostingBuildOutput.outputFiles?.serverApp.include ?? [],
annotations,
buildConfig: {
runCommand: apphostingBuildOutput.runConfig.runCommand,
env: discoveredEnv ?? [],
},
};
}
function toProcessEnv(env: EnvMap): NodeJS.ProcessEnv {
return Object.fromEntries(
Object.entries(env).map(([key, value]) => [key, value.value || ""]),
) as NodeJS.ProcessEnv;
}