Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("build commands", () => {
outputBundleOptions = {
browserDirectory: resolve(tmpDir, "dist", "test", "browser"),
bundleYamlPath: resolve(tmpDir, ".apphosting", "bundle.yaml"),
outputDirectoryBasePath: resolve(tmpDir, ".apphosting"),
serverFilePath: resolve(tmpDir, "dist", "test", "server", "server.mjs"),
needsServerGenerated: false,
};
Expand Down
8 changes: 6 additions & 2 deletions packages/@apphosting/adapter-angular/src/bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
checkBuildConditions,
validateOutputDirectory,
parseOutputBundleOptions,
outputBundleExists,
metaFrameworkOutputBundleExists,
} from "../utils.js";
import { getBuildOptions, runBuild } from "@apphosting/common";

Expand All @@ -21,7 +21,11 @@ if (!output) {
}

const angularVersion = process.env.FRAMEWORK_VERSION || "unspecified";
if (!outputBundleExists()) {
// Frameworks like nitro, analog, nuxt generate the output bundle during their own build process
// when `npm run build` is called which we don't want to overwrite immediately after.
// We only want to overwrite if the existing output is from a previous framework adapter
// build on a plain angular app.
if (!metaFrameworkOutputBundleExists()) {
const outputBundleOptions = parseOutputBundleOptions(output);
const root = process.cwd();
await generateBuildOutput(root, outputBundleOptions, angularVersion);
Expand Down
1 change: 1 addition & 0 deletions packages/@apphosting/adapter-angular/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { URL } from "node:url";
// options to help generate output directory
export interface OutputBundleOptions {
bundleYamlPath: string;
outputDirectoryBasePath: string;
serverFilePath: string;
browserDirectory: string;
needsServerGenerated: boolean;
Expand Down
26 changes: 21 additions & 5 deletions packages/@apphosting/adapter-angular/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import { resolve, normalize, relative, dirname, join } from "path";
import { stringify as yamlStringify } from "yaml";
import { OutputBundleOptions, OutputPaths, buildManifestSchema } from "./interface.js";
import { createRequire } from "node:module";
import { parse as parseYaml } from "yaml";
import stripAnsi from "strip-ansi";
import {
BuildOptions,
OutputBundleConfig,
EnvVarConfig,
Metadata,
Availability,
updateOrCreateGitignore,
} from "@apphosting/common";

// fs-extra is CJS, readJson can't be imported using shorthand
export const { writeFile, move, readJson, mkdir, copyFile, readFileSync, existsSync } = fsExtra;
export const { writeFile, move, readJson, mkdir, copyFile, readFileSync, existsSync, ensureDir } =
fsExtra;

const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -135,6 +138,7 @@ export function populateOutputBundleOptions(outputPaths: OutputPaths): OutputBun
}
return {
bundleYamlPath: resolve(outputBundleDir, "bundle.yaml"),
outputDirectoryBasePath: outputBundleDir,
serverFilePath: resolve(baseDirectory, serverRelativePath, "server.mjs"),
browserDirectory: resolve(baseDirectory, browserRelativePath),
needsServerGenerated,
Expand Down Expand Up @@ -210,6 +214,10 @@ export async function generateBuildOutput(
await generateServer(outputBundleOptions);
}
await generateBundleYaml(outputBundleOptions, cwd, angularVersion);
// generateBundleYaml creates the output directory (if it does not already exist).
// We need to make sure it is gitignored.
const normalizedBundleDir = normalize(relative(cwd, outputBundleOptions.outputDirectoryBasePath));
updateOrCreateGitignore(cwd, [`/${normalizedBundleDir}/`]);
}

// add environment variable to bundle.yaml if needed for specific versions
Expand All @@ -233,7 +241,7 @@ async function generateBundleYaml(
cwd: string,
angularVersion: string,
): Promise<void> {
await mkdir(dirname(opts.bundleYamlPath));
await ensureDir(dirname(opts.bundleYamlPath));
const outputBundle: OutputBundleConfig = {
version: "v1",
runConfig: {
Expand Down Expand Up @@ -270,10 +278,18 @@ export const isMain = (meta: ImportMeta) => {
return process.argv[1] === fileURLToPath(meta.url);
};

export const outputBundleExists = () => {
export const metaFrameworkOutputBundleExists = () => {
const outputBundleDir = resolve(".apphosting");
if (existsSync(outputBundleDir)) {
return true;
const bundleYamlPath = join(outputBundleDir, "bundle.yaml");
if (existsSync(bundleYamlPath)) {
try {
const bundle: OutputBundleConfig = parseYaml(readFileSync(bundleYamlPath, "utf8"));
if (bundle.metadata?.framework && bundle.metadata.framework !== "angular") {
return true;
}
} catch (e) {
logger.debug("Failed to parse bundle.yaml, assuming it can be overwritten", e);
}
}
return false;
};
Loading