Skip to content

Commit f26f57f

Browse files
committed
run monorepo builds from root context
1 parent ca32259 commit f26f57f

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

packages/@apphosting/adapter-angular/src/bin/build.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@ import {
1010

1111
const root = process.cwd();
1212

13-
// Determine root of project to build
13+
// Determine which build runner to use
14+
let cmd = process.env.MONOREPO_COMMAND || DEFAULT_COMMAND;
15+
16+
// Read environment variable (only relevant for monorepos with multiple targets)
17+
let target = process.env.MONOREPO_PROJECT || "";
18+
19+
// Determine root of project to build.
1420
let projectRoot = root;
15-
if (process.env.MONOREPO_PROJECT && process.env.FIREBASE_APP_DIRECTORY) {
21+
// N.B. We don't want to change directories for monorepo builds, so that the build process can
22+
// locate necessary files outside the project directory (e.g. at the root).
23+
if (process.env.FIREBASE_APP_DIRECTORY && !target) {
1624
projectRoot = projectRoot.concat("/", process.env.FIREBASE_APP_DIRECTORY);
17-
const builder = process.env.MONOREPO_BUILDER || "";
18-
checkMonorepoBuildConditions(builder);
25+
}
26+
27+
// Check build conditions, which vary depending on your project structure (standalone or monorepo)
28+
if (target) {
29+
checkMonorepoBuildConditions(cmd, target);
1930
} else {
2031
await checkStandaloneBuildConditions(projectRoot);
2132
}
22-
// Determine which build runner to use
23-
let cmd = DEFAULT_COMMAND;
24-
if (process.env.MONOREPO_COMMAND) {
25-
cmd = process.env.MONOREPO_COMMAND;
26-
}
2733

28-
const outputBundleOptions = await build(projectRoot, cmd);
34+
const outputBundleOptions = await build(projectRoot, cmd, target);
2935
await generateOutputDirectory(root, outputBundleOptions);
3036

3137
await validateOutputDirectory(outputBundleOptions);

packages/@apphosting/adapter-angular/src/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fsExtra from "fs-extra";
22
import logger from "firebase-functions/logger";
33

44
import { fileURLToPath } from "url";
5-
import { spawn } from "child_process";
5+
import { spawn, execSync } from "child_process";
66
import { resolve, normalize, relative, dirname, join } from "path";
77
import { stringify as yamlStringify } from "yaml";
88
import {
@@ -64,7 +64,13 @@ export async function checkStandaloneBuildConditions(cwd: string): Promise<void>
6464
/**
6565
* Check if the monorepo build system is using the Angular application builder.
6666
*/
67-
export function checkMonorepoBuildConditions(builder: string): void {
67+
export function checkMonorepoBuildConditions(cmd: string, target: string) {
68+
let builder;
69+
if (cmd === "nx") {
70+
const output = execSync(`npx nx show project ${target}`);
71+
const projectJson = JSON.parse(output.toString());
72+
builder = projectJson.targets.build.executor;
73+
}
6874
if (builder !== REQUIRED_BUILDER) {
6975
throw new Error(
7076
"Only the Angular application builder is supported. Please refer to https://angular.dev/tools/cli/esbuild#for-existing-applications guide to upgrade your builder to the Angular application builder. ",
@@ -100,11 +106,12 @@ export function populateOutputBundleOptions(outputPaths: OutputPaths): OutputBun
100106
export const build = (
101107
projectRoot = process.cwd(),
102108
cmd = DEFAULT_COMMAND,
109+
...argv: string[]
103110
): Promise<OutputBundleOptions> =>
104111
new Promise((resolve, reject) => {
105112
// enable JSON build logs for application builder
106113
process.env.NG_BUILD_LOGS_JSON = "1";
107-
const childProcess = spawn(cmd, ["run", "build"], {
114+
const childProcess = spawn(cmd, ["build", ...argv], {
108115
cwd: projectRoot,
109116
shell: true,
110117
stdio: ["inherit", "pipe", "pipe"],

0 commit comments

Comments
 (0)