Skip to content

Commit 6b4f969

Browse files
authored
Add env var section (#211)
* Update utils.ts * lint * update * lint * updates * small fix * add comments * lint * refactor angularVersion * lint * add a test for SSR_PORT variable * change env var availability to enum * update interface.ts * update comments * typo * availability is RUNTIME only * update availability to be runtime only * capitalize availability
1 parent 9f4a101 commit 6b4f969

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,45 @@ describe("build commands", () => {
3535
const expectedFiles = {
3636
".apphosting/dist/browser/browserfile": "",
3737
".apphosting/dist/server/server.mjs": "",
38-
".apphosting/bundle.yaml": `headers: []
39-
redirects: []
40-
rewrites: []
38+
".apphosting/bundle.yaml": `
4139
runCommand: node .apphosting/dist/server/server.mjs
4240
neededDirs:
4341
- .apphosting
4442
staticAssets:
4543
- .apphosting/dist/browser
44+
env: []
4645
`,
4746
};
4847
validateTestFiles(tmpDir, expectedFiles);
4948
});
49+
50+
it("expects SSR_PORT variable is added to bundle.yaml for Angular v17.3.2", async () => {
51+
const { generateOutputDirectory } = await importUtils;
52+
const files = {
53+
"dist/test/browser/browserfile": "",
54+
"dist/test/server/server.mjs": "",
55+
};
56+
generateTestFiles(tmpDir, files);
57+
await generateOutputDirectory(tmpDir, outputBundleOptions, "17.3.2");
58+
59+
const expectedFiles = {
60+
".apphosting/dist/browser/browserfile": "",
61+
".apphosting/dist/server/server.mjs": "",
62+
".apphosting/bundle.yaml": `
63+
runCommand: node .apphosting/dist/server/server.mjs
64+
neededDirs:
65+
- .apphosting
66+
staticAssets:
67+
- .apphosting/dist/browser
68+
env:
69+
- variable: SSR_PORT
70+
value: "8080"
71+
availability: RUNTIME
72+
`,
73+
};
74+
validateTestFiles(tmpDir, expectedFiles);
75+
});
76+
5077
it("test failed validateOutputDirectory", async () => {
5178
const { generateOutputDirectory, validateOutputDirectory } = await importUtils;
5279
const files = {
@@ -94,12 +121,16 @@ function generateTestFiles(baseDir: string, filesToGenerate: Object): void {
94121
});
95122
}
96123

124+
function ignoreBlankLines(text: string) {
125+
return text.replace(/^\s*[\r\n]/gm, "");
126+
}
127+
97128
function validateTestFiles(baseDir: string, expectedFiles: Object): void {
98129
Object.entries(expectedFiles).forEach((file) => {
99130
const fileName = file[0];
100131
const expectedContents = file[1];
101132
const fileToRead = path.join(baseDir, fileName);
102133
const contents = fs.readFileSync(fileToRead).toString();
103-
assert.deepEqual(contents, expectedContents);
134+
assert.deepEqual(ignoreBlankLines(contents), ignoreBlankLines(expectedContents));
104135
});
105136
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ if (!output) {
2020
throw new Error("No output from Angular build command, expecting a build manifest file.");
2121
}
2222
const outputBundleOptions = parseOutputBundleOptions(output);
23-
await generateOutputDirectory(root, outputBundleOptions);
23+
await generateOutputDirectory(root, outputBundleOptions, process.env.ANGULAR_VERSION);
2424

2525
await validateOutputDirectory(outputBundleOptions);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ export interface OutputBundleOptions {
1212
needsServerGenerated: boolean;
1313
}
1414

15+
// Environment variable schema for bundle.yaml outputted by angular adapter
16+
export interface EnvironmentVariable {
17+
variable: string;
18+
value: string;
19+
availability: Availability.RUNTIME; // currently support RUNTIME only
20+
}
21+
22+
// defines whether the environment variable is buildtime, runtime or both
23+
export enum Availability {
24+
BUILDTIME = "BUILDTIME",
25+
RUNTIME = "RUNTIME",
26+
}
27+
1528
// valid manifest schema
1629
export interface ValidManifest {
1730
errors: string[];

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { fileURLToPath } from "url";
55
import { execSync } from "child_process";
66
import { resolve, normalize, relative, dirname, join } from "path";
77
import { stringify as yamlStringify } from "yaml";
8-
import { OutputBundleOptions, OutputPaths, buildManifestSchema } from "./interface.js";
8+
import {
9+
Availability,
10+
EnvironmentVariable,
11+
OutputBundleOptions,
12+
OutputPaths,
13+
buildManifestSchema,
14+
} from "./interface.js";
915
import { createRequire } from "node:module";
1016
import stripAnsi from "strip-ansi";
1117
import { BuildOptions } from "@apphosting/common";
@@ -143,30 +149,46 @@ function extractManifestOutput(output: string): string {
143149
export async function generateOutputDirectory(
144150
cwd: string,
145151
outputBundleOptions: OutputBundleOptions,
152+
angularVersion?: string,
146153
): Promise<void> {
147154
await move(outputBundleOptions.baseDirectory, outputBundleOptions.outputBaseDirectory, {
148155
overwrite: true,
149156
});
150157
if (outputBundleOptions.needsServerGenerated) {
151158
await generateServer(outputBundleOptions);
152159
}
153-
await generateBundleYaml(outputBundleOptions, cwd);
160+
await generateBundleYaml(outputBundleOptions, cwd, angularVersion);
161+
}
162+
163+
// add environment variable to bundle.yaml if needed for specific versions
164+
function addBundleYamlEnvVar(angularVersion?: string): EnvironmentVariable[] {
165+
const runtimeEnvVars: EnvironmentVariable[] = [];
166+
// add env var to solve angular port issue, existing only for Angular v17.3.2 (b/332896115)
167+
if (angularVersion && angularVersion === "17.3.2") {
168+
const ssrPortEnvVar: EnvironmentVariable = {
169+
variable: "SSR_PORT",
170+
value: "8080",
171+
availability: Availability.RUNTIME,
172+
};
173+
runtimeEnvVars.push(ssrPortEnvVar);
174+
}
175+
return runtimeEnvVars;
154176
}
155177

156178
// Generate bundle.yaml
157179
async function generateBundleYaml(
158180
outputBundleOptions: OutputBundleOptions,
159181
cwd: string,
182+
angularVersion?: string,
160183
): Promise<void> {
184+
const runtimeEnvVars = addBundleYamlEnvVar(angularVersion);
161185
await writeFile(
162186
outputBundleOptions.bundleYamlPath,
163187
yamlStringify({
164-
headers: [],
165-
redirects: [],
166-
rewrites: [],
167188
runCommand: `node ${normalize(relative(cwd, outputBundleOptions.serverFilePath))}`,
168189
neededDirs: [normalize(relative(cwd, outputBundleOptions.outputDirectory))],
169190
staticAssets: [normalize(relative(cwd, outputBundleOptions.browserDirectory))],
191+
env: runtimeEnvVars,
170192
}),
171193
);
172194
}

0 commit comments

Comments
 (0)