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
5 changes: 4 additions & 1 deletion packages/worktree-cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ function spawnService(
};

const rawArgs = devArgs(svc);
const args = ['--filter', svc.pnpmFilter, 'dev', '--', ...rawArgs.map(interpolate)];
// No `--` separator: with pnpm 9 + `"dev": "vite"`, inserting `--`
// causes vite to receive it as a literal argv entry (`vite -- --port NNN`)
// and silently ignore the port flag, falling back to its default.
const args = ['--filter', svc.pnpmFilter, 'dev', ...rawArgs.map(interpolate)];

const env = { ...process.env };
for (const [k, v] of Object.entries(svc.env ?? {})) {
Expand Down
31 changes: 26 additions & 5 deletions packages/worktree-cli/src/commands/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execFileSync } from 'node:child_process';
import { execFileSync, spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';

Expand All @@ -25,11 +25,17 @@ export async function setup({ branch, cfg, repoRoot }: SetupOptions): Promise<vo

console.log(chalk.bold(`==> Creating worktree for '${branch}' at ${wtPath}...`));

// Fetch origin/main from the main worktree; the new worktree branches from it.
// Fetch origin/main from the main worktree; a fresh worktree branches from it.
execFileSync('git', ['-C', repoRoot, 'fetch', 'origin', 'main'], { stdio: 'inherit' });
execFileSync('git', ['-C', repoRoot, 'worktree', 'add', '-b', branch, wtPath, 'origin/main'], {
stdio: 'inherit',
});

// If the branch already exists (local or remote-tracking), check it out
// into the new worktree instead of creating a fresh branch — otherwise
// `git worktree add -b` errors out on the duplicate.
const reuseExisting = branchExists(repoRoot, branch);
const addArgs = reuseExisting
? ['-C', repoRoot, 'worktree', 'add', wtPath, branch]
: ['-C', repoRoot, 'worktree', 'add', '-b', branch, wtPath, 'origin/main'];
execFileSync('git', addArgs, { stdio: 'inherit' });

const entry = await allocate(cfg, branch);

Expand Down Expand Up @@ -75,3 +81,18 @@ export async function setup({ branch, cfg, repoRoot }: SetupOptions): Promise<vo
console.log('To start the dev server(s):');
Comment thread
rlueder marked this conversation as resolved.
Comment thread
rlueder marked this conversation as resolved.
console.log(` cd ${wtPath} && precisa-worktree dev ${branch}`);
}

function branchExists(repoRoot: string, branch: string): boolean {
const local = spawnSync(
'git',
['-C', repoRoot, 'show-ref', '--verify', '--quiet', `refs/heads/${branch}`],
{ stdio: 'ignore' },
);
if (local.status === 0) return true;
const remote = spawnSync(
'git',
['-C', repoRoot, 'show-ref', '--verify', '--quiet', `refs/remotes/origin/${branch}`],
{ stdio: 'ignore' },
);
return remote.status === 0;
}
Loading