Files
worldwideview/scripts/dev.mjs
T
Silvertakana b787650997 fix(setup): fix install instructions - .env.local, pnpm dev, correct version requirements (#203)
* fix(setup): write .env.local not .env, fix npm->pnpm in completion message

setup.mjs was writing AUTH_SECRET to the git-tracked .env file instead of
.env.local, creating a security risk for users who commit their fork.
Also fixes the completion message which incorrectly instructed users to run
npm run dev instead of pnpm dev (project uses pnpm@9).

* ci: add multi-OS matrix for pnpm setup, fix stale action versions

Adds ubuntu/windows/macos matrix to test-setup job so the install
instructions are verified on all three platforms on every PR. Fixes
actions/checkout, pnpm/action-setup, and actions/setup-node all
pointing at non-existent @v6. Splits into two jobs: test-setup
(3 OS) and test-dev (ubuntu-only, needs Docker).

* fix(ci): use pnpm run setup not pnpm setup

pnpm setup is pnpm's built-in shell config command (writes to .bashrc).
pnpm run setup runs the project script. All three occurrences fixed.

* fix(dev): load .env.local in dev.mjs before spawning workers

Turbopack workers inherit process.env at spawn time, before Next.js
completes its own .env.local loading. If ENCRYPTION_MASTER_KEY is not
already in process.env when concurrently spawns next dev, the
instrumentation.ts startup check sees it as unset and crashes the server.

Fix: loadEnv('.env.local') in dev.mjs after loading .env so all secrets
are present in process.env before any child processes are spawned.

Also fix CI idempotency test: pnpm setup calls pnpm's built-in shell
config command, not the project setup script. Changed to pnpm run setup.
2026-05-29 11:14:49 +12:00

88 lines
2.4 KiB
JavaScript

/**
* @file dev.mjs
* @description Orchestrator for the local development environment.
* Replaces direct usage of 'concurrently' via CLI to allow for optional,
* cross-platform graceful teardown of the Docker database on exit.
* @module scripts
*/
import concurrently from 'concurrently';
import kill from 'tree-kill/index.js';
import { execSync } from 'child_process';
import fs from 'fs';
import process from 'process';
// Manually parse .env to avoid external dependencies early in the boot process
const loadEnv = (file) => {
try {
if (fs.existsSync(file)) {
const content = fs.readFileSync(file, 'utf8');
content.split('\n').forEach(line => {
const match = line.match(/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/);
if (match) {
const key = match[1];
let value = match[2] || '';
if (value.startsWith('"') && value.endsWith('"')) value = value.slice(1, -1);
if (value.startsWith("'") && value.endsWith("'")) value = value.slice(1, -1);
process.env[key] = value;
}
});
}
} catch (e) {
// Ignore read errors
}
};
loadEnv('.env');
loadEnv('.env.local'); // .env.local secrets take precedence and must be in process.env before workers spawn
const teardownDbOnExit = process.env.WWV_TEARDOWN_DB_ON_EXIT === 'true' || process.env.WWV_TEARDOWN_DB_ON_EXIT === '1';
const { result, commands } = concurrently(
[
{ command: 'pnpm run dev:plugins', name: 'plugins', prefixColor: 'magenta' },
{ command: 'pnpm exec next dev', name: 'next', prefixColor: 'blue' }
],
{
prefix: 'name',
killOthers: ['failure', 'success']
}
);
let isShuttingDown = false;
const shutdown = () => {
if (isShuttingDown) return;
isShuttingDown = true;
console.log('\n🛑 Shutting down dev servers...');
// Forcefully kill the process tree of each command cross-platform
commands.forEach(cmd => {
if (cmd.pid) {
kill(cmd.pid, 'SIGKILL');
}
});
if (teardownDbOnExit) {
console.log('📦 Tearing down local PostgreSQL database...');
try {
execSync('docker compose stop db', { stdio: 'inherit' });
console.log('✅ Local database stopped.');
} catch (e) {
console.error('⚠️ Failed to stop database container.');
}
}
process.exit(0);
};
// Listen for termination signals
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
result.then(
() => process.exit(0),
() => process.exit(1)
);