Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/dataqueue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"@modelcontextprotocol/sdk": "^1.26.0",
"croner": "^10.0.1",
"dotenv": "^16.4.5",
"pg": "^8.0.0",
"pg-connection-string": "^2.9.1",
"zod": "^3.25.67"
Expand Down
7 changes: 7 additions & 0 deletions packages/dataqueue/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ function makeDeps() {
error: vi.fn(),
exit: vi.fn(),
spawnSyncImpl: vi.fn(() => makeSpawnSyncReturns(0)),
loadEnvFromPath: vi.fn(),
migrationsDir: '/migrations',
cwd: '/project',
runInitImpl: vi.fn(),
runInstallSkillsImpl: vi.fn(),
runInstallRulesImpl: vi.fn(async () => {}),
Expand Down Expand Up @@ -112,6 +114,11 @@ describe('runCli', () => {
expect(deps.exit).toHaveBeenCalledWith(0);
});

it('calls loadEnvFromPath with env path when --envPath is given', () => {
runCli(['node', 'cli.js', 'migrate', '--envPath', '.env.local'], deps);
expect(deps.loadEnvFromPath).toHaveBeenCalledWith('.env.local');
});

it('passes extra args to spawnSyncImpl', () => {
runCli(['node', 'cli.js', 'migrate', '--foo', 'bar'], deps);
expect(deps.spawnSyncImpl).toHaveBeenCalledWith(
Expand Down
16 changes: 14 additions & 2 deletions packages/dataqueue/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { spawnSync, SpawnSyncReturns } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import { config as loadDotenv } from 'dotenv';
import { InitDeps, runInit } from './init-command.js';
import {
runInstallSkills,
Expand All @@ -19,7 +20,10 @@ export interface CliDeps {
error?: (...args: any[]) => void;
exit?: (code: number) => void;
spawnSyncImpl?: (...args: any[]) => SpawnSyncReturns<any>;
/** Load env vars from a file path (default: dotenv). Path is resolved from cwd. */
loadEnvFromPath?: (filePath: string) => void;
migrationsDir?: string;
cwd?: string;
initDeps?: InitDeps;
runInitImpl?: (deps?: InitDeps) => void;
installSkillsDeps?: InstallSkillsDeps;
Expand All @@ -38,6 +42,10 @@ export function runCli(
error = console.error,
exit = (code: number) => process.exit(code),
spawnSyncImpl = spawnSync,
cwd = process.cwd(),
loadEnvFromPath = (filePath: string) => {
loadDotenv({ path: path.resolve(cwd, filePath) });
},
migrationsDir = path.join(__dirname, '../migrations'),
initDeps,
runInitImpl = runInit,
Expand Down Expand Up @@ -94,11 +102,15 @@ export function runCli(
restArgs.splice(schemaIndex, 2);
}

// Support for --envPath argument
// Support for --envPath argument: load env file so PG_DATAQUEUE_DATABASE is set
// before spawning node-pg-migrate (node-pg-migrate only loads .env if dotenv is
// installed in its context, so we preload here for reliable behavior).
let envPathArg: string[] = [];
const envPathIndex = restArgs.indexOf('--envPath');
if (envPathIndex !== -1 && restArgs[envPathIndex + 1]) {
envPathArg = ['--envPath', restArgs[envPathIndex + 1]];
const envPath = restArgs[envPathIndex + 1];
loadEnvFromPath(envPath);
envPathArg = ['--envPath', envPath];
}

const result: SpawnSyncReturns<any> = spawnSyncImpl(
Expand Down
35 changes: 11 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading