11import { existsSync , renameSync } from "node:fs" ;
22import * as os from "node:os" ;
33import * as path from "node:path" ;
4+ import { DATA_DIR , LEGACY_DATA_DIRS } from "@shared/constants" ;
45import { app } from "electron" ;
56import Store from "electron-store" ;
67
78interface SettingsSchema {
89 worktreeLocation : string ;
910}
1011
11- const LEGACY_DIR_NAME = ".array" ;
12- const CURRENT_DIR_NAME = ".twig" ;
13-
1412function getDefaultWorktreeLocation ( ) : string {
15- return path . join ( os . homedir ( ) , CURRENT_DIR_NAME ) ;
13+ return path . join ( os . homedir ( ) , DATA_DIR ) ;
1614}
1715
18- function getLegacyWorktreeLocation ( ) : string {
19- return path . join ( os . homedir ( ) , LEGACY_DIR_NAME ) ;
16+ function getLegacyWorktreeLocations ( ) : string [ ] {
17+ return LEGACY_DATA_DIRS . map ( ( dir ) => path . join ( os . homedir ( ) , dir ) ) ;
2018}
2119
2220/**
23- * Migrate ~/.array to ~/.twig if needed (one-time migration)
21+ * Migrate legacy directories to current if needed (one-time migration)
2422 */
2523function migrateWorktreeDirectory ( ) : void {
26- const legacyPath = getLegacyWorktreeLocation ( ) ;
2724 const newPath = getDefaultWorktreeLocation ( ) ;
2825
29- // Only migrate if legacy exists and new doesn't
30- if ( existsSync ( legacyPath ) && ! existsSync ( newPath ) ) {
31- try {
32- renameSync ( legacyPath , newPath ) ;
33- } catch {
34- // If rename fails (e.g., cross-device), leave as-is
35- // User can manually migrate or continue using legacy location
26+ // Only migrate if new path doesn't exist yet
27+ if ( existsSync ( newPath ) ) {
28+ return ;
29+ }
30+
31+ // Try to migrate from each legacy location (first one found wins)
32+ for ( const legacyPath of getLegacyWorktreeLocations ( ) ) {
33+ if ( existsSync ( legacyPath ) ) {
34+ try {
35+ renameSync ( legacyPath , newPath ) ;
36+ return ;
37+ } catch {
38+ // If rename fails (e.g., cross-device), leave as-is
39+ // User can manually migrate or continue using legacy location
40+ }
3641 }
3742 }
3843}
@@ -57,16 +62,18 @@ export const settingsStore = new Store<SettingsSchema>({
5762} ) ;
5863
5964/**
60- * Migrate stored worktree setting from ~/.array to ~/.twig if it was the default
65+ * Migrate stored worktree setting from legacy to current if it was a legacy default
6166 */
6267function migrateWorktreeSetting ( ) : void {
6368 const stored = settingsStore . get ( "worktreeLocation" ) ;
64- const legacyDefault = getLegacyWorktreeLocation ( ) ;
6569 const newDefault = getDefaultWorktreeLocation ( ) ;
6670
67- // If user had the legacy default, update to new default
68- if ( stored === legacyDefault && existsSync ( newDefault ) ) {
69- settingsStore . set ( "worktreeLocation" , newDefault ) ;
71+ // If user had a legacy default, update to new default
72+ for ( const legacyPath of getLegacyWorktreeLocations ( ) ) {
73+ if ( stored === legacyPath && existsSync ( newDefault ) ) {
74+ settingsStore . set ( "worktreeLocation" , newDefault ) ;
75+ return ;
76+ }
7077 }
7178}
7279
@@ -77,6 +84,24 @@ export function getWorktreeLocation(): string {
7784 return settingsStore . get ( "worktreeLocation" , getDefaultWorktreeLocation ( ) ) ;
7885}
7986
87+ /**
88+ * Get all worktree locations to check (current + legacy).
89+ * Use this when searching for existing worktrees for backwards compatibility.
90+ */
91+ export function getAllWorktreeLocations ( ) : string [ ] {
92+ const primary = getWorktreeLocation ( ) ;
93+ const locations = [ primary ] ;
94+
95+ // Add legacy locations if they exist and aren't the primary
96+ for ( const legacyPath of getLegacyWorktreeLocations ( ) ) {
97+ if ( legacyPath !== primary && existsSync ( legacyPath ) ) {
98+ locations . push ( legacyPath ) ;
99+ }
100+ }
101+
102+ return locations ;
103+ }
104+
80105export function setWorktreeLocation ( location : string ) : void {
81106 settingsStore . set ( "worktreeLocation" , location ) ;
82107}
0 commit comments