diff --git a/.gitignore b/.gitignore index b92ea225..2d64a7f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ /packages/**/*/dist/ /examples/ /dist +/packages/@apphosting/publish-dev/.local +/packages/@apphosting/publish-dev/htpasswd # NPM node_modules diff --git a/packages/@apphosting/adapter-angular/src/bin/build.ts b/packages/@apphosting/adapter-angular/src/bin/build.ts index 9b70c5a4..666c2eee 100644 --- a/packages/@apphosting/adapter-angular/src/bin/build.ts +++ b/packages/@apphosting/adapter-angular/src/bin/build.ts @@ -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); diff --git a/packages/@apphosting/adapter-angular/src/utils.ts b/packages/@apphosting/adapter-angular/src/utils.ts index 3938edf6..5aa51653 100644 --- a/packages/@apphosting/adapter-angular/src/utils.ts +++ b/packages/@apphosting/adapter-angular/src/utils.ts @@ -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 { @@ -67,7 +67,13 @@ export async function checkStandaloneBuildConditions(cwd: string): Promise /** * 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. ", @@ -103,11 +109,12 @@ export function populateOutputBundleOptions(outputPaths: OutputPaths): OutputBun export const build = ( projectRoot = process.cwd(), cmd = DEFAULT_COMMAND, + ...argv: string[] ): Promise => 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"],