|
1 |
| -const { writeFile, readFile } = require("node:fs/promises"); |
2 |
| -const { pathToFileURL } = require("node:url"); |
3 |
| -const { isAbsolute, join } = require("node:path"); |
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +const { writeFile, readFile } = require('node:fs/promises'); |
| 19 | +const { pathToFileURL } = require('node:url'); |
| 20 | +const { isAbsolute, join } = require('node:path'); |
4 | 21 |
|
5 | 22 | 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 |
| - } |
| 23 | + if (!process.env.FIREBASE_WEBAPP_CONFIG) { |
| 24 | + return Promise.resolve(undefined); |
| 25 | + } |
| 26 | + |
| 27 | + // Like FIREBASE_CONFIG (admin autoinit) FIREBASE_WEBAPP_CONFIG can be |
| 28 | + // either a JSON representation of FirebaseOptions or the path to a filename |
| 29 | + if (process.env.FIREBASE_WEBAPP_CONFIG.startsWith('{"')) { |
| 30 | + try { |
| 31 | + return Promise.resolve(JSON.parse(process.env.FIREBASE_WEBAPP_CONFIG)); |
| 32 | + } catch (e) { |
| 33 | + console.error('FIREBASE_WEBAPP_CONFIG could not be parsed.\n', e); |
| 34 | + return Promise.resolve(undefined); |
19 | 35 | }
|
| 36 | + } |
20 | 37 |
|
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) => { |
| 38 | + const fileName = process.env.FIREBASE_WEBAPP_CONFIG; |
| 39 | + const fileURL = pathToFileURL( |
| 40 | + isAbsolute(fileName) ? fileName : join(process.cwd(), fileName) |
| 41 | + ); |
| 42 | + return readFile(fileURL, 'utf-8').then( |
| 43 | + fileContents => { |
| 44 | + try { |
| 45 | + return JSON.parse(fileContents); |
| 46 | + } catch (e) { |
31 | 47 | console.error(`Contents of "${fileName}" could not be parsed.\n`, e);
|
32 | 48 | return undefined;
|
33 |
| - }); |
| 49 | + } |
| 50 | + }, |
| 51 | + e => { |
| 52 | + console.error(`Contents of "${fileName}" could not be parsed.\n`, e); |
| 53 | + return undefined; |
| 54 | + } |
| 55 | + ); |
34 | 56 | }
|
35 | 57 |
|
36 |
| -getConfigFromEnv().then((partialConfig) => { |
37 |
| - |
| 58 | +getConfigFromEnv() |
| 59 | + .then(partialConfig => { |
38 | 60 | if (!partialConfig) {
|
39 |
| - return undefined; |
| 61 | + return undefined; |
40 | 62 | }
|
41 | 63 | // In Firebase App Hosting the config provided to the environment variable is up-to-date and
|
42 | 64 | // "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; |
| 65 | + if (process.env.X_GOOGLE_TARGET_PLATFORM === 'fah') { |
| 66 | + return partialConfig; |
45 | 67 | }
|
46 |
| - const projectId = partialConfig.projectId || "-"; |
| 68 | + const projectId = partialConfig.projectId || '-'; |
47 | 69 | const appId = partialConfig.appId;
|
48 | 70 | const apiKey = partialConfig.apiKey;
|
49 | 71 | if (!appId || !apiKey) {
|
50 |
| - console.error(`Unable to fetch Firebase config, appId and apiKey are required.`); |
51 |
| - return undefined; |
| 72 | + console.error( |
| 73 | + `Unable to fetch Firebase config, appId and apiKey are required.` |
| 74 | + ); |
| 75 | + return undefined; |
52 | 76 | }
|
53 |
| - |
| 77 | + |
54 | 78 | 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 })); |
| 79 | + `https://firebase.googleapis.com/v1alpha/projects/${projectId}/apps/${appId}/webConfig`, |
| 80 | + { headers: { 'x-goog-api-key': apiKey } } |
| 81 | + ).then(response => { |
| 82 | + if (!response.ok) { |
| 83 | + console.error( |
| 84 | + `Unable to fetch Firebase config, API returned ${response.statusText} (${response.status})` |
| 85 | + ); |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + return response.json().then(json => ({ ...json, apiKey })); |
63 | 89 | });
|
64 |
| - |
65 |
| -}).then((config) => { |
66 |
| - |
| 90 | + }) |
| 91 | + .then(config => { |
67 | 92 | 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, |
| 93 | + firestore: process.env.FIRESTORE_EMULATOR_HOST, |
| 94 | + database: process.env.FIREBASE_DATABASE_EMULATOR_HOST, |
| 95 | + storage: process.env.FIREBASE_STORAGE_EMULATOR_HOST, |
| 96 | + auth: process.env.FIREBASE_AUTH_EMULATOR_HOST |
72 | 97 | }).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 |
| 98 | + // We want a falsy value if none of the above are defined |
| 99 | + (current, [key, value]) => |
| 100 | + value ? { ...current, [key]: value } : current, |
| 101 | + undefined |
76 | 102 | );
|
77 |
| - |
| 103 | + |
78 | 104 | // getDefaults() will use this object, rather than fallback to other autoinit suppliers, if it's
|
79 | 105 | // truthy—if we've done nothing here, make it falsy.
|
80 |
| - const defaults = (config || emulatorHosts) ? { config, emulatorHosts } : undefined; |
| 106 | + const defaults = |
| 107 | + config || emulatorHosts ? { config, emulatorHosts } : undefined; |
81 | 108 |
|
82 | 109 | return Promise.all([
|
83 |
| - writeFile(join(__dirname, "dist", "autoinit_env.js"), `'use strict'; |
| 110 | + writeFile( |
| 111 | + join(__dirname, 'dist', 'autoinit_env.js'), |
| 112 | + `'use strict'; |
84 | 113 | Object.defineProperty(exports, '__esModule', { value: true });
|
85 |
| -exports.postinstallDefaults = ${JSON.stringify(defaults)};`), |
86 |
| - writeFile(join(__dirname, "dist", "autoinit_env.mjs"), `const postinstallDefaults = ${JSON.stringify(defaults)}; |
87 |
| -export { postinstallDefaults };`), |
| 114 | +exports.postinstallDefaults = ${JSON.stringify(defaults)};` |
| 115 | + ), |
| 116 | + writeFile( |
| 117 | + join(__dirname, 'dist', 'autoinit_env.mjs'), |
| 118 | + `const postinstallDefaults = ${JSON.stringify(defaults)}; |
| 119 | +export { postinstallDefaults };` |
| 120 | + ) |
88 | 121 | ]);
|
89 |
| - |
90 |
| -}).then( |
91 |
| - () => process.exit(0), |
| 122 | + }) |
| 123 | + .then( |
92 | 124 | () => process.exit(0),
|
93 |
| -); |
| 125 | + () => process.exit(0) |
| 126 | + ); |
0 commit comments