Skip to content

Commit 4ac42f8

Browse files
committed
ci: add packaging test workflow
1 parent f4f3793 commit 4ac42f8

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

scripts/test-packaging.sh

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
set -e
33

4-
# Argument 1 (Optional): Path to a pre-built tarball.
4+
# Argument 1: Path to a pre-built tarball.
55
# If not provided, the script will run 'npm run build' and 'npm pack' locally.
66
PREBUILT_TARBALL="$1"
77

@@ -15,6 +15,7 @@ trap cleanup EXIT
1515

1616
# Save current directory to resolve relative paths later
1717
START_DIR="$(pwd)"
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1819

1920
if [ -n "$PREBUILT_TARBALL" ]; then
2021
echo "Using prebuilt tarball: $PREBUILT_TARBALL"
@@ -25,8 +26,6 @@ if [ -n "$PREBUILT_TARBALL" ]; then
2526
TARBALL_PATH="$PREBUILT_TARBALL"
2627
else
2728
echo "Building project..."
28-
# Ensure we are in the project root for building
29-
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3029
cd "$SCRIPT_DIR/.."
3130
npm run build
3231

@@ -41,34 +40,10 @@ pushd "$WORK_DIR" > /dev/null
4140
npm init -y > /dev/null
4241
npm install "$TARBALL_PATH" > /dev/null
4342

44-
echo "Generating verification scripts..."
45-
node -e '
46-
const fs = require("fs");
47-
// Read the package.json of the INSTALLED package to verify what was actually packed
48-
const pkg = require("./node_modules/firebase-functions/package.json");
49-
const exports = Object.keys(pkg.exports);
50-
51-
// Filter out non-code entrypoints (e.g. package.json if it were exported)
52-
const entryPoints = exports.filter(e => !e.endsWith(".json"));
53-
54-
let cjsContent = "console.log(\"Verifying CJS exports...\");\n";
55-
let esmContent = "console.log(\"Verifying ESM exports...\");\n";
56-
57-
for (const exp of entryPoints) {
58-
const importPath = exp === "." ? "firebase-functions" : `firebase-functions/${exp.replace("./", "")}`;
59-
cjsContent += `try { require("${importPath}"); console.log("✅ CJS: ${importPath}"); } catch (e) { console.error("❌ CJS Failed: ${importPath}", e); process.exit(1); }\n`;
60-
esmContent += `try { await import("${importPath}"); console.log("✅ ESM: ${importPath}"); } catch (e) { console.error("❌ ESM Failed: ${importPath}", e); process.exit(1); }\n`;
61-
}
62-
63-
fs.writeFileSync("verify-cjs.cjs", cjsContent);
64-
fs.writeFileSync("verify-esm.mjs", esmContent);
65-
'
66-
67-
echo "Running CJS verification..."
68-
node verify-cjs.cjs
69-
70-
echo "Running ESM verification..."
71-
node verify-esm.mjs
43+
echo "Running verification script..."
44+
# Copy the verification script to the work directory to ensure it resolves
45+
# the locally installed 'firebase-functions' package.
46+
cp "$SCRIPT_DIR/verify-exports.mjs" .
47+
node verify-exports.mjs
7248

7349
popd > /dev/null
74-
echo "✨ Packaging test passed!"

scripts/verify-exports.mjs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { createRequire } from 'module';
4+
5+
const require = createRequire(import.meta.url);
6+
7+
// Read the package.json of the INSTALLED package to verify what was actually packed
8+
const pkgPath = path.resolve(process.cwd(), 'node_modules/firebase-functions/package.json');
9+
if (!fs.existsSync(pkgPath)) {
10+
console.error(`❌ Could not find installed package at ${pkgPath}`);
11+
process.exit(1);
12+
}
13+
14+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
15+
const exports = Object.keys(pkg.exports || {});
16+
17+
// Filter out non-code entrypoints (e.g. package.json if it were exported)
18+
const entryPoints = exports.filter(e => !e.endsWith('.json'));
19+
20+
console.log(`Found ${entryPoints.length} entry points to verify.`);
21+
22+
let hasError = false;
23+
24+
async function verify() {
25+
console.log('\n--- Verifying CommonJS (require) ---');
26+
for (const exp of entryPoints) {
27+
const importPath = exp === '.' ? 'firebase-functions' : `firebase-functions/${exp.replace('./', '')}`;
28+
try {
29+
require(importPath);
30+
console.log(`✅ CJS: ${importPath}`);
31+
} catch (e) {
32+
console.error(`❌ CJS Failed: ${importPath}`, e.message);
33+
hasError = true;
34+
}
35+
}
36+
37+
console.log('\n--- Verifying ES Modules (import) ---');
38+
for (const exp of entryPoints) {
39+
const importPath = exp === '.' ? 'firebase-functions' : `firebase-functions/${exp.replace('./', '')}`;
40+
try {
41+
await import(importPath);
42+
console.log(`✅ ESM: ${importPath}`);
43+
} catch (e) {
44+
console.error(`❌ ESM Failed: ${importPath}`, e.message);
45+
hasError = true;
46+
}
47+
}
48+
49+
if (hasError) {
50+
console.error('\n❌ Verification failed with errors.');
51+
process.exit(1);
52+
} else {
53+
console.log('\n✨ All entry points verified successfully!');
54+
}
55+
}
56+
57+
verify();

0 commit comments

Comments
 (0)