Skip to content

Commit 85b8992

Browse files
committed
Format
1 parent 63b449e commit 85b8992

File tree

4 files changed

+130
-78
lines changed

4 files changed

+130
-78
lines changed

packages/util/postinstall.js

Lines changed: 97 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,126 @@
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');
421

522
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);
1935
}
36+
}
2037

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) {
3147
console.error(`Contents of "${fileName}" could not be parsed.\n`, e);
3248
return undefined;
33-
});
49+
}
50+
},
51+
e => {
52+
console.error(`Contents of "${fileName}" could not be parsed.\n`, e);
53+
return undefined;
54+
}
55+
);
3456
}
3557

36-
getConfigFromEnv().then((partialConfig) => {
37-
58+
getConfigFromEnv()
59+
.then(partialConfig => {
3860
if (!partialConfig) {
39-
return undefined;
61+
return undefined;
4062
}
4163
// In Firebase App Hosting the config provided to the environment variable is up-to-date and
4264
// "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;
4567
}
46-
const projectId = partialConfig.projectId || "-";
68+
const projectId = partialConfig.projectId || '-';
4769
const appId = partialConfig.appId;
4870
const apiKey = partialConfig.apiKey;
4971
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;
5276
}
53-
77+
5478
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 }));
6389
});
64-
65-
}).then((config) => {
66-
90+
})
91+
.then(config => {
6792
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
7297
}).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
76102
);
77-
103+
78104
// getDefaults() will use this object, rather than fallback to other autoinit suppliers, if it's
79105
// 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;
81108

82109
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';
84113
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+
)
88121
]);
89-
90-
}).then(
91-
() => process.exit(0),
122+
})
123+
.then(
92124
() => process.exit(0),
93-
);
125+
() => process.exit(0)
126+
);

packages/util/rollup.config.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ import pkg from './package.json';
2222
import { emitModulePackageFile } from '../../scripts/build/rollup_emit_module_package_file';
2323

2424
const deps = [
25-
...Object.keys(
26-
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
27-
),
25+
...Object.keys(Object.assign({}, pkg.peerDependencies, pkg.dependencies)),
2826
'./autoinit_env'
2927
];
3028

3129
const buildPlugins = [
3230
typescriptPlugin({ typescript }),
33-
replacePlugin({ './src/autoinit_env': '"@firebase/util/autoinit_env"', delimiters: ["'", "'"], preventAssignment: true })
31+
replacePlugin({
32+
'./src/autoinit_env': '"@firebase/util/autoinit_env"',
33+
delimiters: ["'", "'"],
34+
preventAssignment: true
35+
})
3436
];
3537

3638
const browserBuilds = [
@@ -80,22 +82,22 @@ const nodeBuilds = [
8082
];
8183

8284
const autoinitBuild = [
83-
{
85+
{
8486
input: './src/autoinit_env.ts',
8587
output: {
8688
file: './dist/autoinit_env.js',
87-
format: 'cjs',
89+
format: 'cjs'
8890
},
89-
plugins: buildPlugins,
91+
plugins: buildPlugins
9092
},
91-
{
93+
{
9294
input: './src/autoinit_env.ts',
9395
output: {
9496
file: './dist/autoinit_env.mjs',
95-
format: 'es',
97+
format: 'es'
9698
},
97-
plugins: buildPlugins,
99+
plugins: buildPlugins
98100
}
99-
]
101+
];
100102

101103
export default [...browserBuilds, ...nodeBuilds, ...autoinitBuild];

packages/util/src/autoinit_env.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
import type { FirebaseDefaults } from "./defaults";
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+
import type { FirebaseDefaults } from './defaults';
219

320
// This value is retrieved and hardcoded by the NPM postinstall script
4-
export const postinstallDefaults: FirebaseDefaults|undefined = undefined;
21+
export const postinstallDefaults: FirebaseDefaults | undefined = undefined;

packages/util/src/defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { base64Decode } from './crypt';
1919
import { getGlobal } from './global';
20-
import { postinstallDefaults } from "./autoinit_env";
20+
import { postinstallDefaults } from './autoinit_env';
2121

2222
/**
2323
* Keys for experimental properties on the `FirebaseDefaults` object.

0 commit comments

Comments
 (0)