@narumitw/pi-worktree 0.51.7 → 0.51.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/session.ts CHANGED
@@ -7,101 +7,93 @@ import { stripTerminalControls } from "./git.js";
7
7
  export type WorktreeSwitchResult = "switched" | "cancelled" | "failed";
8
8
 
9
9
  export async function switchToWorktree(
10
- ctx: ExtensionCommandContext,
11
- targetPath: string,
10
+ ctx: ExtensionCommandContext,
11
+ targetPath: string,
12
12
  ): Promise<WorktreeSwitchResult> {
13
- let sessionPath: string | undefined;
14
- try {
15
- sessionPath = createTargetSession(ctx, targetPath);
16
- const result = await ctx.switchSession(sessionPath, {
17
- withSession: async (replacementCtx) => {
18
- replacementCtx.ui.notify(
19
- stripTerminalControls(`Switched Pi workspace to ${targetPath}.`),
20
- "info",
21
- );
22
- },
23
- });
24
- if (result.cancelled) {
25
- ctx.ui.notify(
26
- stripTerminalControls(
27
- `Workspace switch was cancelled. The prepared target session was retained at ${sessionPath}.`,
28
- ),
29
- "info",
30
- );
31
- return "cancelled";
32
- }
33
- return "switched";
34
- } catch (error) {
35
- const retained = sessionPath ? " The prepared target session was retained." : "";
36
- const message = stripTerminalControls(
37
- `Could not switch Pi workspace to ${targetPath}.${retained} The worktree was retained. Retry from /worktree. ${formatError(error)}`,
38
- );
39
- try {
40
- ctx.ui.notify(message, "error");
41
- } catch {
42
- console.error(message);
43
- }
44
- return "failed";
45
- }
13
+ let sessionPath: string | undefined;
14
+ try {
15
+ sessionPath = createTargetSession(ctx, targetPath);
16
+ const result = await ctx.switchSession(sessionPath, {
17
+ withSession: async (replacementCtx) => {
18
+ replacementCtx.ui.notify(stripTerminalControls(`Switched Pi workspace to ${targetPath}.`), "info");
19
+ },
20
+ });
21
+ if (result.cancelled) {
22
+ ctx.ui.notify(
23
+ stripTerminalControls(
24
+ `Workspace switch was cancelled. The prepared target session was retained at ${sessionPath}.`,
25
+ ),
26
+ "info",
27
+ );
28
+ return "cancelled";
29
+ }
30
+ return "switched";
31
+ } catch (error) {
32
+ const retained = sessionPath ? " The prepared target session was retained." : "";
33
+ const message = stripTerminalControls(
34
+ `Could not switch Pi workspace to ${targetPath}.${retained} The worktree was retained. Retry from /worktree. ${formatError(error)}`,
35
+ );
36
+ try {
37
+ ctx.ui.notify(message, "error");
38
+ } catch {
39
+ console.error(message);
40
+ }
41
+ return "failed";
42
+ }
46
43
  }
47
44
 
48
45
  export function createTargetSession(ctx: ExtensionCommandContext, targetPath: string): string {
49
- const sourceFile = ctx.sessionManager.getSessionFile();
50
- if (sourceFile && existsSync(sourceFile)) {
51
- const persisted = SessionManager.open(sourceFile);
52
- const activeLeaf = ctx.sessionManager.getLeafId();
53
- if (persisted.getLeafId() === activeLeaf) {
54
- const forked = SessionManager.forkFrom(sourceFile, targetPath);
55
- const targetFile = forked.getSessionFile();
56
- if (!targetFile || !existsSync(targetFile)) {
57
- throw new Error("Pi did not create the target worktree session file.");
58
- }
59
- return targetFile;
60
- }
61
- if (activeLeaf !== null && !persisted.getEntry(activeLeaf)) {
62
- throw new Error("The active Pi session branch is not present in the persisted source file.");
63
- }
64
- return writeTargetSession(targetPath, ctx.sessionManager.getBranch(), sourceFile, activeLeaf);
65
- }
46
+ const sourceFile = ctx.sessionManager.getSessionFile();
47
+ if (sourceFile && existsSync(sourceFile)) {
48
+ const persisted = SessionManager.open(sourceFile);
49
+ const activeLeaf = ctx.sessionManager.getLeafId();
50
+ if (persisted.getLeafId() === activeLeaf) {
51
+ const forked = SessionManager.forkFrom(sourceFile, targetPath);
52
+ const targetFile = forked.getSessionFile();
53
+ if (!targetFile || !existsSync(targetFile)) {
54
+ throw new Error("Pi did not create the target worktree session file.");
55
+ }
56
+ return targetFile;
57
+ }
58
+ if (activeLeaf !== null && !persisted.getEntry(activeLeaf)) {
59
+ throw new Error("The active Pi session branch is not present in the persisted source file.");
60
+ }
61
+ return writeTargetSession(targetPath, ctx.sessionManager.getBranch(), sourceFile, activeLeaf);
62
+ }
66
63
 
67
- if (ctx.sessionManager.getEntries().length > 0) {
68
- return writeTargetSession(
69
- targetPath,
70
- ctx.sessionManager.getBranch(),
71
- undefined,
72
- ctx.sessionManager.getLeafId(),
73
- );
74
- }
64
+ if (ctx.sessionManager.getEntries().length > 0) {
65
+ return writeTargetSession(targetPath, ctx.sessionManager.getBranch(), undefined, ctx.sessionManager.getLeafId());
66
+ }
75
67
 
76
- return writeTargetSession(targetPath, [], undefined, null);
68
+ return writeTargetSession(targetPath, [], undefined, null);
77
69
  }
78
70
 
79
71
  function writeTargetSession(
80
- targetPath: string,
81
- entries: readonly SessionEntry[],
82
- parentSession: string | undefined,
83
- expectedLeaf: string | null,
72
+ targetPath: string,
73
+ entries: readonly SessionEntry[],
74
+ parentSession: string | undefined,
75
+ expectedLeaf: string | null,
84
76
  ): string {
85
- const target = SessionManager.create(targetPath, undefined, { parentSession });
86
- const targetFile = target.getSessionFile();
87
- const header = target.getHeader();
88
- if (!targetFile || !header) throw new Error("Pi could not prepare a target session.");
89
- const document = [header, ...entries].map((entry) => JSON.stringify(entry)).join("\n");
90
- writeFileSync(targetFile, `${document}\n`, {
91
- encoding: "utf8",
92
- flag: "wx",
93
- mode: 0o600,
94
- });
95
- const verified = SessionManager.open(targetFile);
96
- if (
97
- normalizeTargetPath(verified.getCwd()) !== normalizeTargetPath(targetPath) ||
98
- verified.getLeafId() !== expectedLeaf
99
- ) {
100
- throw new Error("Pi could not verify the target session cwd and active branch.");
101
- }
102
- return targetFile;
77
+ const target = SessionManager.create(targetPath, undefined, { parentSession });
78
+ const targetFile = target.getSessionFile();
79
+ const header = target.getHeader();
80
+ if (!targetFile || !header) throw new Error("Pi could not prepare a target session.");
81
+ const document = [header, ...entries].map((entry) => JSON.stringify(entry)).join("\n");
82
+ writeFileSync(targetFile, `${document}\n`, {
83
+ encoding: "utf8",
84
+ flag: "wx",
85
+ mode: 0o600,
86
+ });
87
+ const verified = SessionManager.open(targetFile);
88
+ if (
89
+ normalizeTargetPath(verified.getCwd()) !== normalizeTargetPath(targetPath) ||
90
+ verified.getLeafId() !== expectedLeaf
91
+ ) {
92
+ throw new Error("Pi could not verify the target session cwd and active branch.");
93
+ }
94
+ return targetFile;
103
95
  }
104
96
 
105
97
  function formatError(error: unknown): string {
106
- return error instanceof Error ? error.message : String(error);
98
+ return error instanceof Error ? error.message : String(error);
107
99
  }