Switch to double quotes and reformat codebase with Biome
This commit is contained in:
@@ -1,129 +1,134 @@
|
||||
import type { Command } from 'commander'
|
||||
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
|
||||
import type { CommandOptions, SingleResult, OutputSchema, CommandError } from '../../output/index.js'
|
||||
import type { Command } from "commander";
|
||||
import { connectToDaemon, getDaemonHost } from "../../utils/client.js";
|
||||
import type {
|
||||
CommandOptions,
|
||||
SingleResult,
|
||||
OutputSchema,
|
||||
CommandError,
|
||||
} from "../../output/index.js";
|
||||
|
||||
/** Result type for worktree archive command */
|
||||
export interface WorktreeArchiveResult {
|
||||
name: string
|
||||
status: 'archived'
|
||||
removedAgents: string[]
|
||||
name: string;
|
||||
status: "archived";
|
||||
removedAgents: string[];
|
||||
}
|
||||
|
||||
/** Schema for archive command output */
|
||||
export const archiveSchema: OutputSchema<WorktreeArchiveResult> = {
|
||||
idField: 'name',
|
||||
idField: "name",
|
||||
columns: [
|
||||
{ header: 'NAME', field: 'name' },
|
||||
{ header: 'STATUS', field: 'status' },
|
||||
{ header: "NAME", field: "name" },
|
||||
{ header: "STATUS", field: "status" },
|
||||
{
|
||||
header: 'REMOVED AGENTS',
|
||||
field: (item) => item.removedAgents.length > 0 ? item.removedAgents.join(', ') : '-',
|
||||
header: "REMOVED AGENTS",
|
||||
field: (item) => (item.removedAgents.length > 0 ? item.removedAgents.join(", ") : "-"),
|
||||
},
|
||||
],
|
||||
}
|
||||
};
|
||||
|
||||
export interface WorktreeArchiveOptions extends CommandOptions {
|
||||
host?: string
|
||||
host?: string;
|
||||
}
|
||||
|
||||
export type WorktreeArchiveCommandResult = SingleResult<WorktreeArchiveResult>
|
||||
export type WorktreeArchiveCommandResult = SingleResult<WorktreeArchiveResult>;
|
||||
|
||||
export async function runArchiveCommand(
|
||||
nameArg: string,
|
||||
options: WorktreeArchiveOptions,
|
||||
_command: Command
|
||||
_command: Command,
|
||||
): Promise<WorktreeArchiveCommandResult> {
|
||||
const host = getDaemonHost({ host: options.host })
|
||||
const host = getDaemonHost({ host: options.host });
|
||||
|
||||
// Validate arguments
|
||||
if (!nameArg || nameArg.trim().length === 0) {
|
||||
const error: CommandError = {
|
||||
code: 'MISSING_WORKTREE_NAME',
|
||||
message: 'Worktree name is required',
|
||||
details: 'Usage: paseo worktree archive <name>',
|
||||
}
|
||||
throw error
|
||||
code: "MISSING_WORKTREE_NAME",
|
||||
message: "Worktree name is required",
|
||||
details: "Usage: paseo worktree archive <name>",
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
let client
|
||||
let client;
|
||||
try {
|
||||
client = await connectToDaemon({ host: options.host })
|
||||
client = await connectToDaemon({ host: options.host });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const error: CommandError = {
|
||||
code: 'DAEMON_NOT_RUNNING',
|
||||
code: "DAEMON_NOT_RUNNING",
|
||||
message: `Cannot connect to daemon at ${host}: ${message}`,
|
||||
details: 'Start the daemon with: paseo daemon start',
|
||||
}
|
||||
throw error
|
||||
details: "Start the daemon with: paseo daemon start",
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
try {
|
||||
// Get the list of worktrees first to resolve the name
|
||||
const listResponse = await client.getPaseoWorktreeList({})
|
||||
const listResponse = await client.getPaseoWorktreeList({});
|
||||
|
||||
if (listResponse.error) {
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_LIST_FAILED',
|
||||
code: "WORKTREE_LIST_FAILED",
|
||||
message: `Failed to list worktrees: ${listResponse.error.message}`,
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Find the worktree by name or branch
|
||||
const worktree = listResponse.worktrees.find((wt) => {
|
||||
const name = wt.worktreePath.split('/').pop()
|
||||
return name === nameArg || wt.branchName === nameArg
|
||||
})
|
||||
const name = wt.worktreePath.split("/").pop();
|
||||
return name === nameArg || wt.branchName === nameArg;
|
||||
});
|
||||
|
||||
if (!worktree) {
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_NOT_FOUND',
|
||||
code: "WORKTREE_NOT_FOUND",
|
||||
message: `Worktree not found: ${nameArg}`,
|
||||
details: 'Use "paseo worktree ls" to list available worktrees',
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Archive the worktree
|
||||
const response = await client.archivePaseoWorktree({
|
||||
worktreePath: worktree.worktreePath,
|
||||
})
|
||||
});
|
||||
|
||||
await client.close()
|
||||
await client.close();
|
||||
|
||||
if (response.error) {
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_ARCHIVE_FAILED',
|
||||
code: "WORKTREE_ARCHIVE_FAILED",
|
||||
message: `Failed to archive worktree: ${response.error.message}`,
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
const worktreeName = worktree.worktreePath.split('/').pop() ?? nameArg
|
||||
const worktreeName = worktree.worktreePath.split("/").pop() ?? nameArg;
|
||||
|
||||
return {
|
||||
type: 'single',
|
||||
type: "single",
|
||||
data: {
|
||||
name: worktreeName,
|
||||
status: 'archived',
|
||||
status: "archived",
|
||||
removedAgents: response.removedAgents ?? [],
|
||||
},
|
||||
schema: archiveSchema,
|
||||
}
|
||||
};
|
||||
} catch (err) {
|
||||
await client.close().catch(() => {})
|
||||
await client.close().catch(() => {});
|
||||
|
||||
// Re-throw CommandError as-is
|
||||
if (err && typeof err === 'object' && 'code' in err) {
|
||||
throw err
|
||||
if (err && typeof err === "object" && "code" in err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_ARCHIVE_FAILED',
|
||||
code: "WORKTREE_ARCHIVE_FAILED",
|
||||
message: `Failed to archive worktree: ${message}`,
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
import { Command } from 'commander'
|
||||
import { runLsCommand } from './ls.js'
|
||||
import { runArchiveCommand } from './archive.js'
|
||||
import { withOutput } from '../../output/index.js'
|
||||
import { addJsonAndDaemonHostOptions } from '../../utils/command-options.js'
|
||||
import { Command } from "commander";
|
||||
import { runLsCommand } from "./ls.js";
|
||||
import { runArchiveCommand } from "./archive.js";
|
||||
import { withOutput } from "../../output/index.js";
|
||||
import { addJsonAndDaemonHostOptions } from "../../utils/command-options.js";
|
||||
|
||||
export function createWorktreeCommand(): Command {
|
||||
const worktree = new Command('worktree').description('Manage Paseo-managed git worktrees')
|
||||
const worktree = new Command("worktree").description("Manage Paseo-managed git worktrees");
|
||||
|
||||
addJsonAndDaemonHostOptions(
|
||||
worktree.command("ls").description("List Paseo-managed git worktrees"),
|
||||
).action(withOutput(runLsCommand));
|
||||
|
||||
addJsonAndDaemonHostOptions(
|
||||
worktree
|
||||
.command('ls')
|
||||
.description('List Paseo-managed git worktrees')
|
||||
).action(withOutput(runLsCommand))
|
||||
.command("archive")
|
||||
.description("Archive a worktree (removes worktree and associated branch)")
|
||||
.argument("<name>", "Worktree name or branch name"),
|
||||
).action(withOutput(runArchiveCommand));
|
||||
|
||||
addJsonAndDaemonHostOptions(
|
||||
worktree
|
||||
.command('archive')
|
||||
.description('Archive a worktree (removes worktree and associated branch)')
|
||||
.argument('<name>', 'Worktree name or branch name')
|
||||
).action(withOutput(runArchiveCommand))
|
||||
|
||||
return worktree
|
||||
return worktree;
|
||||
}
|
||||
|
||||
@@ -1,130 +1,130 @@
|
||||
import type { Command } from 'commander'
|
||||
import { homedir } from 'node:os'
|
||||
import { basename, join, sep } from 'node:path'
|
||||
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
|
||||
import type { CommandOptions, ListResult, OutputSchema, CommandError } from '../../output/index.js'
|
||||
import type { Command } from "commander";
|
||||
import { homedir } from "node:os";
|
||||
import { basename, join, sep } from "node:path";
|
||||
import { connectToDaemon, getDaemonHost } from "../../utils/client.js";
|
||||
import type { CommandOptions, ListResult, OutputSchema, CommandError } from "../../output/index.js";
|
||||
|
||||
/** Worktree list item for display */
|
||||
export interface WorktreeListItem {
|
||||
name: string
|
||||
branch: string
|
||||
cwd: string
|
||||
agent: string
|
||||
name: string;
|
||||
branch: string;
|
||||
cwd: string;
|
||||
agent: string;
|
||||
}
|
||||
|
||||
/** Shorten home directory in path */
|
||||
function shortenPath(path: string): string {
|
||||
const home = process.env.HOME
|
||||
const home = process.env.HOME;
|
||||
if (home && path.startsWith(home)) {
|
||||
return '~' + path.slice(home.length)
|
||||
return "~" + path.slice(home.length);
|
||||
}
|
||||
return path
|
||||
return path;
|
||||
}
|
||||
|
||||
/** Extract worktree name from path */
|
||||
function extractWorktreeName(path: string): string {
|
||||
return basename(path)
|
||||
return basename(path);
|
||||
}
|
||||
|
||||
export function resolvePaseoHomePath(): string {
|
||||
return process.env.PASEO_HOME ?? join(homedir(), '.paseo')
|
||||
return process.env.PASEO_HOME ?? join(homedir(), ".paseo");
|
||||
}
|
||||
|
||||
export function resolvePaseoWorktreesDir(): string {
|
||||
return join(resolvePaseoHomePath(), 'worktrees')
|
||||
return join(resolvePaseoHomePath(), "worktrees");
|
||||
}
|
||||
|
||||
function isAgentInManagedWorktree(agentCwd: string): boolean {
|
||||
const worktreesDir = resolvePaseoWorktreesDir()
|
||||
return agentCwd === worktreesDir || agentCwd.startsWith(worktreesDir + sep)
|
||||
const worktreesDir = resolvePaseoWorktreesDir();
|
||||
return agentCwd === worktreesDir || agentCwd.startsWith(worktreesDir + sep);
|
||||
}
|
||||
|
||||
/** Schema for worktree ls output */
|
||||
export const worktreeLsSchema: OutputSchema<WorktreeListItem> = {
|
||||
idField: 'name',
|
||||
idField: "name",
|
||||
columns: [
|
||||
{ header: 'NAME', field: 'name', width: 20 },
|
||||
{ header: 'BRANCH', field: 'branch', width: 25 },
|
||||
{ header: 'CWD', field: 'cwd', width: 45 },
|
||||
{ header: 'AGENT', field: 'agent', width: 10 },
|
||||
{ header: "NAME", field: "name", width: 20 },
|
||||
{ header: "BRANCH", field: "branch", width: 25 },
|
||||
{ header: "CWD", field: "cwd", width: 45 },
|
||||
{ header: "AGENT", field: "agent", width: 10 },
|
||||
],
|
||||
}
|
||||
};
|
||||
|
||||
export type WorktreeLsResult = ListResult<WorktreeListItem>
|
||||
export type WorktreeLsResult = ListResult<WorktreeListItem>;
|
||||
|
||||
export interface WorktreeLsOptions extends CommandOptions {
|
||||
host?: string
|
||||
host?: string;
|
||||
}
|
||||
|
||||
export async function runLsCommand(
|
||||
options: WorktreeLsOptions,
|
||||
_command: Command
|
||||
_command: Command,
|
||||
): Promise<WorktreeLsResult> {
|
||||
const host = getDaemonHost({ host: options.host })
|
||||
const host = getDaemonHost({ host: options.host });
|
||||
|
||||
let client
|
||||
let client;
|
||||
try {
|
||||
client = await connectToDaemon({ host: options.host })
|
||||
client = await connectToDaemon({ host: options.host });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const error: CommandError = {
|
||||
code: 'DAEMON_NOT_RUNNING',
|
||||
code: "DAEMON_NOT_RUNNING",
|
||||
message: `Cannot connect to daemon at ${host}: ${message}`,
|
||||
details: 'Start the daemon with: paseo daemon start',
|
||||
}
|
||||
throw error
|
||||
details: "Start the daemon with: paseo daemon start",
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
try {
|
||||
const agentsPayload = await client.fetchAgents({ filter: { includeArchived: true } })
|
||||
const agents = agentsPayload.entries.map((entry) => entry.agent)
|
||||
const agentsPayload = await client.fetchAgents({ filter: { includeArchived: true } });
|
||||
const agents = agentsPayload.entries.map((entry) => entry.agent);
|
||||
|
||||
// Get worktree list from daemon
|
||||
const response = await client.getPaseoWorktreeList({})
|
||||
const response = await client.getPaseoWorktreeList({});
|
||||
|
||||
await client.close()
|
||||
await client.close();
|
||||
|
||||
if (response.error) {
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_LIST_FAILED',
|
||||
code: "WORKTREE_LIST_FAILED",
|
||||
message: `Failed to list worktrees: ${response.error.message}`,
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Build a map of worktree paths to agent IDs
|
||||
const worktreeAgentMap = new Map<string, string>()
|
||||
const worktreeAgentMap = new Map<string, string>();
|
||||
for (const agent of agents) {
|
||||
if (isAgentInManagedWorktree(agent.cwd)) {
|
||||
worktreeAgentMap.set(agent.cwd, agent.id.slice(0, 7))
|
||||
worktreeAgentMap.set(agent.cwd, agent.id.slice(0, 7));
|
||||
}
|
||||
}
|
||||
|
||||
const items: WorktreeListItem[] = response.worktrees.map((wt) => ({
|
||||
name: extractWorktreeName(wt.worktreePath),
|
||||
branch: wt.branchName ?? '-',
|
||||
branch: wt.branchName ?? "-",
|
||||
cwd: shortenPath(wt.worktreePath),
|
||||
agent: worktreeAgentMap.get(wt.worktreePath) ?? '-',
|
||||
}))
|
||||
agent: worktreeAgentMap.get(wt.worktreePath) ?? "-",
|
||||
}));
|
||||
|
||||
return {
|
||||
type: 'list',
|
||||
type: "list",
|
||||
data: items,
|
||||
schema: worktreeLsSchema,
|
||||
}
|
||||
};
|
||||
} catch (err) {
|
||||
await client.close().catch(() => {})
|
||||
await client.close().catch(() => {});
|
||||
|
||||
// Re-throw CommandError as-is
|
||||
if (err && typeof err === 'object' && 'code' in err) {
|
||||
throw err
|
||||
if (err && typeof err === "object" && "code" in err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_LIST_FAILED',
|
||||
code: "WORKTREE_LIST_FAILED",
|
||||
message: `Failed to list worktrees: ${message}`,
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user