Skip to content

Commit 24b37c2

Browse files
committed
fix(cli): resolve SQL directory at runtime for bundled CLI
The sqlDir() function was using __dirname which gets baked in at build time by bundlers (bun/esbuild), causing prepare-db --print-sql to fail when run from npm-installed package. Changes: - Use import.meta.url with fileURLToPath for runtime path resolution - Copy SQL files to dist/sql during build - Add CI smoke test to verify prepare-db --print-sql works
1 parent 68079fc commit 24b37c2

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

.gitlab-ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ cli:node:smoke:
110110
- node ./cli/dist/bin/postgres-ai.js --help
111111
- node ./cli/dist/bin/postgres-ai.js mon status --help
112112
- node ./cli/dist/bin/postgres-ai.js mon targets list --help
113+
# Verify prepare-db --print-sql works (SQL templates are bundled correctly)
114+
- node ./cli/dist/bin/postgres-ai.js prepare-db --print-sql 2>&1 | grep -q "CREATE" || (echo "prepare-db --print-sql failed to output SQL" && exit 1)
113115
- npm install -g ./cli
114116
- echo "prefix=$(npm config get prefix)" && echo "PATH=$PATH"
115117
- command -v postgres-ai && postgres-ai --help

cli/lib/init.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { randomBytes } from "crypto";
2-
import { URL } from "url";
2+
import { URL, fileURLToPath } from "url";
33
import type { ConnectionOptions as TlsConnectionOptions } from "tls";
44
import type { Client as PgClient } from "pg";
55
import * as fs from "fs";
@@ -163,9 +163,15 @@ function sqlDir(): string {
163163
// Handle both development and production paths
164164
// Development: lib/init.ts -> ../sql
165165
// Production (bundled): dist/bin/postgres-ai.js -> ../sql (copied during build)
166+
//
167+
// IMPORTANT: Use import.meta.url instead of __dirname because bundlers (bun/esbuild)
168+
// bake in __dirname at build time, while import.meta.url resolves at runtime.
169+
const currentFile = fileURLToPath(import.meta.url);
170+
const currentDir = path.dirname(currentFile);
171+
166172
const candidates = [
167-
path.resolve(__dirname, "..", "sql"), // bundled: dist/bin -> dist/sql
168-
path.resolve(__dirname, "..", "..", "sql"), // dev from lib: lib -> ../sql
173+
path.resolve(currentDir, "..", "sql"), // bundled: dist/bin -> dist/sql
174+
path.resolve(currentDir, "..", "..", "sql"), // dev from lib: lib -> ../sql
169175
];
170176

171177
for (const candidate of candidates) {

cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"scripts": {
2828
"embed-metrics": "bun run scripts/embed-metrics.ts",
29-
"build": "bun run embed-metrics && bun build ./bin/postgres-ai.ts --outdir ./dist/bin --target node && node -e \"const fs=require('fs');const f='./dist/bin/postgres-ai.js';fs.writeFileSync(f,fs.readFileSync(f,'utf8').replace('#!/usr/bin/env bun','#!/usr/bin/env node'))\"",
29+
"build": "bun run embed-metrics && bun build ./bin/postgres-ai.ts --outdir ./dist/bin --target node && node -e \"const fs=require('fs');const f='./dist/bin/postgres-ai.js';fs.writeFileSync(f,fs.readFileSync(f,'utf8').replace('#!/usr/bin/env bun','#!/usr/bin/env node'))\" && cp -r ./sql ./dist/sql",
3030
"prepublishOnly": "npm run build",
3131
"start": "bun ./bin/postgres-ai.ts --help",
3232
"start:node": "node ./dist/bin/postgres-ai.js --help",

0 commit comments

Comments
 (0)