|
1 | | -import { cpSync } from "fs" |
| 1 | +import { cpSync, readFileSync, writeFileSync } from "fs" |
2 | 2 | import { dirname, join } from "path" |
3 | 3 |
|
| 4 | +const dist = join(import.meta.dir, "..", "dist") |
| 5 | + |
| 6 | +// 1. Copy altimate_python_packages |
4 | 7 | const resolved = require.resolve("@altimateai/dbt-integration") |
5 | 8 | const source = join(dirname(resolved), "altimate_python_packages") |
6 | | -const target = join(import.meta.dir, "..", "dist", "altimate_python_packages") |
7 | | - |
8 | | -cpSync(source, target, { recursive: true }) |
| 9 | +cpSync(source, join(dist, "altimate_python_packages"), { recursive: true }) |
9 | 10 | console.log(`Copied altimate_python_packages → dist/`) |
| 11 | + |
| 12 | +// 2. Copy node_python_bridge.py into dist so it lives next to index.js |
| 13 | +// node_python_bridge.py is shipped in dbt-integration's dist |
| 14 | +const bridgePy = join(dirname(resolved), "node_python_bridge.py") |
| 15 | +cpSync(bridgePy, join(dist, "node_python_bridge.py")) |
| 16 | +console.log(`Copied node_python_bridge.py → dist/`) |
| 17 | + |
| 18 | +// 3. Fix the hardcoded __dirname that bun bakes at compile time. |
| 19 | +// Replace it with a runtime resolution so the bridge script is found |
| 20 | +// relative to the built index.js, not the CI runner's node_modules. |
| 21 | +const indexPath = join(dist, "index.js") |
| 22 | +let code = readFileSync(indexPath, "utf8") |
| 23 | +const pattern = /var __dirname\s*=\s*"[^"]*python-bridge[^"]*"/ |
| 24 | +if (pattern.test(code)) { |
| 25 | + code = code.replace(pattern, `var __dirname = import.meta.dirname`) |
| 26 | + writeFileSync(indexPath, code) |
| 27 | + console.log(`Patched __dirname in dist/index.js`) |
| 28 | +} else { |
| 29 | + console.error(`ERROR: could not find python-bridge __dirname to patch — the bundle format may have changed`) |
| 30 | + process.exit(1) |
| 31 | +} |
0 commit comments