@@ -49,13 +49,16 @@ async function prompt(question) {
4949 } ) ;
5050}
5151
52- function findWranglerToml ( ) {
53- // Look for wrangler.toml in current directory or parent directories
52+ function findWranglerConfig ( ) {
53+ // Prefer wrangler.toml, then wrangler.jsonc, then wrangler.json
54+ const candidates = [ 'wrangler.toml' , 'wrangler.jsonc' , 'wrangler.json' ] ;
5455 let dir = process . cwd ( ) ;
5556 for ( let i = 0 ; i < 5 ; i ++ ) {
56- const tomlPath = join ( dir , 'wrangler.toml' ) ;
57- if ( existsSync ( tomlPath ) ) {
58- return { path : tomlPath , dir } ;
57+ for ( const filename of candidates ) {
58+ const configPath = join ( dir , filename ) ;
59+ if ( existsSync ( configPath ) ) {
60+ return { path : configPath , dir, format : filename . endsWith ( '.toml' ) ? 'toml' : 'json' } ;
61+ }
5962 }
6063 dir = dirname ( dir ) ;
6164 }
@@ -66,16 +69,16 @@ async function main() {
6669 log ( '\n🔧 SonicJS Database Reset Tool' , colors . cyan + colors . bold ) ;
6770 log ( '================================\n' , colors . cyan ) ;
6871
69- // Find wrangler. toml
70- const wranglerInfo = findWranglerToml ( ) ;
72+ // Find wrangler config ( toml, jsonc, or json)
73+ const wranglerInfo = findWranglerConfig ( ) ;
7174 if ( ! wranglerInfo ) {
72- log ( 'Error: Could not find wrangler.toml in current or parent directories' , colors . red ) ;
75+ log ( 'Error: Could not find wrangler.toml / wrangler.jsonc / wrangler.json in current or parent directories' , colors . red ) ;
7376 log ( 'Please run this command from your SonicJS project directory.' , colors . yellow ) ;
7477 process . exit ( 1 ) ;
7578 }
7679
77- const { path : wranglerPath , dir : projectDir } = wranglerInfo ;
78- log ( `Found wrangler.toml at: ${ wranglerPath } ` , colors . green ) ;
80+ const { path : wranglerPath , dir : projectDir , format : wranglerFormat } = wranglerInfo ;
81+ log ( `Found wrangler config at: ${ wranglerPath } ` , colors . green ) ;
7982
8083 // Change to project directory
8184 process . chdir ( projectDir ) ;
@@ -176,22 +179,36 @@ async function main() {
176179
177180 log ( `\nDatabase ID: ${ dbId } ` , colors . green ) ;
178181
179- // Update wrangler.toml with the new database ID
180- log ( '\nUpdating wrangler.toml...' , colors . yellow ) ;
182+ // Update wrangler config with the new database ID
183+ const configBasename = wranglerPath . split ( '/' ) . pop ( ) ;
184+ log ( `\nUpdating ${ configBasename } ...` , colors . yellow ) ;
181185 try {
182186 let wranglerContent = readFileSync ( wranglerPath , 'utf-8' ) ;
183- wranglerContent = wranglerContent . replace (
184- / d a t a b a s e _ i d \s * = \s * " [ ^ " ] * " / ,
185- `database_id = "${ dbId } "`
186- ) ;
187- wranglerContent = wranglerContent . replace (
188- / d a t a b a s e _ n a m e \s * = \s * " [ ^ " ] * " / ,
189- `database_name = "${ dbName } "`
190- ) ;
187+ if ( wranglerFormat === 'toml' ) {
188+ // TOML syntax: database_id = "value"
189+ wranglerContent = wranglerContent . replace (
190+ / d a t a b a s e _ i d \s * = \s * " [ ^ " ] * " / ,
191+ `database_id = "${ dbId } "`
192+ ) ;
193+ wranglerContent = wranglerContent . replace (
194+ / d a t a b a s e _ n a m e \s * = \s * " [ ^ " ] * " / ,
195+ `database_name = "${ dbName } "`
196+ ) ;
197+ } else {
198+ // JSON/JSONC syntax: "database_id": "value"
199+ wranglerContent = wranglerContent . replace (
200+ / " d a t a b a s e _ i d " \s * : \s * " [ ^ " ] * " / ,
201+ `"database_id": "${ dbId } "`
202+ ) ;
203+ wranglerContent = wranglerContent . replace (
204+ / " d a t a b a s e _ n a m e " \s * : \s * " [ ^ " ] * " / ,
205+ `"database_name": "${ dbName } "`
206+ ) ;
207+ }
191208 writeFileSync ( wranglerPath , wranglerContent ) ;
192- log ( ' Updated wrangler.toml successfully' , colors . green ) ;
209+ log ( ` Updated ${ configBasename } successfully` , colors . green ) ;
193210 } catch ( error ) {
194- log ( `Error updating wrangler.toml : ${ error . message } ` , colors . red ) ;
211+ log ( `Error updating ${ configBasename } : ${ error . message } ` , colors . red ) ;
195212 process . exit ( 1 ) ;
196213 }
197214
0 commit comments