Skip to content

Commit 8229e65

Browse files
authored
Run Nx + Angular builds from root context (#177)
* run monorepo builds from root context * update gitignore * add 'run' back into 'npm run build' and read build args from env
1 parent b13253c commit 8229e65

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/packages/**/*/dist/
22
/examples/
33
/dist
4+
/packages/@apphosting/publish-dev/.local
5+
/packages/@apphosting/publish-dev/htpasswd
46

57
# NPM
68
node_modules

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

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

1111
const root = process.cwd();
1212

13-
// Determine root of project to build
13+
// TODO(blidd-google): Refactor monorepo logic into separate module
14+
15+
// Determine which project in a monorepo to build. The environment variable will only exist when
16+
// a monorepo has been detected in the parent buildpacks, so it can also be used to determine
17+
// whether the project we are building is in a monorepo setup.
18+
const project = process.env.MONOREPO_PROJECT || "";
19+
20+
// Determine root of project to build.
1421
let projectRoot = root;
15-
if (process.env.MONOREPO_PROJECT && process.env.FIREBASE_APP_DIRECTORY) {
22+
// N.B. We don't want to change directories for monorepo builds, so that the build process can
23+
// locate necessary files outside the project directory (e.g. at the root).
24+
if (process.env.FIREBASE_APP_DIRECTORY && !project) {
1625
projectRoot = projectRoot.concat("/", process.env.FIREBASE_APP_DIRECTORY);
17-
const builder = process.env.MONOREPO_BUILDER || "";
18-
checkMonorepoBuildConditions(builder);
26+
}
27+
28+
// Determine which command to run the build
29+
const cmd = process.env.MONOREPO_COMMAND || DEFAULT_COMMAND;
30+
31+
// Parse args to pass to the build command
32+
let cmdArgs: string[] = [];
33+
if (process.env.MONOREPO_BUILD_ARGS) {
34+
cmdArgs = process.env.MONOREPO_BUILD_ARGS.split(",");
35+
}
36+
37+
// Check build conditions, which vary depending on your project structure (standalone or monorepo)
38+
if (project) {
39+
checkMonorepoBuildConditions(cmd, project);
1940
} else {
2041
await checkStandaloneBuildConditions(projectRoot);
2142
}
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-
}
2743

28-
const outputBundleOptions = await build(projectRoot, cmd);
44+
const outputBundleOptions = await build(projectRoot, cmd, ...cmdArgs);
2945
await generateOutputDirectory(root, outputBundleOptions);
3046

3147
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 {
@@ -67,7 +67,13 @@ export async function checkStandaloneBuildConditions(cwd: string): Promise<void>
6767
/**
6868
* Check if the monorepo build system is using the Angular application builder.
6969
*/
70-
export function checkMonorepoBuildConditions(builder: string): void {
70+
export function checkMonorepoBuildConditions(cmd: string, target: string) {
71+
let builder;
72+
if (cmd === "nx") {
73+
const output = execSync(`npx nx show project ${target}`);
74+
const projectJson = JSON.parse(output.toString());
75+
builder = projectJson.targets.build.executor;
76+
}
7177
if (builder !== REQUIRED_BUILDER) {
7278
throw new Error(
7379
"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. ",
@@ -103,11 +109,12 @@ export function populateOutputBundleOptions(outputPaths: OutputPaths): OutputBun
103109
export const build = (
104110
projectRoot = process.cwd(),
105111
cmd = DEFAULT_COMMAND,
112+
...argv: string[]
106113
): Promise<OutputBundleOptions> =>
107114
new Promise((resolve, reject) => {
108115
// enable JSON build logs for application builder
109116
process.env.NG_BUILD_LOGS_JSON = "1";
110-
const childProcess = spawn(cmd, ["run", "build"], {
117+
const childProcess = spawn(cmd, ["run", "build", ...argv], {
111118
cwd: projectRoot,
112119
shell: true,
113120
stdio: ["inherit", "pipe", "pipe"],

0 commit comments

Comments
 (0)