1
1
import fs from 'node:fs' ;
2
+ import path from 'node:path' ;
2
3
import { t } from './i18n' ;
3
4
4
5
const lockFiles = [
@@ -9,21 +10,80 @@ const lockFiles = [
9
10
'bun.lock' ,
10
11
] ;
11
12
12
- const existingLockFiles : string [ ] = [ ] ;
13
- export function checkLockFiles ( ) {
13
+ // Function to check if a package.json has a workspaces field
14
+ function hasWorkspaces ( dir : string ) : boolean {
15
+ const pkgPath = path . join ( dir , 'package.json' ) ;
16
+ if ( fs . existsSync ( pkgPath ) ) {
17
+ try {
18
+ const pkg = JSON . parse ( fs . readFileSync ( pkgPath , 'utf-8' ) ) ;
19
+ return ! ! pkg . workspaces ;
20
+ } catch ( e ) {
21
+ // Ignore parsing errors
22
+ }
23
+ }
24
+ return false ;
25
+ }
26
+
27
+ // Helper function to find lock files in a specific directory
28
+ function findLockFilesInDir ( directory : string ) : string [ ] {
29
+ const found : string [ ] = [ ] ;
14
30
for ( const file of lockFiles ) {
15
- if ( fs . existsSync ( file ) ) {
16
- existingLockFiles . push ( file ) ;
31
+ const filePath = path . join ( directory , file ) ;
32
+ if ( fs . existsSync ( filePath ) ) {
33
+ found . push ( filePath ) ;
17
34
}
18
35
}
19
- if ( existingLockFiles . length === 1 ) {
36
+ return found ;
37
+ }
38
+
39
+ export function checkLockFiles ( ) {
40
+ const cwd = process . cwd ( ) ;
41
+ let searchDir = cwd ;
42
+ let foundLockFiles = findLockFilesInDir ( searchDir ) ;
43
+
44
+ // If no lock file in cwd, try to find monorepo root and check there
45
+ if ( foundLockFiles . length === 0 ) {
46
+ // Search upwards for package.json with workspaces
47
+ let currentDir = path . dirname ( cwd ) ; // Start searching from parent
48
+ let projectRootDir : string | null = null ;
49
+
50
+ while ( true ) {
51
+ if ( hasWorkspaces ( currentDir ) ) {
52
+ projectRootDir = currentDir ;
53
+ break ;
54
+ }
55
+ const parentDir = path . dirname ( currentDir ) ;
56
+ if ( parentDir === currentDir ) {
57
+ // Reached the filesystem root
58
+ break ;
59
+ }
60
+ currentDir = parentDir ;
61
+ }
62
+
63
+ // If a potential root was found, switch search directory and re-check
64
+ if ( projectRootDir ) {
65
+ searchDir = projectRootDir ;
66
+ foundLockFiles = findLockFilesInDir ( searchDir ) ;
67
+ }
68
+ // If no projectRootDir found, foundLockFiles remains empty and searchDir remains cwd
69
+ }
70
+
71
+ // Handle results based on findings in the final searchDir
72
+ if ( foundLockFiles . length === 1 ) {
73
+ // Successfully found one lock file in the determined searchDir
20
74
return ;
21
75
}
22
- console . warn ( t ( 'lockBestPractice' ) ) ;
23
- if ( existingLockFiles . length === 0 ) {
24
- throw new Error ( t ( 'lockNotFound' ) ) ;
76
+
77
+ if ( foundLockFiles . length > 1 ) {
78
+ // Found multiple lock files in the determined searchDir
79
+ console . warn ( t ( 'lockBestPractice' ) ) ;
80
+ throw new Error (
81
+ t ( 'multipleLocksFound' , { lockFiles : foundLockFiles . join ( ', ' ) } ) ,
82
+ ) ;
25
83
}
26
- throw new Error (
27
- t ( 'multipleLocksFound' , { lockFiles : existingLockFiles . join ( ', ' ) } ) ,
28
- ) ;
84
+
85
+ // If we reach here, foundLockFiles.length === 0
86
+ console . warn ( t ( 'lockBestPractice' ) ) ;
87
+ // Warn instead of throwing an error if no lock file is found
88
+ console . warn ( t ( 'lockNotFound' ) ) ;
29
89
}
0 commit comments