Skip to content

Run Nx + Angular builds from root context #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/packages/**/*/dist/
/examples/
/dist
/packages/@apphosting/publish-dev/.local
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be leftover from when you tested things locally?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this in case anyone wanted to use the local adapter publishing workflow, which will generate this directory to store downloaded npm modules.

/packages/@apphosting/publish-dev/htpasswd

# NPM
node_modules
Expand Down
36 changes: 26 additions & 10 deletions packages/@apphosting/adapter-angular/src/bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,38 @@ import {

const root = process.cwd();

// Determine root of project to build
// TODO(blidd-google): Refactor monorepo logic into separate module

// Determine which project in a monorepo to build. The environment variable will only exist when
// a monorepo has been detected in the parent buildpacks, so it can also be used to determine
// whether the project we are building is in a monorepo setup.
const project = process.env.MONOREPO_PROJECT || "";

// Determine root of project to build.
let projectRoot = root;
if (process.env.MONOREPO_PROJECT && process.env.FIREBASE_APP_DIRECTORY) {
// N.B. We don't want to change directories for monorepo builds, so that the build process can
// locate necessary files outside the project directory (e.g. at the root).
if (process.env.FIREBASE_APP_DIRECTORY && !project) {
projectRoot = projectRoot.concat("/", process.env.FIREBASE_APP_DIRECTORY);
const builder = process.env.MONOREPO_BUILDER || "";
checkMonorepoBuildConditions(builder);
}

// Determine which command to run the build
const cmd = process.env.MONOREPO_COMMAND || DEFAULT_COMMAND;

// Parse args to pass to the build command
let cmdArgs: string[] = [];
if (process.env.MONOREPO_BUILD_ARGS) {
cmdArgs = process.env.MONOREPO_BUILD_ARGS.split(",");
}

// Check build conditions, which vary depending on your project structure (standalone or monorepo)
if (project) {
checkMonorepoBuildConditions(cmd, project);
} else {
await checkStandaloneBuildConditions(projectRoot);
}
// Determine which build runner to use
let cmd = DEFAULT_COMMAND;
if (process.env.MONOREPO_COMMAND) {
cmd = process.env.MONOREPO_COMMAND;
}

const outputBundleOptions = await build(projectRoot, cmd);
const outputBundleOptions = await build(projectRoot, cmd, ...cmdArgs);
await generateOutputDirectory(root, outputBundleOptions);

await validateOutputDirectory(outputBundleOptions);
13 changes: 10 additions & 3 deletions packages/@apphosting/adapter-angular/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fsExtra from "fs-extra";
import logger from "firebase-functions/logger";

import { fileURLToPath } from "url";
import { spawn } from "child_process";
import { spawn, execSync } from "child_process";
import { resolve, normalize, relative, dirname, join } from "path";
import { stringify as yamlStringify } from "yaml";
import {
Expand Down Expand Up @@ -67,7 +67,13 @@ export async function checkStandaloneBuildConditions(cwd: string): Promise<void>
/**
* Check if the monorepo build system is using the Angular application builder.
*/
export function checkMonorepoBuildConditions(builder: string): void {
export function checkMonorepoBuildConditions(cmd: string, target: string) {
let builder;
if (cmd === "nx") {
const output = execSync(`npx nx show project ${target}`);
const projectJson = JSON.parse(output.toString());
builder = projectJson.targets.build.executor;
}
if (builder !== REQUIRED_BUILDER) {
throw new Error(
"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. ",
Expand Down Expand Up @@ -103,11 +109,12 @@ export function populateOutputBundleOptions(outputPaths: OutputPaths): OutputBun
export const build = (
projectRoot = process.cwd(),
cmd = DEFAULT_COMMAND,
...argv: string[]
): Promise<OutputBundleOptions> =>
new Promise((resolve, reject) => {
// enable JSON build logs for application builder
process.env.NG_BUILD_LOGS_JSON = "1";
const childProcess = spawn(cmd, ["run", "build"], {
const childProcess = spawn(cmd, ["run", "build", ...argv], {
cwd: projectRoot,
shell: true,
stdio: ["inherit", "pipe", "pipe"],
Expand Down
Loading