Skip to content

Commit

Permalink
treat "name" in `wrangler init [name] as a path (#877)
Browse files Browse the repository at this point in the history
Treat the "name" parameter in wrangler init as a path.

This means that running wrangler init . will create a worker in the current directory,
and the worker's name will be the name of the current directory.

You can also run wrangler init path/to/my-worker and a worker will be created at
[CWD]/path/to/my-worker with the name my-worker,
  • Loading branch information
Cass Fridkin committed May 2, 2022
1 parent 7a1eb41 commit 97f945f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .changeset/dry-walls-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler": patch
---

Treat the "name" parameter in `wrangler init` as a path.

This means that running `wrangler init .` will create a worker in the current directory,
and the worker's name will be the name of the current directory.

You can also run `wrangler init path/to/my-worker` and a worker will be created at
`[CWD]/path/to/my-worker` with the name `my-worker`,
53 changes: 53 additions & 0 deletions packages/wrangler/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "node:fs";
import * as fsp from "node:fs/promises";
import path from "node:path";
import * as TOML from "@iarna/toml";
import { parseConfigFileTextToJson } from "typescript";
import { version as wranglerVersion } from "../../package.json";
Expand Down Expand Up @@ -777,4 +778,56 @@ describe("init", () => {
expect(fs.existsSync("./src/index.ts")).toBe(false);
});
});

describe("worker names", () => {
it("should create a worker with a given name", async () => {
await runWrangler("init my-worker -y");

const parsed = TOML.parse(
await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
);

expect(typeof parsed.compatibility_date).toBe("string");
expect(parsed.name).toBe("my-worker");
});

it('should create a worker with the name of the current directory if "name" is .', async () => {
await runWrangler("init . -y");

const parsed = TOML.parse(await fsp.readFile("wrangler.toml", "utf-8"));

expect(typeof parsed.compatibility_date).toBe("string");
expect(parsed.name).toBe(path.basename(process.cwd()).toLowerCase());
expect(fs.existsSync("./my-worker/package.json")).toBe(false);
expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
});

it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
await runWrangler("init path/to/worker -y");

const parsed = TOML.parse(
await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
);

expect(typeof parsed.compatibility_date).toBe("string");
expect(parsed.name).toBe("worker");
expect(fs.existsSync("./my-worker/package.json")).toBe(false);
expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
});

it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
const parsed = TOML.parse(
await fsp.readFile(
"WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml",
"utf-8"
)
);

expect(typeof parsed.compatibility_date).toBe("string");
expect(parsed.name).toBe("weird_w0rkr_n4m3-js-tsx-tar-gz");
expect(fs.existsSync("./my-worker/package.json")).toBe(false);
expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
});
});
});
10 changes: 8 additions & 2 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ export async function main(argv: string[]): Promise<void> {
// TODO: make sure args.name is a valid identifier for a worker name

const creationDirectory = path.join(process.cwd(), args.name ?? "");
const workerName = path
.basename(creationDirectory)
.toLowerCase()
.replaceAll(/[^a-z0-9\-_]/gm, "-");

const packageManager = await getPackageManager(creationDirectory);

Expand All @@ -322,8 +326,7 @@ export async function main(argv: string[]): Promise<void> {
"./wrangler.toml"
);
let justCreatedWranglerToml = false;
const workerName =
args.name || path.basename(path.resolve(process.cwd()));

if (fs.existsSync(wranglerTomlDestination)) {
logger.warn(`${wranglerTomlDestination} file already exists!`);
const shouldContinue = await confirm(
Expand All @@ -335,6 +338,7 @@ export async function main(argv: string[]): Promise<void> {
} else {
await mkdir(creationDirectory, { recursive: true });
const compatibilityDate = new Date().toISOString().substring(0, 10);

try {
await writeFile(
wranglerTomlDestination,
Expand All @@ -343,6 +347,7 @@ export async function main(argv: string[]): Promise<void> {
compatibility_date: compatibilityDate,
}) + "\n"
);

logger.log(`✨ Successfully created wrangler.toml`);
justCreatedWranglerToml = true;
} catch (err) {
Expand Down Expand Up @@ -379,6 +384,7 @@ export async function main(argv: string[]): Promise<void> {
" "
) + "\n"
);

await packageManager.install();
logger.log(`✨ Created package.json`);
pathToPackageJson = path.join(creationDirectory, "package.json");
Expand Down

0 comments on commit 97f945f

Please sign in to comment.