wrangler 0.0.0-e4e9eaf → 0.0.0-e9a1820

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ ## 🤠 wrangler
2
+
3
+ `wrangler` is a command line tool for building [Cloudflare Workers](https://workers.cloudflare.com/).
4
+
5
+ [(Read the full stack week launch blog post.)](https://blog.cloudflare.com/wrangler-v2-beta/)
6
+
7
+ **DISCLAIMER**: This is a work in progress, and is NOT recommended for use in production. We are opening this preview for feedback from the community, and to openly share our [roadmap](https://github.com/cloudflare/wrangler2/issues/12) for the future. As such, expect APIs and documentation to change before the end of the preview.
8
+
9
+ Further, we will NOT do a general release until we are both feature complete, and have a full backward compatibility and incremental migration plan in place. For more details, follow the [parent roadmap issue](https://github.com/cloudflare/wrangler2/issues/12).
10
+
11
+ ## Quick Start
12
+
13
+ ```bash
14
+ # Make a javascript file
15
+ $ echo "export default { fetch() { return new Response('hello world') } }" > index.js
16
+ # try it out
17
+ $ npx wrangler@beta dev index.js
18
+ # and then publish it
19
+ $ npx wrangler@beta publish index.js --name my-worker
20
+ # visit https://my-worker.<your workers subdomain>.workers.dev
21
+ ```
22
+
23
+ ## Installation:
24
+
25
+ ```bash
26
+ $ npm install wrangler@beta
27
+ ```
28
+
29
+ ## Commands
30
+
31
+ ### `wrangler init [name]`
32
+
33
+ Creates a `wrangler.toml` configuration file. For more details on the configuration keys and values, refer to the [documentation](https://developers.cloudflare.com/workers/cli-wrangler/configuration).
34
+
35
+ ### `wrangler dev [script]`
36
+
37
+ Start a local development server, with live reloading and devtools.
38
+
39
+ ### `wrangler publish [script] --name [name]`
40
+
41
+ Publish the given script to the worldwide Cloudflare network.
42
+
43
+ For more commands and options, refer to the [documentation](https://developers.cloudflare.com/workers/cli-wrangler/commands).
44
+
45
+ ### `wrangler pages dev [directory] [-- command]`
46
+
47
+ Either serves a static build asset directory, or proxies itself in front of a command.
48
+
49
+ Builds and runs functions from a `./functions` directory or uses a `_worker.js` file inside the static build asset directory.
50
+
51
+ For more commands and options, refer to the [documentation](https://developers.cloudflare.com/pages/platform/functions#develop-and-preview-locally) or run `wrangler pages dev --help`.
package/bin/wrangler.js CHANGED
@@ -10,11 +10,11 @@ async function main() {
10
10
  // Note Volta and nvm are also recommended in the official docs:
11
11
  // https://developers.cloudflare.com/workers/get-started/guide#2-install-the-workers-cli
12
12
  console.error(
13
- `Wrangler requires at least Node.js ${MIN_NODE_VERSION}.
13
+ `Wrangler requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}.
14
14
  You should use the latest Node.js version if possible, as Cloudflare Workers use a very up-to-date version of V8.
15
15
  Consider using a Node.js version manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.`
16
16
  );
17
- process.exit(1);
17
+ process.exitCode = 1;
18
18
  return;
19
19
  }
20
20
 
@@ -24,7 +24,7 @@ Consider using a Node.js version manager such as https://volta.sh/ or https://gi
24
24
  "--no-warnings",
25
25
  "--experimental-vm-modules",
26
26
  ...process.execArgv,
27
- join(__dirname, "../wrangler-dist/index_node.js"),
27
+ join(__dirname, "../wrangler-dist/cli.js"),
28
28
  ...process.argv.slice(2),
29
29
  ],
30
30
  { stdio: "inherit" }
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "wrangler",
3
- "version": "0.0.0-e4e9eaf",
3
+ "version": "0.0.0-e9a1820",
4
4
  "author": "wrangler@cloudflare.com",
5
5
  "description": "Command-line interface for all things Cloudflare Workers",
6
- "bin": "./bin/wrangler.js",
6
+ "bin": {
7
+ "wrangler": "./bin/wrangler.js",
8
+ "wrangler2": "./bin/wrangler.js"
9
+ },
7
10
  "license": "MIT OR Apache-2.0",
8
11
  "bugs": {
9
12
  "url": "https://github.com/cloudflare/wrangler/issues"
@@ -33,8 +36,9 @@
33
36
  "cli"
34
37
  ],
35
38
  "dependencies": {
36
- "esbuild": "^0.13.12",
37
- "miniflare": "2.0.0-rc.1",
39
+ "@cloudflare/pages-functions-compiler": "0.3.8",
40
+ "esbuild": "0.14.1",
41
+ "miniflare": "2.0.0-rc.5",
38
42
  "semiver": "^1.1.0"
39
43
  },
40
44
  "optionalDependencies": {
@@ -42,32 +46,39 @@
42
46
  },
43
47
  "devDependencies": {
44
48
  "@iarna/toml": "^2.2.5",
45
- "@types/cloudflare": "^2.7.6",
46
- "@types/react": "^17.0.34",
49
+ "@types/mime": "^2.0.3",
50
+ "@types/react": "^17.0.37",
51
+ "@types/serve-static": "^1.13.10",
47
52
  "@types/signal-exit": "^3.0.1",
48
- "@types/ws": "^8.2.0",
49
- "@types/yargs": "^17.0.5",
53
+ "@types/ws": "^8.2.1",
54
+ "@types/yargs": "^17.0.7",
55
+ "chokidar": "^3.5.2",
50
56
  "clipboardy": "^3.0.0",
51
57
  "command-exists": "^1.2.9",
52
- "execa": "^5.1.1",
58
+ "execa": "^6.0.0",
59
+ "faye-websocket": "^0.11.4",
53
60
  "finalhandler": "^1.1.2",
54
61
  "find-up": "^6.2.0",
55
62
  "formdata-node": "^4.3.1",
56
- "http-proxy": "^1.18.1",
57
63
  "ink": "^3.2.0",
58
- "ink-select-input": "^4.2.0",
64
+ "ink-select-input": "^4.2.1",
59
65
  "ink-table": "^3.0.0",
60
- "ink-text-input": "^4.0.1",
61
- "node-fetch": "github:tekwiz/node-fetch#fix/redirect-with-empty-chunked-transfer-encoding",
66
+ "ink-text-input": "^4.0.2",
67
+ "mime": "^3.0.0",
68
+ "node-fetch": "^3.1.0",
62
69
  "open": "^8.4.0",
70
+ "path-to-regexp": "^6.2.0",
63
71
  "react": "^17.0.2",
72
+ "react-error-boundary": "^3.1.4",
64
73
  "serve-static": "^1.14.1",
65
- "signal-exit": "^3.0.5",
74
+ "signal-exit": "^3.0.6",
66
75
  "tmp-promise": "^3.0.3",
67
- "ws": "^8.2.3",
68
- "yargs": "^17.2.1"
76
+ "undici": "^4.11.1",
77
+ "ws": "^8.3.0",
78
+ "yargs": "^17.3.0"
69
79
  },
70
80
  "files": [
81
+ "src",
71
82
  "bin",
72
83
  "miniflare-config-stubs",
73
84
  "wrangler-dist",
@@ -80,22 +91,30 @@
80
91
  "bundle": "node -r esbuild-register scripts/bundle.ts",
81
92
  "build": "npm run clean && npm run bundle",
82
93
  "start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
83
- "test": "npm run build && NODE_OPTIONS=--enable-source-maps jest --silent=false"
94
+ "test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true"
84
95
  },
85
96
  "engines": {
86
- "node": ">=16.0.0"
97
+ "node": ">=16.7.0"
87
98
  },
88
99
  "jest": {
100
+ "restoreMocks": true,
101
+ "testRegex": ".*.(test|spec)\\.[jt]sx?$",
102
+ "transformIgnorePatterns": [
103
+ "node_modules/(?!node-fetch|fetch-blob|find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|data-uri-to-buffer|formdata-polyfill|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)"
104
+ ],
105
+ "moduleNameMapper": {
106
+ "clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
107
+ },
89
108
  "transform": {
90
- "^.+\\.tsx?$": [
109
+ "^.+\\.c?(t|j)sx?$": [
91
110
  "esbuild-jest",
92
111
  {
93
- "sourcemap": true,
94
- "loaders": {
95
- ".spec.ts": "tsx"
96
- }
112
+ "sourcemap": true
97
113
  }
98
114
  ]
99
- }
115
+ },
116
+ "setupFilesAfterEnv": [
117
+ "<rootDir>/src/__tests__/jest.setup.ts"
118
+ ]
100
119
  }
101
- }
120
+ }
@@ -0,0 +1,4 @@
1
+ // We use this mock during tests because we're having trouble
2
+ // properly compiling clipboardy for jest
3
+ // We can revisit this if and when we write tests that test
4
+ // the copying part of `dev`
@@ -0,0 +1,287 @@
1
+ import * as fsp from "node:fs/promises";
2
+ import * as TOML from "@iarna/toml";
3
+ import { mockConfirm } from "./mock-dialogs";
4
+ import { runWrangler } from "./run-wrangler";
5
+ import { runInTempDir } from "./run-in-tmp";
6
+ import * as fs from "node:fs";
7
+
8
+ describe("wrangler", () => {
9
+ runInTempDir();
10
+
11
+ describe("no command", () => {
12
+ it("should display a list of available commands", async () => {
13
+ const { stdout, stderr } = await runWrangler();
14
+
15
+ expect(stdout).toMatchInlineSnapshot(`
16
+ "wrangler
17
+
18
+ Commands:
19
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
20
+ wrangler dev <filename> 👂 Start a local server for developing your worker
21
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
22
+ wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
23
+ wrangler secret 🤫 Generate a secret that can be referenced in the worker script
24
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
25
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
26
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
27
+ wrangler pages ⚡️ Configure Cloudflare Pages
28
+
29
+ Flags:
30
+ -c, --config Path to .toml configuration file [string]
31
+ -h, --help Show help [boolean]
32
+ -v, --version Show version number [boolean]
33
+
34
+ Options:
35
+ -l, --local Run on my machine [boolean] [default: false]"
36
+ `);
37
+
38
+ expect(stderr).toMatchInlineSnapshot(`""`);
39
+ });
40
+ });
41
+
42
+ describe("invalid command", () => {
43
+ it("should display an error", async () => {
44
+ const { error, stdout, stderr } = await runWrangler("invalid-command");
45
+
46
+ expect(stdout).toMatchInlineSnapshot(`""`);
47
+ expect(stderr).toMatchInlineSnapshot(`
48
+ "wrangler
49
+
50
+ Commands:
51
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
52
+ wrangler dev <filename> 👂 Start a local server for developing your worker
53
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
54
+ wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
55
+ wrangler secret 🤫 Generate a secret that can be referenced in the worker script
56
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
57
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
58
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
59
+ wrangler pages ⚡️ Configure Cloudflare Pages
60
+
61
+ Flags:
62
+ -c, --config Path to .toml configuration file [string]
63
+ -h, --help Show help [boolean]
64
+ -v, --version Show version number [boolean]
65
+
66
+ Options:
67
+ -l, --local Run on my machine [boolean] [default: false]
68
+
69
+ Unknown command: invalid-command."
70
+ `);
71
+ expect(error).toMatchInlineSnapshot(
72
+ `[Error: Unknown command: invalid-command.]`
73
+ );
74
+ });
75
+ });
76
+
77
+ describe("init", () => {
78
+ it("should create a wrangler.toml", async () => {
79
+ mockConfirm({
80
+ text: "No package.json found. Would you like to create one?",
81
+ result: false,
82
+ });
83
+ await runWrangler("init");
84
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
85
+ expect(typeof parsed.compatibility_date).toBe("string");
86
+ expect(fs.existsSync("./package.json")).toBe(false);
87
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
88
+ });
89
+
90
+ it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
91
+ fs.writeFileSync("./wrangler.toml", "", "utf-8");
92
+ mockConfirm({
93
+ text: "Do you want to continue initializing this project?",
94
+ result: false,
95
+ });
96
+ const { warnings } = await runWrangler("init");
97
+ expect(warnings).toContain("wrangler.toml file already exists!");
98
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
99
+ expect(typeof parsed.compatibility_date).toBe("undefined");
100
+ });
101
+
102
+ it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
103
+ fs.writeFileSync("./wrangler.toml", "", "utf-8");
104
+ mockConfirm(
105
+ {
106
+ text: "Do you want to continue initializing this project?",
107
+ result: true,
108
+ },
109
+ {
110
+ text: "No package.json found. Would you like to create one?",
111
+ result: false,
112
+ }
113
+ );
114
+ const { warnings } = await runWrangler("init");
115
+ expect(warnings).toContain("wrangler.toml file already exists!");
116
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
117
+ expect(typeof parsed.compatibility_date).toBe("string");
118
+ });
119
+
120
+ it("should create a package.json if none is found and user confirms", async () => {
121
+ mockConfirm(
122
+ {
123
+ text: "No package.json found. Would you like to create one?",
124
+ result: true,
125
+ },
126
+ {
127
+ text: "Would you like to use typescript?",
128
+ result: false,
129
+ }
130
+ );
131
+ await runWrangler("init");
132
+ expect(fs.existsSync("./package.json")).toBe(true);
133
+ const packageJson = JSON.parse(
134
+ fs.readFileSync("./package.json", "utf-8")
135
+ );
136
+ expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
137
+ expect(packageJson.version).toEqual("0.0.1");
138
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
139
+ });
140
+
141
+ it("should not touch an existing package.json in the same directory", async () => {
142
+ mockConfirm({
143
+ text: "Would you like to use typescript?",
144
+ result: false,
145
+ });
146
+
147
+ fs.writeFileSync(
148
+ "./package.json",
149
+ JSON.stringify({ name: "test", version: "1.0.0" }),
150
+ "utf-8"
151
+ );
152
+
153
+ await runWrangler("init");
154
+ const packageJson = JSON.parse(
155
+ fs.readFileSync("./package.json", "utf-8")
156
+ );
157
+ expect(packageJson.name).toEqual("test");
158
+ expect(packageJson.version).toEqual("1.0.0");
159
+ });
160
+
161
+ it("should not touch an existing package.json in an ancestor directory", async () => {
162
+ mockConfirm({
163
+ text: "Would you like to use typescript?",
164
+ result: false,
165
+ });
166
+
167
+ fs.writeFileSync(
168
+ "./package.json",
169
+ JSON.stringify({ name: "test", version: "1.0.0" }),
170
+ "utf-8"
171
+ );
172
+
173
+ fs.mkdirSync("./sub-1/sub-2", { recursive: true });
174
+ process.chdir("./sub-1/sub-2");
175
+
176
+ await runWrangler("init");
177
+ expect(fs.existsSync("./package.json")).toBe(false);
178
+ expect(fs.existsSync("../../package.json")).toBe(true);
179
+
180
+ const packageJson = JSON.parse(
181
+ fs.readFileSync("../../package.json", "utf-8")
182
+ );
183
+ expect(packageJson.name).toEqual("test");
184
+ expect(packageJson.version).toEqual("1.0.0");
185
+ });
186
+
187
+ it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
188
+ mockConfirm(
189
+ {
190
+ text: "No package.json found. Would you like to create one?",
191
+ result: true,
192
+ },
193
+ {
194
+ text: "Would you like to use typescript?",
195
+ result: true,
196
+ }
197
+ );
198
+ await runWrangler("init");
199
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
200
+ const tsconfigJson = JSON.parse(
201
+ fs.readFileSync("./tsconfig.json", "utf-8")
202
+ );
203
+ expect(tsconfigJson.compilerOptions.types).toEqual([
204
+ "@cloudflare/workers-types",
205
+ ]);
206
+ const packageJson = JSON.parse(
207
+ fs.readFileSync("./package.json", "utf-8")
208
+ );
209
+ expect(packageJson.devDependencies).toEqual({
210
+ "@cloudflare/workers-types": expect.any(String),
211
+ });
212
+ });
213
+
214
+ it("should not touch an existing tsconfig.json in the same directory", async () => {
215
+ fs.writeFileSync(
216
+ "./package.json",
217
+ JSON.stringify({ name: "test", version: "1.0.0" }),
218
+ "utf-8"
219
+ );
220
+ fs.writeFileSync(
221
+ "./tsconfig.json",
222
+ JSON.stringify({ compilerOptions: {} }),
223
+ "utf-8"
224
+ );
225
+
226
+ await runWrangler("init");
227
+ const tsconfigJson = JSON.parse(
228
+ fs.readFileSync("./tsconfig.json", "utf-8")
229
+ );
230
+ expect(tsconfigJson.compilerOptions).toEqual({});
231
+ });
232
+
233
+ it("should not touch an existing package.json in an ancestor directory", async () => {
234
+ fs.writeFileSync(
235
+ "./package.json",
236
+ JSON.stringify({ name: "test", version: "1.0.0" }),
237
+ "utf-8"
238
+ );
239
+ fs.writeFileSync(
240
+ "./tsconfig.json",
241
+ JSON.stringify({ compilerOptions: {} }),
242
+ "utf-8"
243
+ );
244
+
245
+ fs.mkdirSync("./sub-1/sub-2", { recursive: true });
246
+ process.chdir("./sub-1/sub-2");
247
+
248
+ await runWrangler("init");
249
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
250
+ expect(fs.existsSync("../../tsconfig.json")).toBe(true);
251
+
252
+ const tsconfigJson = JSON.parse(
253
+ fs.readFileSync("../../tsconfig.json", "utf-8")
254
+ );
255
+ expect(tsconfigJson.compilerOptions).toEqual({});
256
+ });
257
+
258
+ it("should error if `--type` is used", async () => {
259
+ const { error } = await runWrangler("init --type");
260
+ expect(error).toMatchInlineSnapshot(
261
+ `[Error: The --type option is no longer supported.]`
262
+ );
263
+ });
264
+
265
+ it("should error if `--type javascript` is used", async () => {
266
+ const { error } = await runWrangler("init --type javascript");
267
+ expect(error).toMatchInlineSnapshot(
268
+ `[Error: The --type option is no longer supported.]`
269
+ );
270
+ });
271
+
272
+ it("should error if `--type rust` is used", async () => {
273
+ const { error } = await runWrangler("init --type rust");
274
+ expect(error).toMatchInlineSnapshot(
275
+ `[Error: The --type option is no longer supported.]`
276
+ );
277
+ });
278
+
279
+ it("should error if `--type webpack` is used", async () => {
280
+ const { error } = await runWrangler("init --type webpack");
281
+ expect(error).toMatchInlineSnapshot(`
282
+ [Error: The --type option is no longer supported.
283
+ If you wish to use webpack then you will need to create a custom build.]
284
+ `);
285
+ });
286
+ });
287
+ });
@@ -0,0 +1,18 @@
1
+ import { confirm, prompt } from "../dialogs";
2
+
3
+ jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));
4
+ jest.mock("../dialogs");
5
+
6
+ // By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
7
+ (confirm as jest.Mock).mockImplementation(() => {
8
+ throw new Error(
9
+ "Unexpected call to `confirm()`. You should use `mockConfirm()` to mock calls to `confirm()` with expectations. Search the codebase for `mockConfirm` to learn more."
10
+ );
11
+ });
12
+
13
+ // By default (if not configured by mockPrompt()) calls to `prompt()` should throw.
14
+ (prompt as jest.Mock).mockImplementation(() => {
15
+ throw new Error(
16
+ "Unexpected call to `prompt()`. You should use `mockPrompt()` to mock calls to `prompt()` with expectations. Search the codebase for `mockPrompt` to learn more."
17
+ );
18
+ });