Skip to content

Commit d0ce18e

Browse files
committed
Move to .js file
1 parent 525a4e2 commit d0ce18e

File tree

6 files changed

+95
-101
lines changed

6 files changed

+95
-101
lines changed

packages/util/autoinit_env.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/util/autoinit_env.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {"config":{"projectId":"web-frameworks-e2e","appId":"1:1087080420931:web:a3151cc6bccd2e1aa1a6f6","databaseURL":"https://web-frameworks-e2e-default-rtdb.firebaseio.com","storageBucket":"web-frameworks-e2e.firebasestorage.app","authDomain":"web-frameworks-e2e.firebaseapp.com","messagingSenderId":"1087080420931","apiKey":"AIzaSyCgZEulMefCoYike_SESqhPW0dTwo5nWxs"}}

packages/util/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"./package.json": "./package.json"
2828
},
2929
"files": [
30-
"dist"
30+
"dist",
31+
"postinstall.js"
3132
],
3233
"scripts": {
3334
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
@@ -44,7 +45,7 @@
4445
"trusted-type-check": "tsec -p tsconfig.json --noEmit",
4546
"api-report": "api-extractor run --local --verbose",
4647
"typings:public": "node ../../scripts/build/use_typings.js ./dist/util-public.d.ts",
47-
"postinstall": "node ./dist/postinstall.js"
48+
"postinstall": "node ./postinstall.js"
4849
},
4950
"license": "Apache-2.0",
5051
"dependencies": {

packages/util/postinstall.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const { writeFile, readFile } = require("node:fs/promises");
2+
const { pathToFileURL } = require("node:url");
3+
const { isAbsolute, join } = require("node:path");
4+
5+
function getConfigFromEnv() {
6+
if (!process.env.FIREBASE_WEBAPP_CONFIG) {
7+
return Promise.resolve(undefined);
8+
}
9+
10+
// Like FIREBASE_CONFIG (admin autoinit) FIREBASE_WEBAPP_CONFIG can be
11+
// either a JSON representation of FirebaseOptions or the path to a filename
12+
if (process.env.FIREBASE_WEBAPP_CONFIG.startsWith("{\"")) {
13+
try {
14+
return Promise.resolve(JSON.parse(process.env.FIREBASE_WEBAPP_CONFIG));
15+
} catch(e) {
16+
console.error("FIREBASE_WEBAPP_CONFIG could not be parsed.\n", e);
17+
return Promise.resolve(undefined);
18+
}
19+
}
20+
21+
const fileName = process.env.FIREBASE_WEBAPP_CONFIG;
22+
const fileURL = pathToFileURL(isAbsolute(fileName) ? fileName : join(process.cwd(), fileName));
23+
return readFile(fileURL, "utf-8").then((fileContents) => {
24+
try {
25+
return JSON.parse(fileContents);
26+
} catch(e) {
27+
console.error(`Contents of "${fileName}" could not be parsed.\n`, e);
28+
return undefined;
29+
}
30+
}, (e) => {
31+
console.error(`Contents of "${fileName}" could not be parsed.\n`, e);
32+
return undefined;
33+
});
34+
}
35+
36+
getConfigFromEnv().then((partialConfig) => {
37+
38+
if (!partialConfig) {
39+
return undefined;
40+
}
41+
// In Firebase App Hosting the config provided to the environment variable is up-to-date and
42+
// "complete" we should not reach out to the webConfig endpoint to freshen it
43+
if (process.env.X_GOOGLE_TARGET_PLATFORM === "fah") {
44+
return partialConfig;
45+
}
46+
const projectId = partialConfig.projectId || "-";
47+
const appId = partialConfig.appId;
48+
const apiKey = partialConfig.apiKey;
49+
if (!appId || !apiKey) {
50+
console.error(`Unable to fetch Firebase config, appId and apiKey are required.`);
51+
return undefined;
52+
}
53+
54+
return fetch(
55+
`https://firebase.googleapis.com/v1alpha/projects/${projectId}/apps/${appId}/webConfig`,
56+
{ headers: { "x-goog-api-key": apiKey } }
57+
).then((response) => {
58+
if (!response.ok) {
59+
console.error(`Unable to fetch Firebase config, API returned ${response.statusText} (${response.status})`);
60+
return undefined;
61+
}
62+
return response.json().then((json) => ({ ...json, apiKey }));
63+
});
64+
65+
}).then((config) => {
66+
67+
const emulatorHosts = Object.entries({
68+
firestore: process.env.FIRESTORE_EMULATOR_HOST,
69+
database: process.env.FIREBASE_DATABASE_EMULATOR_HOST,
70+
storage: process.env.FIREBASE_STORAGE_EMULATOR_HOST,
71+
auth: process.env.FIREBASE_AUTH_EMULATOR_HOST,
72+
}).reduce(
73+
// We want a falsy value if none of the above are defined
74+
(current, [key, value]) => value ? { ...current, [key]: value } : current,
75+
undefined
76+
);
77+
78+
// getDefaults() will use this object, rather than fallback to other autoinit suppliers, if it's
79+
// truthy—if we've done nothing here, make it falsy.
80+
const defaults = (config || emulatorHosts) ? { config, emulatorHosts } : undefined;
81+
82+
return Promise.all([
83+
writeFile(join(__dirname, "autoinit_env.js"), `module.exports = ${JSON.stringify(defaults)}`),
84+
writeFile(join(__dirname, "autoinit_env.mjs"), `export default ${JSON.stringify(defaults)}`),
85+
]);
86+
87+
}).then(
88+
() => process.exit(0),
89+
() => process.exit(0),
90+
);

packages/util/postinstall.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

packages/util/rollup.config.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,6 @@ const autoinitBuild = [
8989
format: 'es',
9090
},
9191
plugins: buildPlugins,
92-
},
93-
{
94-
input: './postinstall.ts',
95-
output: {
96-
file: './dist/postinstall.js',
97-
format: 'cjs',
98-
},
99-
plugins: buildPlugins,
100-
external: () => true,
10192
}
10293
]
10394

0 commit comments

Comments
 (0)