Skip to content

Commit

Permalink
polish: Added scripts to package.json
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JacobMGEvans committed Feb 8, 2022
1 parent 40d9553 commit 2e90013
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .changeset/orange-hats-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": patch
---

polish: Added scripts to package.json & autogenerated name value
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.
The name property is now derived from user input on `init <name>` or parent directory if no input is provided.
194 changes: 193 additions & 1 deletion packages/wrangler/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe("wrangler", () => {
const packageJson = JSON.parse(
fs.readFileSync("./package.json", "utf-8")
);
expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
expect(packageJson.name).toContain("wrangler-tests");
expect(packageJson.version).toEqual("0.0.0");
expect(packageJson.devDependencies).toEqual({
wrangler: expect.any(String),
Expand All @@ -172,6 +172,28 @@ describe("wrangler", () => {
expect(mockPackageManager.install).toHaveBeenCalled();
});

it("should create a package.json if none is found and user confirms", async () => {
mockConfirm(
{
text: "No package.json found. Would you like to create one?",
result: true,
},
{
text: "Would you like to use TypeScript?",
result: false,
},
{
text: "Would you like to create a Worker at src/index.js?",
result: false,
}
);
await runWrangler("init my-worker");
const packageJson = JSON.parse(
fs.readFileSync("./package.json", "utf-8")
);
expect(packageJson.name).toBe("my-worker");
});

it("should not touch an existing package.json in the same directory", async () => {
mockConfirm(
{
Expand Down Expand Up @@ -329,6 +351,176 @@ describe("wrangler", () => {
expect(fs.existsSync("./src/index.ts")).toBe(true);
});

it("should add scripts for a typescript project with .ts extension", async () => {
mockConfirm(
{
text: "No package.json found. Would you like to create one?",
result: true,
},
{
text: "Would you like to install wrangler into your package.json?",
result: false,
},
{
text: "Would you like to use TypeScript?",
result: true,
},
{
text: "Would you like to create a Worker at src/index.ts?",
result: true,
}
);
await runWrangler("init");

expect(fs.existsSync("./package.json")).toBe(true);
const packageJson = JSON.parse(
fs.readFileSync("./package.json", "utf-8")
);

expect(fs.existsSync("./src/index.js")).toBe(false);
expect(fs.existsSync("./src/index.ts")).toBe(true);

expect(packageJson.scripts.start).toBe("wrangler dev src/index.ts");
expect(packageJson.scripts.deploy).toBe("wrangler publish src/index.ts");
expect(packageJson.name).toContain("wrangler-tests");
expect(packageJson.version).toEqual("0.0.0");
expect(std.out).toMatchInlineSnapshot(`
"✨ Successfully created wrangler.toml
✨ Created package.json
✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
To start developing on your worker, run npm start.
To publish your worker on to the internet, run npm run deploy.
✨ Created src/index.ts"
`);
});

it("should not add scripts for a typescript project with .ts extension", async () => {
mockConfirm(
{
text: "Would you like to install wrangler into your package.json?",
result: false,
},
{
text: "Would you like to use TypeScript?",
result: true,
},
{
text: "Would you like to create a Worker at src/index.ts?",
result: true,
}
);
await fsp.writeFile(
"./package.json",
JSON.stringify({
scripts: {
start: "test-start",
deploy: "test-deploy",
},
})
);
const packageJson = JSON.parse(
fs.readFileSync("./package.json", "utf-8")
);
await runWrangler("init");

expect(fs.existsSync("./src/index.js")).toBe(false);
expect(fs.existsSync("./src/index.ts")).toBe(true);

expect(packageJson.scripts.start).toBe("test-start");
expect(packageJson.scripts.deploy).toBe("test-deploy");
expect(std.out).toMatchInlineSnapshot(`
"✨ Successfully created wrangler.toml
✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
To start developing on your worker, npx wrangler dev src/index.ts
To publish your worker on to the internet, npx wrangler publish src/index.ts
✨ Created src/index.ts"
`);
});

it("should add missing scripts for a non-ts project with .js extension", async () => {
mockConfirm(
{
text: "No package.json found. Would you like to create one?",
result: true,
},
{
text: "Would you like to install wrangler into your package.json?",
result: false,
},
{
text: "Would you like to use TypeScript?",
result: false,
},
{
text: "Would you like to create a Worker at src/index.js?",
result: true,
}
);
await runWrangler("init");

expect(fs.existsSync("./package.json")).toBe(true);
const packageJson = JSON.parse(
fs.readFileSync("./package.json", "utf-8")
);

expect(fs.existsSync("./src/index.js")).toBe(true);
expect(fs.existsSync("./src/index.ts")).toBe(false);

expect(packageJson.scripts.start).toBe("wrangler dev src/index.js");
expect(packageJson.scripts.deploy).toBe("wrangler publish src/index.js");
expect(packageJson.name).toContain("wrangler-tests");
expect(packageJson.version).toEqual("0.0.0");
expect(std.out).toMatchInlineSnapshot(`
"✨ Successfully created wrangler.toml
✨ Created package.json
To start developing on your worker, run npm start.
To publish your worker on to the internet, run npm run deploy.
✨ Created src/index.js"
`);
});

it("should not add scripts for a non-ts project with .js extension", async () => {
mockConfirm(
{
text: "Would you like to install wrangler into your package.json?",
result: false,
},
{
text: "Would you like to use TypeScript?",
result: false,
},
{
text: "Would you like to create a Worker at src/index.js?",
result: true,
}
);
await fsp.writeFile(
"./package.json",
JSON.stringify({
scripts: {
start: "test-start",
deploy: "test-deploy",
},
})
);
const packageJson = JSON.parse(
fs.readFileSync("./package.json", "utf-8")
);
await runWrangler("init");

expect(fs.existsSync("./src/index.js")).toBe(true);
expect(fs.existsSync("./src/index.ts")).toBe(false);

expect(packageJson.scripts.start).toBe("test-start");
expect(packageJson.scripts.deploy).toBe("test-deploy");
expect(std.out).toMatchInlineSnapshot(`
"✨ Successfully created wrangler.toml
To start developing on your worker, npx wrangler dev src/index.js
To publish your worker on to the internet, npx wrangler publish src/index.js
✨ Created src/index.js"
`);
});

it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
mockConfirm(
{
Expand Down
75 changes: 73 additions & 2 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,18 @@ export async function main(argv: string[]): Promise<void> {
}

let pathToPackageJson = await findUp("package.json");
let shouldCreatePackageJson: boolean | undefined;
if (!pathToPackageJson) {
// If no package.json exists, ask to create one
const shouldCreatePackageJson = await confirm(
shouldCreatePackageJson = await confirm(
"No package.json found. Would you like to create one?"
);
if (shouldCreatePackageJson) {
await writeFile(
"./package.json",
JSON.stringify(
{
name: "worker",
name: args.name || path.basename(path.resolve(process.cwd())),
version: "0.0.0",
devDependencies: {
wrangler: wranglerVersion,
Expand Down Expand Up @@ -333,6 +334,9 @@ export async function main(argv: string[]): Promise<void> {
}
}

const packageJsonContent = JSON.parse(
await readFile(pathToPackageJson, "utf-8")
);
if (isTypescriptProject) {
if (!fs.existsSync("./src/index.ts")) {
const shouldCreateSource = await confirm(
Expand All @@ -347,6 +351,39 @@ export async function main(argv: string[]): Promise<void> {
"utf-8"
)
);
if (
!packageJsonContent.scripts?.start &&
!packageJsonContent.scripts?.deploy &&
shouldCreatePackageJson
) {
await writeFile(
pathToPackageJson,
JSON.stringify(
{
...packageJsonContent,
scripts: {
...packageJsonContent.scripts,
start: "wrangler dev src/index.ts",
deploy: "wrangler publish src/index.ts",
},
},
null,
" "
) + "\n"
);
console.log(`To start developing on your worker, run npm start.`);
console.log(
`To publish your worker on to the internet, run npm run deploy.`
);
} else {
console.log(
`To start developing on your worker, npx wrangler dev src/index.ts`
);
console.log(
`To publish your worker on to the internet, npx wrangler publish src/index.ts`
);
}

console.log(`✨ Created src/index.ts`);
}
}
Expand All @@ -364,6 +401,40 @@ export async function main(argv: string[]): Promise<void> {
"utf-8"
)
);

if (
!packageJsonContent.scripts?.start &&
!packageJsonContent.scripts?.deploy &&
shouldCreatePackageJson
) {
await writeFile(
pathToPackageJson,
JSON.stringify(
{
...packageJsonContent,
scripts: {
...packageJsonContent.scripts,
start: "wrangler dev src/index.js",
deploy: "wrangler publish src/index.js",
},
},
null,
" "
) + "\n"
);
console.log(`To start developing on your worker, run npm start.`);
console.log(
`To publish your worker on to the internet, run npm run deploy.`
);
} else {
console.log(
`To start developing on your worker, npx wrangler dev src/index.js`
);
console.log(
`To publish your worker on to the internet, npx wrangler publish src/index.js`
);
}

console.log(`✨ Created src/index.js`);
}
}
Expand Down

0 comments on commit 2e90013

Please sign in to comment.