@@ -146,6 +146,8 @@ describe("wrangler", () => {
146
146
await w ( "init" ) ;
147
147
const parsed = TOML . parse ( await fsp . readFile ( "./wrangler.toml" , "utf-8" ) ) ;
148
148
expect ( typeof parsed . compatibility_date ) . toBe ( "string" ) ;
149
+ expect ( fs . existsSync ( "./package.json" ) ) . toBe ( false ) ;
150
+ expect ( fs . existsSync ( "./tsconfig.json" ) ) . toBe ( false ) ;
149
151
} ) ;
150
152
151
153
it ( "should display warning when wrangler.toml already exists, and exit if user does not want to carry on" , async ( ) => {
@@ -178,6 +180,144 @@ describe("wrangler", () => {
178
180
expect ( typeof parsed . compatibility_date ) . toBe ( "string" ) ;
179
181
} ) ;
180
182
183
+ it ( "should create a package.json if none is found and user confirms" , async ( ) => {
184
+ mockConfirm (
185
+ {
186
+ text : "No package.json found. Would you like to create one?" ,
187
+ result : true ,
188
+ } ,
189
+ {
190
+ text : "Would you like to use typescript?" ,
191
+ result : false ,
192
+ }
193
+ ) ;
194
+ await w ( "init" ) ;
195
+ expect ( fs . existsSync ( "./package.json" ) ) . toBe ( true ) ;
196
+ const packageJson = JSON . parse (
197
+ fs . readFileSync ( "./package.json" , "utf-8" )
198
+ ) ;
199
+ expect ( packageJson . name ) . toEqual ( "worker" ) ; // TODO: should we infer the name from the directory?
200
+ expect ( packageJson . version ) . toEqual ( "0.0.1" ) ;
201
+ expect ( fs . existsSync ( "./tsconfig.json" ) ) . toBe ( false ) ;
202
+ } ) ;
203
+
204
+ it ( "should not touch an existing package.json in the same directory" , async ( ) => {
205
+ mockConfirm ( {
206
+ text : "Would you like to use typescript?" ,
207
+ result : false ,
208
+ } ) ;
209
+
210
+ fs . writeFileSync (
211
+ "./package.json" ,
212
+ JSON . stringify ( { name : "test" , version : "1.0.0" } ) ,
213
+ "utf-8"
214
+ ) ;
215
+
216
+ await w ( "init" ) ;
217
+ const packageJson = JSON . parse (
218
+ fs . readFileSync ( "./package.json" , "utf-8" )
219
+ ) ;
220
+ expect ( packageJson . name ) . toEqual ( "test" ) ;
221
+ expect ( packageJson . version ) . toEqual ( "1.0.0" ) ;
222
+ } ) ;
223
+
224
+ it ( "should not touch an existing package.json in an ancestor directory" , async ( ) => {
225
+ mockConfirm ( {
226
+ text : "Would you like to use typescript?" ,
227
+ result : false ,
228
+ } ) ;
229
+
230
+ fs . writeFileSync (
231
+ "./package.json" ,
232
+ JSON . stringify ( { name : "test" , version : "1.0.0" } ) ,
233
+ "utf-8"
234
+ ) ;
235
+
236
+ fs . mkdirSync ( "./sub-1/sub-2" , { recursive : true } ) ;
237
+ process . chdir ( "./sub-1/sub-2" ) ;
238
+
239
+ await w ( "init" ) ;
240
+ expect ( fs . existsSync ( "./package.json" ) ) . toBe ( false ) ;
241
+ expect ( fs . existsSync ( "../../package.json" ) ) . toBe ( true ) ;
242
+
243
+ const packageJson = JSON . parse (
244
+ fs . readFileSync ( "../../package.json" , "utf-8" )
245
+ ) ;
246
+ expect ( packageJson . name ) . toEqual ( "test" ) ;
247
+ expect ( packageJson . version ) . toEqual ( "1.0.0" ) ;
248
+ } ) ;
249
+
250
+ it ( "should create a tsconfig.json and install `workers-types` if none is found and user confirms" , async ( ) => {
251
+ mockConfirm (
252
+ {
253
+ text : "No package.json found. Would you like to create one?" ,
254
+ result : true ,
255
+ } ,
256
+ {
257
+ text : "Would you like to use typescript?" ,
258
+ result : true ,
259
+ }
260
+ ) ;
261
+ await w ( "init" ) ;
262
+ expect ( fs . existsSync ( "./tsconfig.json" ) ) . toBe ( true ) ;
263
+ const tsconfigJson = JSON . parse (
264
+ fs . readFileSync ( "./tsconfig.json" , "utf-8" )
265
+ ) ;
266
+ expect ( tsconfigJson . compilerOptions . types ) . toEqual ( [
267
+ "@cloudflare/workers-types" ,
268
+ ] ) ;
269
+ const packageJson = JSON . parse (
270
+ fs . readFileSync ( "./package.json" , "utf-8" )
271
+ ) ;
272
+ expect ( packageJson . devDependencies ) . toEqual ( {
273
+ "@cloudflare/workers-types" : expect . any ( String ) ,
274
+ } ) ;
275
+ } ) ;
276
+
277
+ it ( "should not touch an existing tsconfig.json in the same directory" , async ( ) => {
278
+ fs . writeFileSync (
279
+ "./package.json" ,
280
+ JSON . stringify ( { name : "test" , version : "1.0.0" } ) ,
281
+ "utf-8"
282
+ ) ;
283
+ fs . writeFileSync (
284
+ "./tsconfig.json" ,
285
+ JSON . stringify ( { compilerOptions : { } } ) ,
286
+ "utf-8"
287
+ ) ;
288
+
289
+ await w ( "init" ) ;
290
+ const tsconfigJson = JSON . parse (
291
+ fs . readFileSync ( "./tsconfig.json" , "utf-8" )
292
+ ) ;
293
+ expect ( tsconfigJson . compilerOptions ) . toEqual ( { } ) ;
294
+ } ) ;
295
+
296
+ it ( "should not touch an existing package.json in an ancestor directory" , async ( ) => {
297
+ fs . writeFileSync (
298
+ "./package.json" ,
299
+ JSON . stringify ( { name : "test" , version : "1.0.0" } ) ,
300
+ "utf-8"
301
+ ) ;
302
+ fs . writeFileSync (
303
+ "./tsconfig.json" ,
304
+ JSON . stringify ( { compilerOptions : { } } ) ,
305
+ "utf-8"
306
+ ) ;
307
+
308
+ fs . mkdirSync ( "./sub-1/sub-2" , { recursive : true } ) ;
309
+ process . chdir ( "./sub-1/sub-2" ) ;
310
+
311
+ await w ( "init" ) ;
312
+ expect ( fs . existsSync ( "./tsconfig.json" ) ) . toBe ( false ) ;
313
+ expect ( fs . existsSync ( "../../tsconfig.json" ) ) . toBe ( true ) ;
314
+
315
+ const tsconfigJson = JSON . parse (
316
+ fs . readFileSync ( "../../tsconfig.json" , "utf-8" )
317
+ ) ;
318
+ expect ( tsconfigJson . compilerOptions ) . toEqual ( { } ) ;
319
+ } ) ;
320
+
181
321
it ( "should error if `--type` is used" , async ( ) => {
182
322
const noValue = await w ( "init --type" ) ;
183
323
expect ( noValue . stderr ) . toMatchInlineSnapshot (
0 commit comments