@@ -757,14 +757,18 @@ describe("init", () => {
757
757
` ) ;
758
758
} ) ;
759
759
760
- it ( "should not touch an existing package.json in a target directory" , async ( ) => {
760
+ it ( "should not touch an existing package.json in an ancestor directory, when a name is passed " , async ( ) => {
761
761
mockConfirm (
762
762
{
763
763
text : "Would you like to use git to manage this Worker?" ,
764
764
result : false ,
765
765
} ,
766
766
{
767
- text : "Would you like to install wrangler into path/to/worker/package.json?" ,
767
+ text : "No package.json found. Would you like to create one?" ,
768
+ result : true ,
769
+ } ,
770
+ {
771
+ text : "Would you like to install wrangler into path/to/worker/my-worker/package.json?" ,
768
772
result : false ,
769
773
} ,
770
774
{
@@ -797,7 +801,8 @@ describe("init", () => {
797
801
Object {
798
802
"debug": "",
799
803
"err": "",
800
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml",
804
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
805
+ ✨ Created path/to/worker/my-worker/package.json",
801
806
"warn": "",
802
807
}
803
808
` ) ;
@@ -853,14 +858,14 @@ describe("init", () => {
853
858
` ) ;
854
859
} ) ;
855
860
856
- it ( "should offer to install wrangler into a package.json relative to the target directory" , async ( ) => {
861
+ it ( "should offer to install wrangler into a package.json relative to the target directory, if no name is provided " , async ( ) => {
857
862
mockConfirm (
858
863
{
859
864
text : "Would you like to use git to manage this Worker?" ,
860
865
result : false ,
861
866
} ,
862
867
{
863
- text : "Would you like to install wrangler into path/to/worker /package.json?" ,
868
+ text : "Would you like to install wrangler into .. /package.json?" ,
864
869
result : true ,
865
870
} ,
866
871
{
@@ -869,7 +874,7 @@ describe("init", () => {
869
874
}
870
875
) ;
871
876
mockSelect ( {
872
- text : "Would you like to create a Worker at path/to/worker/my-worker/ src/index.js?" ,
877
+ text : "Would you like to create a Worker at src/index.js?" ,
873
878
result : "none" ,
874
879
} ) ;
875
880
writeFiles ( {
@@ -879,9 +884,11 @@ describe("init", () => {
879
884
} ,
880
885
} ,
881
886
} ) ;
887
+ setWorkingDirectory ( "path/to/worker/my-worker" ) ;
882
888
883
- await runWrangler ( "init path/to/worker/my-worker " ) ;
889
+ await runWrangler ( "init" ) ;
884
890
891
+ setWorkingDirectory ( "../../../.." ) ;
885
892
checkFiles ( {
886
893
items : {
887
894
"path/to/worker/package.json" : {
@@ -898,7 +905,7 @@ describe("init", () => {
898
905
Object {
899
906
"debug": "",
900
907
"err": "",
901
- "out": "✨ Created path/to/worker/my-worker/ wrangler.toml
908
+ "out": "✨ Created wrangler.toml
902
909
✨ Installed wrangler into devDependencies",
903
910
"warn": "",
904
911
}
@@ -1221,6 +1228,10 @@ describe("init", () => {
1221
1228
text : "Would you like to use git to manage this Worker?" ,
1222
1229
result : false ,
1223
1230
} ,
1231
+ {
1232
+ text : "No package.json found. Would you like to create one?" ,
1233
+ result : true ,
1234
+ } ,
1224
1235
{
1225
1236
text : "Would you like to install wrangler into package.json?" ,
1226
1237
result : false ,
@@ -1251,6 +1262,7 @@ describe("init", () => {
1251
1262
"debug": "",
1252
1263
"err": "",
1253
1264
"out": "✨ Created my-worker/wrangler.toml
1265
+ ✨ Created my-worker/package.json
1254
1266
✨ Created my-worker/tsconfig.json
1255
1267
✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1256
1268
"warn": "",
@@ -1359,11 +1371,21 @@ describe("init", () => {
1359
1371
` ) ;
1360
1372
} ) ;
1361
1373
1362
- it ( "should not touch an existing tsconfig.json in the ancestor of a target directory" , async ( ) => {
1363
- mockConfirm ( {
1364
- text : "Would you like to use git to manage this Worker?" ,
1365
- result : false ,
1366
- } ) ;
1374
+ it ( "should not touch an existing tsconfig.json in the ancestor of a target directory, if a name is passed" , async ( ) => {
1375
+ mockConfirm (
1376
+ {
1377
+ text : "Would you like to use git to manage this Worker?" ,
1378
+ result : false ,
1379
+ } ,
1380
+ {
1381
+ text : "No package.json found. Would you like to create one?" ,
1382
+ result : true ,
1383
+ } ,
1384
+ {
1385
+ text : "Would you like to use TypeScript?" ,
1386
+ result : true ,
1387
+ }
1388
+ ) ;
1367
1389
mockSelect ( {
1368
1390
text : "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?" ,
1369
1391
result : "fetch" ,
@@ -1398,10 +1420,13 @@ describe("init", () => {
1398
1420
"debug": "",
1399
1421
"err": "",
1400
1422
"out": "✨ Created path/to/worker/my-worker/wrangler.toml
1423
+ ✨ Created path/to/worker/my-worker/package.json
1424
+ ✨ Created path/to/worker/my-worker/tsconfig.json
1401
1425
✨ Created path/to/worker/my-worker/src/index.ts
1426
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1402
1427
1403
- To start developing your Worker, run \`npx wrangler dev \`
1404
- To publish your Worker to the Internet, run \`npx wrangler publish \`",
1428
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start \`
1429
+ To publish your Worker to the Internet, run \`npm run deploy \`",
1405
1430
"warn": "",
1406
1431
}
1407
1432
` ) ;
@@ -1670,7 +1695,7 @@ describe("init", () => {
1670
1695
result : false ,
1671
1696
} ,
1672
1697
{
1673
- text : "Would you like to install wrangler into package.json?" ,
1698
+ text : "Would you like to install wrangler into my-worker/ package.json?" ,
1674
1699
result : false ,
1675
1700
} ,
1676
1701
{
@@ -1681,7 +1706,9 @@ describe("init", () => {
1681
1706
const PLACEHOLDER = "/* placeholder text */" ;
1682
1707
writeFiles ( {
1683
1708
items : {
1684
- "package.json" : { contents : { name : "test" , version : "1.0.0" } } ,
1709
+ "my-worker/package.json" : {
1710
+ contents : { name : "test" , version : "1.0.0" } ,
1711
+ } ,
1685
1712
"my-worker/src/index.js" : { contents : PLACEHOLDER } ,
1686
1713
} ,
1687
1714
} ) ;
@@ -1806,6 +1833,72 @@ describe("init", () => {
1806
1833
}
1807
1834
` ) ;
1808
1835
} ) ;
1836
+
1837
+ it ( "should ignore ancestor files (such as wrangler.toml, package.json and tsconfig.json) if a name/path is given" , async ( ) => {
1838
+ mockConfirm (
1839
+ {
1840
+ text : "Would you like to use git to manage this Worker?" ,
1841
+ result : false ,
1842
+ } ,
1843
+ {
1844
+ text : "Would you like to use TypeScript?" ,
1845
+ result : true ,
1846
+ } ,
1847
+ {
1848
+ text : "No package.json found. Would you like to create one?" ,
1849
+ result : true ,
1850
+ } ,
1851
+ {
1852
+ text : "Would you like to install the type definitions for Workers into your package.json?" ,
1853
+ result : true ,
1854
+ }
1855
+ ) ;
1856
+ mockSelect ( {
1857
+ text : "Would you like to create a Worker at sub/folder/worker/src/index.ts?" ,
1858
+ result : "fetch" ,
1859
+ } ) ;
1860
+ writeFiles ( {
1861
+ items : {
1862
+ "package.json" : { contents : { name : "top-level" } } ,
1863
+ "tsconfig.json" : { contents : { compilerOptions : { } } } ,
1864
+ "wrangler.toml" : wranglerToml ( {
1865
+ name : "top-level" ,
1866
+ compatibility_date : "some-date" ,
1867
+ } ) ,
1868
+ } ,
1869
+ } ) ;
1870
+
1871
+ await runWrangler ( "init sub/folder/worker" ) ;
1872
+
1873
+ // Ancestor files are untouched.
1874
+ checkFiles ( {
1875
+ items : {
1876
+ "package.json" : { contents : { name : "top-level" } } ,
1877
+ "tsconfig.json" : {
1878
+ contents : { config : { compilerOptions : { } } , error : undefined } ,
1879
+ } ,
1880
+ "wrangler.toml" : wranglerToml ( {
1881
+ name : "top-level" ,
1882
+ compatibility_date : "some-date" ,
1883
+ } ) ,
1884
+ } ,
1885
+ } ) ;
1886
+ // New initialized Worker has its own files.
1887
+ checkFiles ( {
1888
+ items : {
1889
+ "sub/folder/worker/package.json" : {
1890
+ contents : expect . objectContaining ( {
1891
+ name : "worker" ,
1892
+ } ) ,
1893
+ } ,
1894
+ "sub/folder/worker/tsconfig.json" : true ,
1895
+ "sub/folder/worker/wrangler.toml" : wranglerToml ( {
1896
+ ...MINIMAL_WRANGLER_TOML ,
1897
+ name : "worker" ,
1898
+ } ) ,
1899
+ } ,
1900
+ } ) ;
1901
+ } ) ;
1809
1902
} ) ;
1810
1903
} ) ;
1811
1904
0 commit comments