Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5155348545 | ||
|
|
5fbfe8954b | ||
|
|
1194be67c4 |
No files matched your search
@@ -24,20 +24,29 @@ If you create a worktree inside a subdirectory of the current repo (e.g., `.agen
|
||||
Do not use raw `git worktree add`. Always use the `git-wt` alias provided by Worktrunk. It is pre-configured via `.config/wt.toml` to:
|
||||
- Automatically place the worktree in the correct sibling path.
|
||||
- Automatically run `pnpm install`.
|
||||
- Automatically copy `.env` and `.env.local` files so the environment works immediately.
|
||||
- Automatically copy `.env` files so the environment works immediately.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> **For AI Agents:** You MUST append the `--yes` flag when creating worktrees to bypass the interactive approval prompt for these hooks, otherwise the command will fail in your non-interactive shell.
|
||||
|
||||
### 3. Database Lifecycle & Port Assignment
|
||||
Each worktree gets a deterministic PostgreSQL port assigned automatically based on its directory hash (to avoid collisions between multiple running worktrees).
|
||||
|
||||
> [!CAUTION]
|
||||
> **Orphaned Volumes:** You MUST use `git-wt remove` to delete worktrees. This triggers a `pre-remove` hook that cleanly tears down the Docker database. If you manually delete the folder (`rm -rf`), the Docker volume is orphaned. If this happens, run `pnpm run db:prune` in the main repository to garbage collect orphaned database volumes.
|
||||
|
||||
## Quick Reference
|
||||
|
||||
| Action | Command | Details |
|
||||
| :--- | :--- | :--- |
|
||||
| **Create Worktree** | `git-wt switch --create <branch>` | Provisions the sibling directory, branch, env files, and dependencies. |
|
||||
| **Create Worktree** | `git-wt switch --create <branch> --yes` | Provisions the sibling directory, branch, env files, and dependencies. |
|
||||
| **Delete Worktree** | `git-wt remove` | Deletes the worktree and the associated branch cleanly. Run from within the worktree. |
|
||||
|
||||
## Implementation Steps
|
||||
|
||||
1. Run the create command from the main repository:
|
||||
1. Run the create command from the main repository (always include `--yes`):
|
||||
```bash
|
||||
git-wt switch --create feature/my-new-idea
|
||||
git-wt switch --create feature/my-new-idea --yes
|
||||
```
|
||||
2. **CRITICAL:** Change your working directory to the newly created worktree before editing files. Worktrunk creates it as a sibling.
|
||||
```bash
|
||||
|
||||
+4
-1
@@ -2,4 +2,7 @@
|
||||
install_deps = "pnpm install"
|
||||
|
||||
[[pre-start]]
|
||||
copy_env = """powershell -Command "if (Test-Path '../worldwideview/.env') { Copy-Item '../worldwideview/.env' '.env' -Force }; if (Test-Path '../worldwideview/.env.local') { Copy-Item '../worldwideview/.env.local' '.env.local' -Force }""""
|
||||
copy_env = """powershell -Command "if (Test-Path '../worldwideview/.env') { Copy-Item '../worldwideview/.env' '.env' -Force }""""
|
||||
|
||||
[[pre-remove]]
|
||||
docker_clean = "docker compose down -v"
|
||||
@@ -55,6 +55,11 @@ NEXT_PUBLIC_DEMO_DEFAULT_PLUGINS=
|
||||
# Edition: "local" | "cloud" | "demo"
|
||||
NEXT_PUBLIC_WWV_EDITION=local
|
||||
|
||||
# Optional Database Teardown
|
||||
# If set to true, terminating 'pnpm run dev' (Ctrl+C) will automatically
|
||||
# stop the local database container (freeing up port 5432).
|
||||
WWV_TEARDOWN_DB_ON_EXIT=false
|
||||
|
||||
# Demo Admin Secret (demo edition only)
|
||||
# Allows operator plugin management on the demo instance via x-wwv-admin-secret header.
|
||||
# Generate any random string. Only set this on the demo deployment.
|
||||
|
||||
@@ -30,6 +30,9 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Setup environment
|
||||
run: cp .env.example .env
|
||||
|
||||
- name: Test predev setup
|
||||
# Tests that Docker DB boots, port mappings work, Prisma pushes schema, and plugins sync
|
||||
run: pnpm run predev
|
||||
|
||||
@@ -208,7 +208,9 @@ Refer to these skill documents for specialized tasks:
|
||||
- **Workflow**: You **MUST** use the `/commit` workflow before every git commit to ensure proper semantic versioning bumps.
|
||||
- **Required Checks**: Ensure `pnpm test` and `pnpm build` complete successfully before proposing a merge.
|
||||
- **Review Process**: Use `/pr-review` to conduct a comprehensive multi-role review on any pull request.
|
||||
- **Git Worktrees**: We use [Worktrunk](https://github.com/max-sixty/worktrunk) to manage parallel branches and avoid relative path breakage in the monorepo. Use `git-wt switch --create <branch>` to provision isolated sibling worktrees automatically configured with `.env` files and `pnpm install`. Use `git-wt remove` to clean up.
|
||||
- **Git Worktrees & Database Lifecycle**: We use [Worktrunk](https://github.com/max-sixty/worktrunk) to manage parallel branches and avoid relative path breakage. `git-wt switch --create <branch>` provisions isolated sibling worktrees configured with `.env` files and `pnpm install`. The database port is assigned deterministically based on the directory hash.
|
||||
> [!CAUTION]
|
||||
> You MUST use `git-wt remove` to clean up worktrees. Manually deleting a worktree folder (`rm -rf`) will orphan the associated PostgreSQL Docker volume. If this happens, you must manually run `pnpm run db:prune` in the main repository to reclaim disk space.
|
||||
|
||||
---
|
||||
|
||||
|
||||
+1
-1
@@ -43,7 +43,7 @@ services:
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: worldwideview
|
||||
ports:
|
||||
- "5432:5432"
|
||||
- "${WWV_DB_PORT:-5432}:5432"
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
|
||||
+4
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "worldwideview",
|
||||
"version": "2.11.0",
|
||||
"version": "2.13.1",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"packageManager": "[email protected]",
|
||||
@@ -8,7 +8,7 @@
|
||||
"setup": "node scripts/setup.mjs",
|
||||
"test-worktree": "node scripts/test-worktree.mjs",
|
||||
"clean": "node -e \"const fs=require('fs');fs.rmSync('.next',{recursive:true,force:true})\"",
|
||||
"dev": "concurrently \"pnpm run dev:plugins\" \"next dev\"",
|
||||
"dev": "node scripts/dev.mjs",
|
||||
"dev:all": "concurrently -n \"dev,backends\" -c \"magenta,blue\" \"pnpm dev\" \"pnpm dev:backends\"",
|
||||
"dev:backends": "docker compose --profile engine up wwv-data-engine wwv-redis",
|
||||
"dev:plugins": "node scripts/watch-local-plugins.mjs",
|
||||
@@ -21,6 +21,7 @@
|
||||
"test:mutate": "stryker run",
|
||||
"lint": "eslint .",
|
||||
"db:reset": "npx prisma migrate reset --force",
|
||||
"db:prune": "node scripts/db-prune.mjs",
|
||||
"generate": "prisma generate"
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -78,6 +79,7 @@
|
||||
"fast-check": "^4.8.0",
|
||||
"jsdom": "^28.1.0",
|
||||
"rollup-plugin-external-globals": "^0.13.0",
|
||||
"tree-kill": "^1.2.2",
|
||||
"tsx": "^4.21.0",
|
||||
"typescript": "^5",
|
||||
"vite": "^8.0.12",
|
||||
|
||||
Generated
+3
@@ -172,6 +172,9 @@ importers:
|
||||
rollup-plugin-external-globals:
|
||||
specifier: ^0.13.0
|
||||
version: 0.13.0([email protected])
|
||||
tree-kill:
|
||||
specifier: ^1.2.2
|
||||
version: 1.2.2
|
||||
tsx:
|
||||
specifier: ^4.21.0
|
||||
version: 4.21.0
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
import { execSync } from 'child_process';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import crypto from 'crypto';
|
||||
|
||||
// Load environment variables manually since dotenv might not be installed globally
|
||||
/**
|
||||
@@ -46,6 +47,32 @@ if (skipLocalDb) {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Deterministic Port Assignment
|
||||
const cwd = process.cwd();
|
||||
const folderName = path.basename(cwd);
|
||||
let port = 5432; // Default for main repo
|
||||
|
||||
if (folderName !== 'worldwideview') {
|
||||
const hash = crypto.createHash('sha256').update(cwd).digest('hex');
|
||||
const portOffset = parseInt(hash.substring(0, 4), 16) % 1000;
|
||||
port = 5433 + portOffset;
|
||||
}
|
||||
|
||||
process.env.WWV_DB_PORT = port.toString();
|
||||
console.log(`🔌 Assigned deterministic database port: ${port}`);
|
||||
|
||||
// Rewrite DATABASE_URL in .env if it exists
|
||||
const envPath = path.resolve(cwd, '.env');
|
||||
if (fs.existsSync(envPath)) {
|
||||
let envContent = fs.readFileSync(envPath, 'utf8');
|
||||
const urlRegex = /(DATABASE_URL\s*=\s*["']?postgresql:\/\/[^:]+:[^@]+@localhost:)(\d+)(\/.*)/;
|
||||
if (urlRegex.test(envContent)) {
|
||||
envContent = envContent.replace(urlRegex, `$1${port}$3`);
|
||||
fs.writeFileSync(envPath, envContent, 'utf8');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
console.log('🚀 Checking local PostgreSQL database...');
|
||||
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* @file db-prune.mjs
|
||||
* @description Garbage collects orphaned Docker volumes from deleted git worktrees.
|
||||
* Resolves the issue where manually deleting a worktree folder (rm -rf) leaves
|
||||
* behind persistent database and data volumes.
|
||||
*/
|
||||
|
||||
import { execSync } from 'child_process';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
console.log('🧹 Starting Garbage Collection for orphaned WorldWideView Docker volumes...');
|
||||
|
||||
try {
|
||||
// Get all docker volumes
|
||||
const volumesOutput = execSync('docker volume ls --format "{{.Name}}"').toString();
|
||||
const volumes = volumesOutput.split('\n').map(v => v.trim()).filter(Boolean);
|
||||
|
||||
// Identify WorldWideView volumes
|
||||
const wwvVolumes = volumes.filter(v =>
|
||||
(v.startsWith('worldwideview') && v.endsWith('_postgres-data')) ||
|
||||
(v.startsWith('worldwideview') && v.endsWith('_wwv-data'))
|
||||
);
|
||||
|
||||
if (wwvVolumes.length === 0) {
|
||||
console.log('✅ No WorldWideView volumes found.');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Get active worktree folders in the parent directory
|
||||
const parentDir = path.resolve(process.cwd(), '..');
|
||||
const folders = fs.readdirSync(parentDir, { withFileTypes: true })
|
||||
.filter(dirent => dirent.isDirectory() && dirent.name.startsWith('worldwideview'))
|
||||
.map(dirent => dirent.name);
|
||||
|
||||
// Compute their expected Docker project names (lowercase, alphanumeric only)
|
||||
// Docker Compose defaults to this sanitization for project names based on directories
|
||||
const activeProjectNames = new Set(
|
||||
folders.map(folder => folder.replace(/[^a-zA-Z0-9]/g, '').toLowerCase())
|
||||
);
|
||||
|
||||
let orphanedCount = 0;
|
||||
|
||||
for (const volume of wwvVolumes) {
|
||||
let projectName = '';
|
||||
if (volume.endsWith('_postgres-data')) {
|
||||
projectName = volume.slice(0, -'_postgres-data'.length);
|
||||
} else if (volume.endsWith('_wwv-data')) {
|
||||
projectName = volume.slice(0, -'_wwv-data'.length);
|
||||
}
|
||||
|
||||
if (!activeProjectNames.has(projectName)) {
|
||||
console.log(`🗑️ Deleting orphaned volume: ${volume}`);
|
||||
try {
|
||||
execSync(`docker volume rm ${volume}`, { stdio: 'inherit' });
|
||||
orphanedCount++;
|
||||
} catch (rmError) {
|
||||
console.warn(`⚠️ Could not remove volume ${volume}. It may still be in use.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (orphanedCount === 0) {
|
||||
console.log('✅ All existing volumes belong to active worktrees. Nothing to prune.');
|
||||
} else {
|
||||
console.log(`✅ Successfully pruned ${orphanedCount} orphaned volume(s).`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to prune volumes:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* @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';
|
||||
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');
|
||||
|
||||
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: '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)
|
||||
);
|
||||
@@ -1,7 +1,7 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import * as client from "openid-client";
|
||||
import { encryptCredential } from "@/lib/auth/encryption";
|
||||
import db from "@/lib/db";
|
||||
import { prisma as db } from "@/lib/db";
|
||||
export async function GET(req: NextRequest) {
|
||||
const isHttps = req.nextUrl.protocol === "https:";
|
||||
const cookiePrefix = isHttps ? "__Host-" : "";
|
||||
@@ -25,12 +25,11 @@ export async function GET(req: NextRequest) {
|
||||
authorization_endpoint: new URL("/oauth/authorize", issuer).toString(),
|
||||
token_endpoint: new URL("/api/tickets/exchange", issuer).toString(),
|
||||
};
|
||||
const config = { client_id: "local-app" };
|
||||
const config = { server, clientId: "local-app" };
|
||||
|
||||
try {
|
||||
const tokens = await client.processAuthCodeResponse(
|
||||
server,
|
||||
config,
|
||||
const tokens = await client.authorizationCodeGrant(
|
||||
config as any,
|
||||
new URL(req.url),
|
||||
{ expectedState: stateCookie, pkceCodeVerifier: verifierCookie }
|
||||
);
|
||||
|
||||
@@ -10,7 +10,24 @@ vi.mock("openid-client", () => ({
|
||||
discoveryRequest: vi.fn(),
|
||||
processAuthorizationResponse: vi.fn(),
|
||||
validateAuthResponse: vi.fn(),
|
||||
processAuthCodeResponse: vi.fn()
|
||||
authorizationCodeGrant: vi.fn().mockResolvedValue({ access_token: "mock-token" })
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/auth/encryption", () => ({
|
||||
encryptCredential: vi.fn().mockResolvedValue({
|
||||
version: 1,
|
||||
salt: "mock-salt",
|
||||
nonce: "mock-nonce",
|
||||
ciphertext: "mock-ciphertext"
|
||||
})
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
marketplaceCredential: {
|
||||
upsert: vi.fn().mockResolvedValue({})
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
describe("PKCE Flow", () => {
|
||||
|
||||
Reference in new issue
Block a user