Skip to content

Commit f662485

Browse files
committed
polish: Added scripts to package.json
To get wrangler init projects up and running with good ergonomics for deploying and development, added default scripts "start" & "deploy" with assumed TS or JS files in generated ./src/index Provide messages of how to utilize the scripts or npx wrangler based on if the scripts were generated.
1 parent 40d9553 commit f662485

File tree

3 files changed

+259
-3
lines changed

3 files changed

+259
-3
lines changed

.changeset/orange-hats-yell.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
feat: add scripts to package.json & autogenerate name value when initializing a project
6+
To get wrangler init projects up and running with good ergonomics for deploying and development,
7+
added default scripts "start" & "deploy" with assumed TS or JS files in generated ./src/index.
8+
The name property is now derived from user input on `init <name>` or parent directory if no input is provided.

packages/wrangler/src/__tests__/index.test.ts

+193-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe("wrangler", () => {
163163
const packageJson = JSON.parse(
164164
fs.readFileSync("./package.json", "utf-8")
165165
);
166-
expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
166+
expect(packageJson.name).toContain("wrangler-tests");
167167
expect(packageJson.version).toEqual("0.0.0");
168168
expect(packageJson.devDependencies).toEqual({
169169
wrangler: expect.any(String),
@@ -172,6 +172,28 @@ describe("wrangler", () => {
172172
expect(mockPackageManager.install).toHaveBeenCalled();
173173
});
174174

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+
175197
it("should not touch an existing package.json in the same directory", async () => {
176198
mockConfirm(
177199
{
@@ -329,6 +351,176 @@ describe("wrangler", () => {
329351
expect(fs.existsSync("./src/index.ts")).toBe(true);
330352
});
331353

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 overwrite package.json 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+
332524
it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
333525
mockConfirm(
334526
{

packages/wrangler/src/index.tsx

+58-2
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,18 @@ export async function main(argv: string[]): Promise<void> {
217217
}
218218

219219
let pathToPackageJson = await findUp("package.json");
220+
let shouldCreatePackageJson = false;
220221
if (!pathToPackageJson) {
221222
// If no package.json exists, ask to create one
222-
const shouldCreatePackageJson = await confirm(
223+
shouldCreatePackageJson = await confirm(
223224
"No package.json found. Would you like to create one?"
224225
);
225226
if (shouldCreatePackageJson) {
226227
await writeFile(
227228
"./package.json",
228229
JSON.stringify(
229230
{
230-
name: "worker",
231+
name: args.name || path.basename(path.resolve(process.cwd())),
231232
version: "0.0.0",
232233
devDependencies: {
233234
wrangler: wranglerVersion,
@@ -333,6 +334,47 @@ export async function main(argv: string[]): Promise<void> {
333334
}
334335
}
335336

337+
const packageJsonContent = JSON.parse(
338+
await readFile(pathToPackageJson, "utf-8")
339+
);
340+
const shouldWritePackageJsonScripts =
341+
!packageJsonContent.scripts?.start &&
342+
!packageJsonContent.scripts?.deploy &&
343+
shouldCreatePackageJson;
344+
async function writePackageJsonScripts(
345+
isWritingScripts: boolean,
346+
packagePath: string,
347+
scriptPath: string
348+
) {
349+
if (isWritingScripts) {
350+
await writeFile(
351+
packagePath,
352+
JSON.stringify(
353+
{
354+
...packageJsonContent,
355+
scripts: {
356+
...packageJsonContent.scripts,
357+
start: `wrangler dev ${scriptPath}`,
358+
deploy: `wrangler publish ${scriptPath}`,
359+
},
360+
},
361+
null,
362+
" "
363+
) + "\n"
364+
);
365+
console.log(`To start developing on your worker, run npm start.`);
366+
console.log(
367+
`To publish your worker on to the internet, run npm run deploy.`
368+
);
369+
} else {
370+
console.log(
371+
`To start developing on your worker, npx wrangler dev ${scriptPath}`
372+
);
373+
console.log(
374+
`To publish your worker on to the internet, npx wrangler publish ${scriptPath}`
375+
);
376+
}
377+
}
336378
if (isTypescriptProject) {
337379
if (!fs.existsSync("./src/index.ts")) {
338380
const shouldCreateSource = await confirm(
@@ -347,6 +389,13 @@ export async function main(argv: string[]): Promise<void> {
347389
"utf-8"
348390
)
349391
);
392+
393+
await writePackageJsonScripts(
394+
shouldWritePackageJsonScripts,
395+
pathToPackageJson,
396+
"src/index.ts"
397+
);
398+
350399
console.log(`✨ Created src/index.ts`);
351400
}
352401
}
@@ -364,6 +413,13 @@ export async function main(argv: string[]): Promise<void> {
364413
"utf-8"
365414
)
366415
);
416+
417+
await writePackageJsonScripts(
418+
shouldWritePackageJsonScripts,
419+
pathToPackageJson,
420+
"src/index.js"
421+
);
422+
367423
console.log(`✨ Created src/index.js`);
368424
}
369425
}

0 commit comments

Comments
 (0)