1
1
#!/usr/bin/env bun
2
2
3
- import fs from 'fs' ;
4
- import path from ' path' ;
3
+ import fs from "fs" ;
4
+ import path from " path" ;
5
5
6
- const rootDir = path . resolve ( import . meta. dir , '..' ) ;
7
- const BACKUP_DIR = path . join ( rootDir , ' .package-backups' ) ;
6
+ const rootDir = path . resolve ( import . meta. dir , ".." ) ;
7
+ const BACKUP_DIR = path . join ( rootDir , " .package-backups" ) ;
8
8
9
9
interface PackageJson {
10
10
name : string ;
@@ -28,139 +28,182 @@ interface RootPackageJson extends PackageJson {
28
28
}
29
29
30
30
function readJSON < T = any > ( filePath : string ) : T {
31
- return JSON . parse ( fs . readFileSync ( filePath , ' utf8' ) ) ;
31
+ return JSON . parse ( fs . readFileSync ( filePath , " utf8" ) ) ;
32
32
}
33
33
34
34
function writeJSON ( filePath : string , data : any ) : void {
35
- fs . writeFileSync ( filePath , JSON . stringify ( data , null , 4 ) + '\n' ) ;
35
+ fs . writeFileSync ( filePath , JSON . stringify ( data , null , 4 ) + "\n" ) ;
36
36
}
37
37
38
38
function getWorkspacePackages ( ) : Map < string , WorkspacePackageInfo > {
39
39
const packages = new Map < string , WorkspacePackageInfo > ( ) ;
40
- const packagesDir = path . join ( rootDir , 'packages' ) ;
41
-
42
- const dirs = fs . readdirSync ( packagesDir )
43
- . filter ( dir => fs . statSync ( path . join ( packagesDir , dir ) ) . isDirectory ( ) ) ;
44
-
40
+ const packagesDir = path . join ( rootDir , "packages" ) ;
41
+
42
+ const dirs = fs
43
+ . readdirSync ( packagesDir )
44
+ . filter ( ( dir ) =>
45
+ fs . statSync ( path . join ( packagesDir , dir ) ) . isDirectory ( )
46
+ ) ;
47
+
45
48
for ( const dir of dirs ) {
46
- const pkgPath = path . join ( packagesDir , dir , ' package.json' ) ;
49
+ const pkgPath = path . join ( packagesDir , dir , " package.json" ) ;
47
50
if ( fs . existsSync ( pkgPath ) ) {
48
51
const pkg = readJSON < PackageJson > ( pkgPath ) ;
49
52
packages . set ( pkg . name , {
50
53
version : pkg . version ,
51
- path : pkgPath
54
+ path : pkgPath ,
52
55
} ) ;
53
56
}
54
57
}
55
-
58
+
56
59
return packages ;
57
60
}
58
61
59
62
function getCatalogVersions ( ) : Record < string , string > {
60
- const rootPkg = readJSON < RootPackageJson > ( path . join ( rootDir , 'package.json' ) ) ;
63
+ const rootPkg = readJSON < RootPackageJson > (
64
+ path . join ( rootDir , "package.json" )
65
+ ) ;
61
66
return rootPkg . workspaces ?. catalog || { } ;
62
67
}
63
68
64
- function resolveWorkspaceVersion ( depName : string , workspacePackages : Map < string , WorkspacePackageInfo > ) : string | null {
69
+ function resolveWorkspaceVersion (
70
+ depName : string ,
71
+ workspacePackages : Map < string , WorkspacePackageInfo >
72
+ ) : string | null {
65
73
const pkg = workspacePackages . get ( depName ) ;
66
74
return pkg ? pkg . version : null ;
67
75
}
68
76
69
- function resolveCatalogVersion ( depName : string , catalogVersions : Record < string , string > ) : string | null {
77
+ function resolveCatalogVersion (
78
+ depName : string ,
79
+ catalogVersions : Record < string , string >
80
+ ) : string | null {
70
81
return catalogVersions [ depName ] || null ;
71
82
}
72
83
73
84
function backupPackageJson ( filePath : string ) : void {
74
85
if ( ! fs . existsSync ( BACKUP_DIR ) ) {
75
86
fs . mkdirSync ( BACKUP_DIR , { recursive : true } ) ;
76
87
}
77
-
88
+
78
89
const relativePath = path . relative ( rootDir , filePath ) ;
79
90
const backupPath = path . join ( BACKUP_DIR , relativePath ) ;
80
91
const backupDir = path . dirname ( backupPath ) ;
81
-
92
+
82
93
if ( ! fs . existsSync ( backupDir ) ) {
83
94
fs . mkdirSync ( backupDir , { recursive : true } ) ;
84
95
}
85
-
96
+
86
97
fs . copyFileSync ( filePath , backupPath ) ;
87
98
}
99
+ function handleDependency < T > (
100
+ type : string ,
101
+ deps : Record < string , string > ,
102
+ name : string ,
103
+ version : string ,
104
+ catalogVersions : T ,
105
+ resolverFn : ( name : string , version : T ) => string | null
106
+ ) : { modified : boolean ; deps : Record < string , string > } {
107
+ if ( version !== type ) {
108
+ return { modified : false , deps } ;
109
+ }
110
+ const resolvedVersion = resolverFn ( name , catalogVersions ) ;
111
+ if ( ! resolvedVersion ) {
112
+ console . warn ( ` ! Could not resolve ${ type } version for ${ name } ` ) ;
113
+ return { modified : false , deps } ;
114
+ }
115
+ deps [ name ] = resolvedVersion ;
116
+ console . log ( ` ✓ Resolved ${ name } : ${ type } → ${ resolvedVersion } ` ) ;
117
+ return { modified : true , deps } ;
118
+ }
88
119
89
120
function processPackageJson (
90
- pkgPath : string ,
91
- workspacePackages : Map < string , WorkspacePackageInfo > ,
121
+ pkgPath : string ,
122
+ workspacePackages : Map < string , WorkspacePackageInfo > ,
92
123
catalogVersions : Record < string , string >
93
124
) : boolean {
94
125
const pkg = readJSON < PackageJson > ( pkgPath ) ;
95
126
let modified = false ;
96
-
127
+
97
128
backupPackageJson ( pkgPath ) ;
98
-
129
+
99
130
const processDeps = ( deps ?: Record < string , string > ) => {
100
131
if ( ! deps ) return ;
101
-
132
+
102
133
for ( const [ name , version ] of Object . entries ( deps ) ) {
103
- if ( version === 'workspace:*' ) {
104
- const resolvedVersion = resolveWorkspaceVersion ( name , workspacePackages ) ;
105
- if ( resolvedVersion ) {
106
- deps [ name ] = `^${ resolvedVersion } ` ;
107
- modified = true ;
108
- console . log ( ` ✓ Resolved ${ name } : workspace:* → ^${ resolvedVersion } ` ) ;
109
- } else {
110
- console . warn ( ` ⚠ Could not resolve workspace version for ${ name } ` ) ;
111
- }
112
- } else if ( version === 'catalog:' ) {
113
- const resolvedVersion = resolveCatalogVersion ( name , catalogVersions ) ;
114
- if ( resolvedVersion ) {
115
- deps [ name ] = resolvedVersion ;
116
- modified = true ;
117
- console . log ( ` ✓ Resolved ${ name } : catalog: → ${ resolvedVersion } ` ) ;
118
- } else {
119
- console . warn ( ` ⚠ Could not resolve catalog version for ${ name } ` ) ;
120
- }
134
+ const workspaceDep = handleDependency (
135
+ "workspace:*" ,
136
+ deps ! ,
137
+ name ,
138
+ version ,
139
+ workspacePackages ,
140
+ resolveWorkspaceVersion
141
+ ) ;
142
+ deps = workspaceDep . deps ;
143
+ if ( workspaceDep . modified ) {
144
+ modified = true ;
145
+ continue ;
146
+ }
147
+ const catalogDep = handleDependency (
148
+ "catalog:" ,
149
+ deps ! ,
150
+ name ,
151
+ version ,
152
+ catalogVersions ,
153
+ resolveCatalogVersion
154
+ ) ;
155
+ deps = catalogDep . deps ;
156
+ if ( catalogDep . modified ) {
157
+ modified = true ;
158
+ continue ;
121
159
}
122
160
}
123
161
} ;
124
-
162
+
125
163
processDeps ( pkg . dependencies ) ;
126
164
processDeps ( pkg . devDependencies ) ;
127
165
processDeps ( pkg . peerDependencies ) ;
128
166
processDeps ( pkg . optionalDependencies ) ;
129
-
167
+
130
168
if ( modified ) {
131
169
writeJSON ( pkgPath , pkg ) ;
132
170
console . log ( ` ✅ Updated ${ path . relative ( rootDir , pkgPath ) } ` ) ;
133
171
}
134
-
172
+
135
173
return modified ;
136
174
}
137
175
138
176
function main ( ) : void {
139
- console . log ( ' 🔧 Preparing packages for publishing...\n' ) ;
140
-
177
+ console . log ( " 🔧 Preparing packages for publishing...\n" ) ;
178
+
141
179
const workspacePackages = getWorkspacePackages ( ) ;
142
180
const catalogVersions = getCatalogVersions ( ) ;
143
-
181
+
144
182
console . log ( `📦 Found ${ workspacePackages . size } workspace packages` ) ;
145
- console . log ( `📚 Found ${ Object . keys ( catalogVersions ) . length } catalog entries\n` ) ;
146
-
183
+ console . log (
184
+ `📚 Found ${ Object . keys ( catalogVersions ) . length } catalog entries\n`
185
+ ) ;
186
+
147
187
let packagesModified = 0 ;
148
-
188
+
149
189
for ( const [ name , info ] of workspacePackages ) {
150
190
console . log ( `Processing ${ name } ...` ) ;
151
191
if ( processPackageJson ( info . path , workspacePackages , catalogVersions ) ) {
152
192
packagesModified ++ ;
153
193
}
154
- console . log ( '' ) ;
194
+ console . log ( "" ) ;
155
195
}
156
-
196
+
157
197
console . log ( `✅ Prepared ${ packagesModified } packages for publishing` ) ;
158
198
console . log ( `📁 Backups saved to ${ path . relative ( rootDir , BACKUP_DIR ) } ` ) ;
159
199
}
160
200
161
201
try {
162
202
main ( ) ;
163
203
} catch ( error ) {
164
- console . error ( '❌ Error preparing packages:' , error instanceof Error ? error . message : error ) ;
204
+ console . error (
205
+ "❌ Error preparing packages:" ,
206
+ error instanceof Error ? error . message : error
207
+ ) ;
165
208
process . exit ( 1 ) ;
166
- }
209
+ }
0 commit comments