@@ -163,7 +163,7 @@ describe("wrangler", () => {
163
163
const packageJson = JSON . parse (
164
164
fs . readFileSync ( "./package.json" , "utf-8" )
165
165
) ;
166
- expect ( packageJson . name ) . toEqual ( "worker ") ; // TODO: should we infer the name from the directory?
166
+ expect ( packageJson . name ) . toContain ( "wrangler-tests ") ;
167
167
expect ( packageJson . version ) . toEqual ( "0.0.0" ) ;
168
168
expect ( packageJson . devDependencies ) . toEqual ( {
169
169
wrangler : expect . any ( String ) ,
@@ -172,6 +172,28 @@ describe("wrangler", () => {
172
172
expect ( mockPackageManager . install ) . toHaveBeenCalled ( ) ;
173
173
} ) ;
174
174
175
+ it ( "should create a package.json, with the specified name, if none is found and user confirms" , async ( ) => {
176
+ mockConfirm (
177
+ {
178
+ text : "No package.json found. Would you like to create one?" ,
179
+ result : true ,
180
+ } ,
181
+ {
182
+ text : "Would you like to use TypeScript?" ,
183
+ result : false ,
184
+ } ,
185
+ {
186
+ text : "Would you like to create a Worker at src/index.js?" ,
187
+ result : false ,
188
+ }
189
+ ) ;
190
+ await runWrangler ( "init my-worker" ) ;
191
+ const packageJson = JSON . parse (
192
+ fs . readFileSync ( "./package.json" , "utf-8" )
193
+ ) ;
194
+ expect ( packageJson . name ) . toBe ( "my-worker" ) ;
195
+ } ) ;
196
+
175
197
it ( "should not touch an existing package.json in the same directory" , async ( ) => {
176
198
mockConfirm (
177
199
{
@@ -329,6 +351,176 @@ describe("wrangler", () => {
329
351
expect ( fs . existsSync ( "./src/index.ts" ) ) . toBe ( true ) ;
330
352
} ) ;
331
353
354
+ it ( "should add scripts for a typescript project with .ts extension" , async ( ) => {
355
+ mockConfirm (
356
+ {
357
+ text : "No package.json found. Would you like to create one?" ,
358
+ result : true ,
359
+ } ,
360
+ {
361
+ text : "Would you like to install wrangler into your package.json?" ,
362
+ result : false ,
363
+ } ,
364
+ {
365
+ text : "Would you like to use TypeScript?" ,
366
+ result : true ,
367
+ } ,
368
+ {
369
+ text : "Would you like to create a Worker at src/index.ts?" ,
370
+ result : true ,
371
+ }
372
+ ) ;
373
+ await runWrangler ( "init" ) ;
374
+
375
+ expect ( fs . existsSync ( "./package.json" ) ) . toBe ( true ) ;
376
+ const packageJson = JSON . parse (
377
+ fs . readFileSync ( "./package.json" , "utf-8" )
378
+ ) ;
379
+
380
+ expect ( fs . existsSync ( "./src/index.js" ) ) . toBe ( false ) ;
381
+ expect ( fs . existsSync ( "./src/index.ts" ) ) . toBe ( true ) ;
382
+
383
+ expect ( packageJson . scripts . start ) . toBe ( "wrangler dev src/index.ts" ) ;
384
+ expect ( packageJson . scripts . deploy ) . toBe ( "wrangler publish src/index.ts" ) ;
385
+ expect ( packageJson . name ) . toContain ( "wrangler-tests" ) ;
386
+ expect ( packageJson . version ) . toEqual ( "0.0.0" ) ;
387
+ expect ( std . out ) . toMatchInlineSnapshot ( `
388
+ "✨ Successfully created wrangler.toml
389
+ ✨ Created package.json
390
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
391
+ To start developing on your worker, run npm start.
392
+ To publish your worker on to the internet, run npm run deploy.
393
+ ✨ Created src/index.ts"
394
+ ` ) ;
395
+ } ) ;
396
+
397
+ it ( "should not overwrite package.json scripts for a typescript project" , async ( ) => {
398
+ mockConfirm (
399
+ {
400
+ text : "Would you like to install wrangler into your package.json?" ,
401
+ result : false ,
402
+ } ,
403
+ {
404
+ text : "Would you like to use TypeScript?" ,
405
+ result : true ,
406
+ } ,
407
+ {
408
+ text : "Would you like to create a Worker at src/index.ts?" ,
409
+ result : true ,
410
+ }
411
+ ) ;
412
+ await fsp . writeFile (
413
+ "./package.json" ,
414
+ JSON . stringify ( {
415
+ scripts : {
416
+ start : "test-start" ,
417
+ deploy : "test-deploy" ,
418
+ } ,
419
+ } )
420
+ ) ;
421
+ const packageJson = JSON . parse (
422
+ fs . readFileSync ( "./package.json" , "utf-8" )
423
+ ) ;
424
+ await runWrangler ( "init" ) ;
425
+
426
+ expect ( fs . existsSync ( "./src/index.js" ) ) . toBe ( false ) ;
427
+ expect ( fs . existsSync ( "./src/index.ts" ) ) . toBe ( true ) ;
428
+
429
+ expect ( packageJson . scripts . start ) . toBe ( "test-start" ) ;
430
+ expect ( packageJson . scripts . deploy ) . toBe ( "test-deploy" ) ;
431
+ expect ( std . out ) . toMatchInlineSnapshot ( `
432
+ "✨ Successfully created wrangler.toml
433
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
434
+ To start developing on your worker, npx wrangler dev src/index.ts
435
+ To publish your worker on to the internet, npx wrangler publish src/index.ts
436
+ ✨ Created src/index.ts"
437
+ ` ) ;
438
+ } ) ;
439
+
440
+ it ( "should add missing scripts for a non-ts project with .js extension" , async ( ) => {
441
+ mockConfirm (
442
+ {
443
+ text : "No package.json found. Would you like to create one?" ,
444
+ result : true ,
445
+ } ,
446
+ {
447
+ text : "Would you like to install wrangler into your package.json?" ,
448
+ result : false ,
449
+ } ,
450
+ {
451
+ text : "Would you like to use TypeScript?" ,
452
+ result : false ,
453
+ } ,
454
+ {
455
+ text : "Would you like to create a Worker at src/index.js?" ,
456
+ result : true ,
457
+ }
458
+ ) ;
459
+ await runWrangler ( "init" ) ;
460
+
461
+ expect ( fs . existsSync ( "./package.json" ) ) . toBe ( true ) ;
462
+ const packageJson = JSON . parse (
463
+ fs . readFileSync ( "./package.json" , "utf-8" )
464
+ ) ;
465
+
466
+ expect ( fs . existsSync ( "./src/index.js" ) ) . toBe ( true ) ;
467
+ expect ( fs . existsSync ( "./src/index.ts" ) ) . toBe ( false ) ;
468
+
469
+ expect ( packageJson . scripts . start ) . toBe ( "wrangler dev src/index.js" ) ;
470
+ expect ( packageJson . scripts . deploy ) . toBe ( "wrangler publish src/index.js" ) ;
471
+ expect ( packageJson . name ) . toContain ( "wrangler-tests" ) ;
472
+ expect ( packageJson . version ) . toEqual ( "0.0.0" ) ;
473
+ expect ( std . out ) . toMatchInlineSnapshot ( `
474
+ "✨ Successfully created wrangler.toml
475
+ ✨ Created package.json
476
+ To start developing on your worker, run npm start.
477
+ To publish your worker on to the internet, run npm run deploy.
478
+ ✨ Created src/index.js"
479
+ ` ) ;
480
+ } ) ;
481
+
482
+ it ( "should not add scripts for a non-ts project with .js extension" , async ( ) => {
483
+ mockConfirm (
484
+ {
485
+ text : "Would you like to install wrangler into your package.json?" ,
486
+ result : false ,
487
+ } ,
488
+ {
489
+ text : "Would you like to use TypeScript?" ,
490
+ result : false ,
491
+ } ,
492
+ {
493
+ text : "Would you like to create a Worker at src/index.js?" ,
494
+ result : true ,
495
+ }
496
+ ) ;
497
+ await fsp . writeFile (
498
+ "./package.json" ,
499
+ JSON . stringify ( {
500
+ scripts : {
501
+ start : "test-start" ,
502
+ deploy : "test-deploy" ,
503
+ } ,
504
+ } )
505
+ ) ;
506
+ const packageJson = JSON . parse (
507
+ fs . readFileSync ( "./package.json" , "utf-8" )
508
+ ) ;
509
+ await runWrangler ( "init" ) ;
510
+
511
+ expect ( fs . existsSync ( "./src/index.js" ) ) . toBe ( true ) ;
512
+ expect ( fs . existsSync ( "./src/index.ts" ) ) . toBe ( false ) ;
513
+
514
+ expect ( packageJson . scripts . start ) . toBe ( "test-start" ) ;
515
+ expect ( packageJson . scripts . deploy ) . toBe ( "test-deploy" ) ;
516
+ expect ( std . out ) . toMatchInlineSnapshot ( `
517
+ "✨ Successfully created wrangler.toml
518
+ To start developing on your worker, npx wrangler dev src/index.js
519
+ To publish your worker on to the internet, npx wrangler publish src/index.js
520
+ ✨ Created src/index.js"
521
+ ` ) ;
522
+ } ) ;
523
+
332
524
it ( "should not offer to create a worker in a non-ts project if a file already exists at the location" , async ( ) => {
333
525
mockConfirm (
334
526
{
0 commit comments