wrangler 0.0.0-c9dc473 → 0.0.0-ce2d7d1
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 +51 -0
- package/package.json +28 -23
- package/src/__tests__/index.test.ts +329 -91
- package/src/__tests__/jest.setup.ts +17 -0
- package/src/__tests__/mock-cfetch.js +11 -15
- package/src/__tests__/mock-dialogs.ts +65 -0
- package/src/api/form_data.ts +75 -70
- package/src/api/inspect.ts +0 -11
- package/src/api/preview.ts +28 -23
- package/src/api/worker.ts +46 -31
- package/src/config.ts +39 -4
- package/src/dev.tsx +307 -102
- package/src/dialogs.tsx +2 -10
- package/src/index.tsx +189 -49
- package/src/kv.tsx +9 -5
- package/src/module-collection.ts +64 -0
- package/src/pages.tsx +583 -80
- package/src/proxy.ts +104 -0
- package/src/publish.ts +105 -33
- package/src/sites.tsx +1 -1
- package/src/tail.tsx +1 -1
- package/src/user.tsx +1 -1
- package/wrangler-dist/cli.js +125526 -533
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/util/fetch.ts +0 -74
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrangler",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-ce2d7d1",
|
|
4
4
|
"author": "wrangler@cloudflare.com",
|
|
5
5
|
"description": "Command-line interface for all things Cloudflare Workers",
|
|
6
6
|
"bin": {
|
|
@@ -36,45 +36,46 @@
|
|
|
36
36
|
"cli"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@cloudflare/pages-functions-compiler": "0.3.
|
|
40
|
-
"esbuild": "0.
|
|
41
|
-
"miniflare": "2.0.0-rc.
|
|
42
|
-
"semiver": "^1.1.0"
|
|
43
|
-
"serve": "^13.0.2"
|
|
39
|
+
"@cloudflare/pages-functions-compiler": "0.3.8",
|
|
40
|
+
"esbuild": "0.14.1",
|
|
41
|
+
"miniflare": "2.0.0-rc.4",
|
|
42
|
+
"semiver": "^1.1.0"
|
|
44
43
|
},
|
|
45
44
|
"optionalDependencies": {
|
|
46
45
|
"fsevents": "~2.3.2"
|
|
47
46
|
},
|
|
48
47
|
"devDependencies": {
|
|
49
48
|
"@iarna/toml": "^2.2.5",
|
|
50
|
-
"@types/
|
|
51
|
-
"@types/
|
|
52
|
-
"@types/
|
|
49
|
+
"@types/mime": "^2.0.3",
|
|
50
|
+
"@types/react": "^17.0.37",
|
|
51
|
+
"@types/serve-static": "^1.13.10",
|
|
53
52
|
"@types/signal-exit": "^3.0.1",
|
|
54
|
-
"@types/ws": "^8.2.
|
|
55
|
-
"@types/yargs": "^17.0.
|
|
53
|
+
"@types/ws": "^8.2.1",
|
|
54
|
+
"@types/yargs": "^17.0.7",
|
|
55
|
+
"chokidar": "^3.5.2",
|
|
56
56
|
"clipboardy": "^3.0.0",
|
|
57
57
|
"command-exists": "^1.2.9",
|
|
58
|
-
"execa": "^
|
|
59
|
-
"
|
|
58
|
+
"execa": "^6.0.0",
|
|
59
|
+
"faye-websocket": "^0.11.4",
|
|
60
60
|
"finalhandler": "^1.1.2",
|
|
61
61
|
"find-up": "^6.2.0",
|
|
62
62
|
"formdata-node": "^4.3.1",
|
|
63
|
-
"http-proxy": "^1.18.1",
|
|
64
|
-
"http-proxy-middleware": "^2.0.1",
|
|
65
63
|
"ink": "^3.2.0",
|
|
66
|
-
"ink-select-input": "^4.2.
|
|
64
|
+
"ink-select-input": "^4.2.1",
|
|
67
65
|
"ink-table": "^3.0.0",
|
|
68
|
-
"ink-text-input": "^4.0.
|
|
66
|
+
"ink-text-input": "^4.0.2",
|
|
67
|
+
"mime": "^3.0.0",
|
|
69
68
|
"node-fetch": "^3.1.0",
|
|
70
69
|
"open": "^8.4.0",
|
|
71
70
|
"path-to-regexp": "^6.2.0",
|
|
72
71
|
"react": "^17.0.2",
|
|
72
|
+
"react-error-boundary": "^3.1.4",
|
|
73
73
|
"serve-static": "^1.14.1",
|
|
74
|
-
"signal-exit": "^3.0.
|
|
74
|
+
"signal-exit": "^3.0.6",
|
|
75
75
|
"tmp-promise": "^3.0.3",
|
|
76
|
-
"
|
|
77
|
-
"
|
|
76
|
+
"undici": "^4.11.1",
|
|
77
|
+
"ws": "^8.3.0",
|
|
78
|
+
"yargs": "^17.3.0"
|
|
78
79
|
},
|
|
79
80
|
"files": [
|
|
80
81
|
"src",
|
|
@@ -93,12 +94,13 @@
|
|
|
93
94
|
"test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true"
|
|
94
95
|
},
|
|
95
96
|
"engines": {
|
|
96
|
-
"node": ">=16.
|
|
97
|
+
"node": ">=16.7.0"
|
|
97
98
|
},
|
|
98
99
|
"jest": {
|
|
100
|
+
"restoreMocks": true,
|
|
99
101
|
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
|
|
100
102
|
"transformIgnorePatterns": [
|
|
101
|
-
"node_modules/(?!node-fetch|fetch-blob|find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|data-uri-to-buffer|formdata-polyfill)"
|
|
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)"
|
|
102
104
|
],
|
|
103
105
|
"moduleNameMapper": {
|
|
104
106
|
"clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
|
|
@@ -110,6 +112,9 @@
|
|
|
110
112
|
"sourcemap": true
|
|
111
113
|
}
|
|
112
114
|
]
|
|
113
|
-
}
|
|
115
|
+
},
|
|
116
|
+
"setupFilesAfterEnv": [
|
|
117
|
+
"<rootDir>/src/__tests__/jest.setup.ts"
|
|
118
|
+
]
|
|
114
119
|
}
|
|
115
120
|
}
|
|
@@ -1,153 +1,391 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import * as fsp from "node:fs/promises";
|
|
3
|
+
import * as os from "node:os";
|
|
3
4
|
import * as path from "node:path";
|
|
4
5
|
import * as TOML from "@iarna/toml";
|
|
5
6
|
import { main } from "../index";
|
|
6
|
-
|
|
7
|
-
import {
|
|
7
|
+
import { setMock, unsetAllMocks } from "./mock-cfetch";
|
|
8
|
+
import { mockConfirm } from "./mock-dialogs";
|
|
8
9
|
|
|
9
|
-
jest.mock("../cfetch", () =>
|
|
10
|
-
return jest.requireActual("./mock-cfetch");
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
async function w(cmd: void | string, options?: { tap: boolean }) {
|
|
14
|
-
const tapped = options?.tap ? tap() : undefined;
|
|
15
|
-
await main([...(cmd ? cmd.split(" ") : [])]);
|
|
16
|
-
tapped?.off();
|
|
17
|
-
return { stdout: tapped?.out, stderr: tapped?.err };
|
|
18
|
-
}
|
|
10
|
+
jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));
|
|
19
11
|
|
|
20
|
-
function
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// console.trace(...args); // use this if you want to find the true source of your console.log
|
|
37
|
-
};
|
|
38
|
-
console.error = (...args) => {
|
|
39
|
-
toReturn.err += args.join("");
|
|
40
|
-
oldError.apply(console, args);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return toReturn;
|
|
12
|
+
async function w(cmd?: string) {
|
|
13
|
+
const logSpy = jest.spyOn(console, "log").mockImplementation();
|
|
14
|
+
const errorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
15
|
+
const warnSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
16
|
+
try {
|
|
17
|
+
await main(cmd?.split(" ") ?? []);
|
|
18
|
+
return {
|
|
19
|
+
stdout: logSpy.mock.calls.flat(2).join("\n"),
|
|
20
|
+
stderr: errorSpy.mock.calls.flat(2).join("\n"),
|
|
21
|
+
warnings: warnSpy.mock.calls.flat(2).join("\n"),
|
|
22
|
+
};
|
|
23
|
+
} finally {
|
|
24
|
+
logSpy.mockRestore();
|
|
25
|
+
errorSpy.mockRestore();
|
|
26
|
+
warnSpy.mockRestore();
|
|
27
|
+
}
|
|
44
28
|
}
|
|
45
29
|
|
|
46
30
|
describe("wrangler", () => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
31
|
+
describe("no command", () => {
|
|
32
|
+
it("should display a list of available commands", async () => {
|
|
33
|
+
const { stdout, stderr } = await w();
|
|
34
|
+
|
|
35
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
36
|
+
"wrangler
|
|
37
|
+
|
|
38
|
+
Commands:
|
|
39
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
40
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
41
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
42
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
43
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
44
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
45
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
46
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
47
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
48
|
+
|
|
49
|
+
Flags:
|
|
50
|
+
-c, --config Path to .toml configuration file [string]
|
|
51
|
+
-h, --help Show help [boolean]
|
|
52
|
+
-v, --version Show version number [boolean]
|
|
53
|
+
|
|
54
|
+
Options:
|
|
55
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
56
|
+
`);
|
|
57
|
+
|
|
58
|
+
expect(stderr).toMatchInlineSnapshot(`""`);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("invalid command", () => {
|
|
63
|
+
it("should display an error", async () => {
|
|
64
|
+
const { stdout, stderr } = await w("invalid-command");
|
|
65
|
+
|
|
66
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
67
|
+
"wrangler
|
|
68
|
+
|
|
69
|
+
Commands:
|
|
70
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
71
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
72
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
73
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
74
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
75
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
76
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
77
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
78
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
79
|
+
|
|
80
|
+
Flags:
|
|
81
|
+
-c, --config Path to .toml configuration file [string]
|
|
82
|
+
-h, --help Show help [boolean]
|
|
83
|
+
-v, --version Show version number [boolean]
|
|
84
|
+
|
|
85
|
+
Options:
|
|
86
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
87
|
+
`);
|
|
88
|
+
|
|
89
|
+
expect(stderr).toMatchInlineSnapshot(`
|
|
90
|
+
"
|
|
91
|
+
Unknown command: invalid-command."
|
|
92
|
+
`);
|
|
93
|
+
});
|
|
72
94
|
});
|
|
73
95
|
|
|
74
96
|
describe("init", () => {
|
|
75
97
|
const ogcwd = process.cwd();
|
|
98
|
+
let tmpDir: string;
|
|
76
99
|
|
|
77
|
-
beforeEach(() => {
|
|
78
|
-
|
|
100
|
+
beforeEach(async () => {
|
|
101
|
+
tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), "init-"));
|
|
102
|
+
process.chdir(tmpDir);
|
|
79
103
|
});
|
|
80
104
|
|
|
81
105
|
afterEach(async () => {
|
|
82
|
-
await fsp.rm("./wrangler.toml");
|
|
83
106
|
process.chdir(ogcwd);
|
|
107
|
+
await fsp.rm(tmpDir, { recursive: true });
|
|
84
108
|
});
|
|
85
109
|
|
|
86
110
|
it("should create a wrangler.toml", async () => {
|
|
111
|
+
mockConfirm({
|
|
112
|
+
text: "No package.json found. Would you like to create one?",
|
|
113
|
+
result: false,
|
|
114
|
+
});
|
|
87
115
|
await w("init");
|
|
88
116
|
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
89
117
|
expect(typeof parsed.compatibility_date).toBe("string");
|
|
118
|
+
expect(fs.existsSync("./package.json")).toBe(false);
|
|
119
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
90
120
|
});
|
|
91
121
|
|
|
92
|
-
it("should
|
|
122
|
+
it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
|
|
93
123
|
fs.closeSync(fs.openSync("./wrangler.toml", "w"));
|
|
94
|
-
|
|
95
|
-
|
|
124
|
+
mockConfirm({
|
|
125
|
+
text: "Do you want to continue initializing this project?",
|
|
126
|
+
result: false,
|
|
127
|
+
});
|
|
128
|
+
const { stderr } = await w("init");
|
|
129
|
+
expect(stderr).toContain("wrangler.toml file already exists!");
|
|
130
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
131
|
+
expect(typeof parsed.compatibility_date).toBe("undefined");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
|
|
135
|
+
fs.closeSync(fs.openSync("./wrangler.toml", "w"));
|
|
136
|
+
mockConfirm(
|
|
137
|
+
{
|
|
138
|
+
text: "Do you want to continue initializing this project?",
|
|
139
|
+
result: true,
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
text: "No package.json found. Would you like to create one?",
|
|
143
|
+
result: false,
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
const { stderr } = await w("init");
|
|
147
|
+
expect(stderr).toContain("wrangler.toml file already exists!");
|
|
148
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
149
|
+
expect(typeof parsed.compatibility_date).toBe("string");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("should create a package.json if none is found and user confirms", async () => {
|
|
153
|
+
mockConfirm(
|
|
154
|
+
{
|
|
155
|
+
text: "No package.json found. Would you like to create one?",
|
|
156
|
+
result: true,
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
text: "Would you like to use typescript?",
|
|
160
|
+
result: false,
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
await w("init");
|
|
164
|
+
expect(fs.existsSync("./package.json")).toBe(true);
|
|
165
|
+
const packageJson = JSON.parse(
|
|
166
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
167
|
+
);
|
|
168
|
+
expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
|
|
169
|
+
expect(packageJson.version).toEqual("0.0.1");
|
|
170
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("should not touch an existing package.json in the same directory", async () => {
|
|
174
|
+
mockConfirm({
|
|
175
|
+
text: "Would you like to use typescript?",
|
|
176
|
+
result: false,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
fs.writeFileSync(
|
|
180
|
+
"./package.json",
|
|
181
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
182
|
+
"utf-8"
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
await w("init");
|
|
186
|
+
const packageJson = JSON.parse(
|
|
187
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
188
|
+
);
|
|
189
|
+
expect(packageJson.name).toEqual("test");
|
|
190
|
+
expect(packageJson.version).toEqual("1.0.0");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("should not touch an existing package.json in an ancestor directory", async () => {
|
|
194
|
+
mockConfirm({
|
|
195
|
+
text: "Would you like to use typescript?",
|
|
196
|
+
result: false,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
fs.writeFileSync(
|
|
200
|
+
"./package.json",
|
|
201
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
202
|
+
"utf-8"
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
206
|
+
process.chdir("./sub-1/sub-2");
|
|
207
|
+
|
|
208
|
+
await w("init");
|
|
209
|
+
expect(fs.existsSync("./package.json")).toBe(false);
|
|
210
|
+
expect(fs.existsSync("../../package.json")).toBe(true);
|
|
211
|
+
|
|
212
|
+
const packageJson = JSON.parse(
|
|
213
|
+
fs.readFileSync("../../package.json", "utf-8")
|
|
214
|
+
);
|
|
215
|
+
expect(packageJson.name).toEqual("test");
|
|
216
|
+
expect(packageJson.version).toEqual("1.0.0");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
|
|
220
|
+
mockConfirm(
|
|
221
|
+
{
|
|
222
|
+
text: "No package.json found. Would you like to create one?",
|
|
223
|
+
result: true,
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
text: "Would you like to use typescript?",
|
|
227
|
+
result: true,
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
await w("init");
|
|
231
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(true);
|
|
232
|
+
const tsconfigJson = JSON.parse(
|
|
233
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
234
|
+
);
|
|
235
|
+
expect(tsconfigJson.compilerOptions.types).toEqual([
|
|
236
|
+
"@cloudflare/workers-types",
|
|
237
|
+
]);
|
|
238
|
+
const packageJson = JSON.parse(
|
|
239
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
240
|
+
);
|
|
241
|
+
expect(packageJson.devDependencies).toEqual({
|
|
242
|
+
"@cloudflare/workers-types": expect.any(String),
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("should not touch an existing tsconfig.json in the same directory", async () => {
|
|
247
|
+
fs.writeFileSync(
|
|
248
|
+
"./package.json",
|
|
249
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
250
|
+
"utf-8"
|
|
251
|
+
);
|
|
252
|
+
fs.writeFileSync(
|
|
253
|
+
"./tsconfig.json",
|
|
254
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
255
|
+
"utf-8"
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
await w("init");
|
|
259
|
+
const tsconfigJson = JSON.parse(
|
|
260
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
261
|
+
);
|
|
262
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("should not touch an existing package.json in an ancestor directory", async () => {
|
|
266
|
+
fs.writeFileSync(
|
|
267
|
+
"./package.json",
|
|
268
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
269
|
+
"utf-8"
|
|
270
|
+
);
|
|
271
|
+
fs.writeFileSync(
|
|
272
|
+
"./tsconfig.json",
|
|
273
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
274
|
+
"utf-8"
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
278
|
+
process.chdir("./sub-1/sub-2");
|
|
279
|
+
|
|
280
|
+
await w("init");
|
|
281
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
282
|
+
expect(fs.existsSync("../../tsconfig.json")).toBe(true);
|
|
283
|
+
|
|
284
|
+
const tsconfigJson = JSON.parse(
|
|
285
|
+
fs.readFileSync("../../tsconfig.json", "utf-8")
|
|
286
|
+
);
|
|
287
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("should error if `--type` is used", async () => {
|
|
291
|
+
const noValue = await w("init --type");
|
|
292
|
+
expect(noValue.stderr).toMatchInlineSnapshot(
|
|
293
|
+
`"The --type option is no longer supported."`
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("should error if `--type javascript` is used", async () => {
|
|
298
|
+
const javascriptValue = await w("init --type javascript");
|
|
299
|
+
expect(javascriptValue.stderr).toMatchInlineSnapshot(
|
|
300
|
+
`"The --type option is no longer supported."`
|
|
301
|
+
);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("should error if `--type rust` is used", async () => {
|
|
305
|
+
const rustValue = await w("init --type rust");
|
|
306
|
+
expect(rustValue.stderr).toMatchInlineSnapshot(
|
|
307
|
+
`"The --type option is no longer supported."`
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("should error if `--type webpack` is used", async () => {
|
|
312
|
+
const webpackValue = await w("init --type webpack");
|
|
313
|
+
expect(webpackValue.stderr).toMatchInlineSnapshot(`
|
|
314
|
+
"The --type option is no longer supported.
|
|
315
|
+
If you wish to use webpack then you will need to create a custom build."
|
|
316
|
+
`);
|
|
96
317
|
});
|
|
97
318
|
});
|
|
98
319
|
|
|
99
320
|
describe("kv:namespace", () => {
|
|
100
|
-
|
|
321
|
+
afterEach(() => {
|
|
101
322
|
unsetAllMocks();
|
|
102
323
|
});
|
|
103
|
-
|
|
324
|
+
|
|
104
325
|
it("can create a namespace", async () => {
|
|
326
|
+
const KVNamespaces: { title: string; id: string }[] = [];
|
|
105
327
|
setMock("/accounts/:accountId/storage/kv/namespaces", (uri, init) => {
|
|
106
328
|
expect(init.method === "POST");
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
329
|
+
expect(uri[0]).toEqual(
|
|
330
|
+
"/accounts/some-account-id/storage/kv/namespaces"
|
|
331
|
+
);
|
|
332
|
+
const { title } = JSON.parse(init.body);
|
|
333
|
+
expect(title).toEqual("worker-UnitTestNamespace");
|
|
334
|
+
KVNamespaces.push({ title, id: "some-namespace-id" });
|
|
110
335
|
return { id: "some-namespace-id" };
|
|
111
336
|
});
|
|
112
337
|
|
|
113
|
-
await w(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
338
|
+
await w("kv:namespace create UnitTestNamespace");
|
|
339
|
+
|
|
340
|
+
expect(KVNamespaces).toEqual([
|
|
341
|
+
{
|
|
342
|
+
title: "worker-UnitTestNamespace",
|
|
343
|
+
id: "some-namespace-id",
|
|
344
|
+
},
|
|
345
|
+
]);
|
|
117
346
|
});
|
|
118
347
|
|
|
119
|
-
let createdNamespace: { id: string; title: string };
|
|
120
348
|
it("can list namespaces", async () => {
|
|
349
|
+
const KVNamespaces: { title: string; id: string }[] = [
|
|
350
|
+
{ title: "title-1", id: "id-1" },
|
|
351
|
+
{ title: "title-2", id: "id-2" },
|
|
352
|
+
];
|
|
121
353
|
setMock(
|
|
122
354
|
"/accounts/:accountId/storage/kv/namespaces\\?:qs",
|
|
123
355
|
(uri, init) => {
|
|
356
|
+
expect(uri[0]).toContain(
|
|
357
|
+
"/accounts/some-account-id/storage/kv/namespaces"
|
|
358
|
+
);
|
|
359
|
+
expect(uri[2]).toContain("per_page=100");
|
|
360
|
+
expect(uri[2]).toContain("order=title");
|
|
361
|
+
expect(uri[2]).toContain("direction=asc");
|
|
362
|
+
expect(uri[2]).toContain("page=1");
|
|
124
363
|
expect(init).toBe(undefined);
|
|
125
364
|
return KVNamespaces;
|
|
126
365
|
}
|
|
127
366
|
);
|
|
128
|
-
const { stdout } = await w("kv:namespace list"
|
|
129
|
-
const namespaces = JSON.parse(stdout);
|
|
130
|
-
|
|
131
|
-
(ns) => ns.title === "worker-UnitTestNamespace"
|
|
132
|
-
);
|
|
133
|
-
expect(createdNamespace.title).toBe("worker-UnitTestNamespace");
|
|
367
|
+
const { stdout } = await w("kv:namespace list");
|
|
368
|
+
const namespaces = JSON.parse(stdout) as { id: string; title: string }[];
|
|
369
|
+
expect(namespaces).toEqual(KVNamespaces);
|
|
134
370
|
});
|
|
135
371
|
|
|
136
372
|
it("can delete a namespace", async () => {
|
|
137
|
-
|
|
373
|
+
let accountId = "";
|
|
374
|
+
let namespaceId = "";
|
|
138
375
|
setMock(
|
|
139
376
|
"/accounts/:accountId/storage/kv/namespaces/:namespaceId",
|
|
140
377
|
(uri, init) => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
378
|
+
accountId = uri[1];
|
|
379
|
+
namespaceId = uri[2];
|
|
380
|
+
expect(uri[0]).toEqual(
|
|
381
|
+
"/accounts/some-account-id/storage/kv/namespaces/some-namespace-id"
|
|
144
382
|
);
|
|
383
|
+
expect(init.method).toBe("DELETE");
|
|
145
384
|
}
|
|
146
385
|
);
|
|
147
|
-
await w(`kv:namespace delete --namespace-id
|
|
148
|
-
expect(
|
|
149
|
-
|
|
150
|
-
);
|
|
386
|
+
await w(`kv:namespace delete --namespace-id some-namespace-id`);
|
|
387
|
+
expect(accountId).toEqual("some-account-id");
|
|
388
|
+
expect(namespaceId).toEqual("some-namespace-id");
|
|
151
389
|
});
|
|
152
390
|
});
|
|
153
391
|
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { confirm, prompt } from "../dialogs";
|
|
2
|
+
|
|
3
|
+
jest.mock("../dialogs");
|
|
4
|
+
|
|
5
|
+
// By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
|
|
6
|
+
(confirm as jest.Mock).mockImplementation(() => {
|
|
7
|
+
throw new Error(
|
|
8
|
+
"Unexpected call to `confirm()`. You should use `mockConfirm()` to mock calls to `confirm()` with expectations. Search the codebase for `mockConfirm` to learn more."
|
|
9
|
+
);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// By default (if not configured by mockPrompt()) calls to `prompt()` should throw.
|
|
13
|
+
(prompt as jest.Mock).mockImplementation(() => {
|
|
14
|
+
throw new Error(
|
|
15
|
+
"Unexpected call to `prompt()`. You should use `mockPrompt()` to mock calls to `prompt()` with expectations. Search the codebase for `mockPrompt` to learn more."
|
|
16
|
+
);
|
|
17
|
+
});
|