@@ -31,8 +31,23 @@ export function findSmythPath(_path: string = '', callback?: (smythDir: string,
3131 return envDir ;
3232 }
3333
34+ const isElectron = ! ! process . versions . electron ;
35+ let execPath = '' ;
36+ if ( isElectron ) {
37+ // In packaged Electron apps, use resourcesPath if available
38+ if ( ( process as any ) . resourcesPath ) {
39+ // process.resourcesPath points to the 'resources' folder in packaged app
40+ // Go up one level to get the app directory
41+ execPath = path . dirname ( ( process as any ) . resourcesPath ) ;
42+ } else {
43+ // Development mode or fallback
44+ execPath = path . dirname ( process . execPath ) ;
45+ }
46+ } else {
47+ execPath = process . cwd ( ) ;
48+ }
3449 // 1. Try to find in local directory (the directory from which the program was run)
35- const localDir = path . resolve ( process . cwd ( ) , '.smyth' , _path ) ;
50+ const localDir = path . resolve ( execPath , '.smyth' , _path ) ;
3651 searchDirectories . push ( localDir ) ;
3752
3853 // 2. Try to find from main script's directory (entry point)
@@ -61,10 +76,11 @@ export function findSmythPath(_path: string = '', callback?: (smythDir: string,
6176 const homeDir = path . resolve ( os . homedir ( ) , '.smyth' , _path ) ;
6277 searchDirectories . push ( homeDir ) ;
6378
79+ const deduplicatedSearchDirectories = Array . from ( new Set ( searchDirectories ) ) ;
6480 //check if any of the directories exist
65- for ( let i = 0 ; i < searchDirectories . length ; i ++ ) {
66- const dir = searchDirectories [ i ] ;
67- const nextDir = searchDirectories [ i + 1 ] ;
81+ for ( let i = 0 ; i < deduplicatedSearchDirectories . length ; i ++ ) {
82+ const dir = deduplicatedSearchDirectories [ i ] ;
83+ const nextDir = deduplicatedSearchDirectories [ i + 1 ] ;
6884 if ( ! fs . existsSync ( dir ) ) {
6985 callback ?.( dir , false , nextDir ) ;
7086 continue ;
@@ -82,7 +98,8 @@ export function findSmythPath(_path: string = '', callback?: (smythDir: string,
8298
8399export function findValidResourcePath ( listOfLocations : string [ ] , callback ?: ( dir : string , success ?: boolean , nextDir ?: string ) => void ) {
84100 let found = '' ;
85- for ( let location of listOfLocations ) {
101+ const deduplicatedLocations = Array . from ( new Set ( listOfLocations ) ) ;
102+ for ( let location of deduplicatedLocations ) {
86103 findSmythPath ( location , ( dir , success , nextDir ) => {
87104 callback ?.( dir , success , nextDir ) ;
88105 if ( success ) {
@@ -93,16 +110,35 @@ export function findValidResourcePath(listOfLocations: string[], callback?: (dir
93110 }
94111 return found ;
95112}
96- function findPackageRoot ( startDir = process . cwd ( ) ) {
97- let currentDir = startDir ;
98-
99- while ( currentDir !== path . dirname ( currentDir ) ) {
100- const packageJsonPath = path . resolve ( currentDir , 'package.json' ) ;
101- if ( fs . existsSync ( packageJsonPath ) ) {
102- return currentDir ;
113+ function findPackageRoot ( startDir ?) {
114+ try {
115+ if ( ! startDir ) {
116+ const isElectron = ! ! process . versions . electron ;
117+ let execPath = '' ;
118+ if ( isElectron ) {
119+ // In packaged Electron apps, use resourcesPath if available
120+ if ( ( process as any ) . resourcesPath ) {
121+ // process.resourcesPath points to the 'resources' folder in packaged app
122+ // Go up one level to get the app directory
123+ execPath = path . dirname ( ( process as any ) . resourcesPath ) ;
124+ } else {
125+ // Development mode or fallback
126+ execPath = path . dirname ( process . execPath ) ;
127+ }
128+ } else {
129+ execPath = process . cwd ( ) ;
130+ }
131+ startDir = execPath ;
103132 }
104- currentDir = path . dirname ( currentDir ) ;
105- }
133+ let currentDir = startDir ;
106134
135+ while ( currentDir !== path . dirname ( currentDir ) ) {
136+ const packageJsonPath = path . resolve ( currentDir , 'package.json' ) ;
137+ if ( fs . existsSync ( packageJsonPath ) ) {
138+ return currentDir ;
139+ }
140+ currentDir = path . dirname ( currentDir ) ;
141+ }
142+ } catch ( error ) { }
107143 return null ;
108144}
0 commit comments