wrangler 0.0.0-efa548c → 0.0.0-fb43821
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/bin/wrangler.js +36 -2
- package/import_meta_url.js +3 -0
- package/package.json +56 -22
- package/src/__tests__/clipboardy-mock.js +4 -0
- package/src/__tests__/index.test.ts +422 -0
- package/src/__tests__/mock-cfetch.js +42 -0
- package/src/api/form_data.ts +141 -0
- package/src/api/inspect.ts +430 -0
- package/src/api/preview.ts +128 -0
- package/src/api/worker.ts +161 -0
- package/src/cfetch.ts +72 -0
- package/src/cli.ts +10 -0
- package/src/config.ts +122 -0
- package/src/dev.tsx +776 -0
- package/src/dialogs.tsx +77 -0
- package/src/index.tsx +1874 -0
- package/src/kv.tsx +211 -0
- package/src/module-collection.ts +64 -0
- package/src/pages.tsx +818 -0
- package/src/publish.ts +346 -0
- package/src/sites.tsx +115 -0
- package/src/tail.tsx +71 -0
- package/src/user.tsx +1029 -0
- package/{facade.js → static-asset-facade.js} +2 -7
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +262 -192
- package/vendor/@cloudflare/kv-asset-handler/package.json +1 -1
- package/vendor/{mime → wrangler-mime}/CHANGELOG.md +0 -0
- package/vendor/{mime → wrangler-mime}/LICENSE +0 -0
- package/vendor/{mime → wrangler-mime}/Mime.js +0 -0
- package/vendor/{mime → wrangler-mime}/README.md +0 -0
- package/vendor/{mime → wrangler-mime}/cli.js +0 -0
- package/vendor/{mime → wrangler-mime}/index.js +0 -0
- package/vendor/{mime → wrangler-mime}/lite.js +0 -0
- package/vendor/{mime → wrangler-mime}/package.json +1 -1
- package/vendor/{mime → wrangler-mime}/types/other.js +0 -0
- package/vendor/{mime → wrangler-mime}/types/standard.js +0 -0
- package/wrangler-dist/{index_node.js → cli.js} +90785 -61395
- package/wrangler-dist/cli.js.map +7 -0
- package/wrangler-dist/index_node.js.map +0 -7
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
|
@@ -1,2 +1,36 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
require("
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { spawn } = require("child_process");
|
|
3
|
+
const { join } = require("path");
|
|
4
|
+
const semiver = require("semiver");
|
|
5
|
+
|
|
6
|
+
const MIN_NODE_VERSION = "16.7.0";
|
|
7
|
+
|
|
8
|
+
async function main() {
|
|
9
|
+
if (semiver(process.versions.node, MIN_NODE_VERSION) < 0) {
|
|
10
|
+
// Note Volta and nvm are also recommended in the official docs:
|
|
11
|
+
// https://developers.cloudflare.com/workers/get-started/guide#2-install-the-workers-cli
|
|
12
|
+
console.error(
|
|
13
|
+
`Wrangler requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}.
|
|
14
|
+
You should use the latest Node.js version if possible, as Cloudflare Workers use a very up-to-date version of V8.
|
|
15
|
+
Consider using a Node.js version manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.`
|
|
16
|
+
);
|
|
17
|
+
process.exitCode = 1;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
spawn(
|
|
22
|
+
process.execPath,
|
|
23
|
+
[
|
|
24
|
+
"--no-warnings",
|
|
25
|
+
"--experimental-vm-modules",
|
|
26
|
+
...process.execArgv,
|
|
27
|
+
join(__dirname, "../wrangler-dist/cli.js"),
|
|
28
|
+
...process.argv.slice(2),
|
|
29
|
+
],
|
|
30
|
+
{ stdio: "inherit" }
|
|
31
|
+
).on("exit", (code) =>
|
|
32
|
+
process.exit(code === undefined || code === null ? 0 : code)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
void main();
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrangler",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-fb43821",
|
|
4
4
|
"author": "wrangler@cloudflare.com",
|
|
5
5
|
"description": "Command-line interface for all things Cloudflare Workers",
|
|
6
|
-
"bin":
|
|
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,48 +36,79 @@
|
|
|
33
36
|
"cli"
|
|
34
37
|
],
|
|
35
38
|
"dependencies": {
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
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"
|
|
39
43
|
},
|
|
40
44
|
"optionalDependencies": {
|
|
41
45
|
"fsevents": "~2.3.2"
|
|
42
46
|
},
|
|
43
47
|
"devDependencies": {
|
|
44
48
|
"@iarna/toml": "^2.2.5",
|
|
45
|
-
"@types/
|
|
46
|
-
"@types/react": "^17.0.19",
|
|
49
|
+
"@types/react": "^17.0.37",
|
|
47
50
|
"@types/signal-exit": "^3.0.1",
|
|
48
|
-
"@types/ws": "^
|
|
49
|
-
"@types/yargs": "^17.0.
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
51
|
+
"@types/ws": "^8.2.1",
|
|
52
|
+
"@types/yargs": "^17.0.7",
|
|
53
|
+
"chokidar": "^3.5.2",
|
|
54
|
+
"clipboardy": "^3.0.0",
|
|
55
|
+
"command-exists": "^1.2.9",
|
|
56
|
+
"execa": "^6.0.0",
|
|
57
|
+
"finalhandler": "^1.1.2",
|
|
58
|
+
"find-up": "^6.2.0",
|
|
59
|
+
"formdata-node": "^4.3.1",
|
|
54
60
|
"http-proxy": "^1.18.1",
|
|
55
61
|
"ink": "^3.2.0",
|
|
62
|
+
"ink-select-input": "^4.2.1",
|
|
56
63
|
"ink-table": "^3.0.0",
|
|
57
|
-
"
|
|
58
|
-
"
|
|
64
|
+
"ink-text-input": "^4.0.2",
|
|
65
|
+
"node-fetch": "^3.1.0",
|
|
66
|
+
"open": "^8.4.0",
|
|
67
|
+
"path-to-regexp": "^6.2.0",
|
|
59
68
|
"react": "^17.0.2",
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
69
|
+
"react-error-boundary": "^3.1.4",
|
|
70
|
+
"serve-static": "^1.14.1",
|
|
71
|
+
"signal-exit": "^3.0.6",
|
|
72
|
+
"tmp-promise": "^3.0.3",
|
|
73
|
+
"undici": "^4.11.1",
|
|
74
|
+
"ws": "^8.3.0",
|
|
75
|
+
"yargs": "^17.3.0"
|
|
63
76
|
},
|
|
64
77
|
"files": [
|
|
78
|
+
"src",
|
|
65
79
|
"bin",
|
|
66
80
|
"miniflare-config-stubs",
|
|
67
81
|
"wrangler-dist",
|
|
68
|
-
"facade.js",
|
|
69
|
-
"vendor"
|
|
82
|
+
"static-asset-facade.js",
|
|
83
|
+
"vendor",
|
|
84
|
+
"import_meta_url.js"
|
|
70
85
|
],
|
|
71
86
|
"scripts": {
|
|
72
87
|
"clean": "rm -rf wrangler-dist",
|
|
73
88
|
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
74
89
|
"build": "npm run clean && npm run bundle",
|
|
75
|
-
"start": "npm run bundle && ./bin/wrangler.js"
|
|
90
|
+
"start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
|
|
91
|
+
"test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true"
|
|
76
92
|
},
|
|
77
93
|
"engines": {
|
|
78
|
-
"node": ">=16.
|
|
94
|
+
"node": ">=16.7.0"
|
|
95
|
+
},
|
|
96
|
+
"jest": {
|
|
97
|
+
"restoreMocks": true,
|
|
98
|
+
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
|
|
99
|
+
"transformIgnorePatterns": [
|
|
100
|
+
"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)"
|
|
101
|
+
],
|
|
102
|
+
"moduleNameMapper": {
|
|
103
|
+
"clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
|
|
104
|
+
},
|
|
105
|
+
"transform": {
|
|
106
|
+
"^.+\\.c?(t|j)sx?$": [
|
|
107
|
+
"esbuild-jest",
|
|
108
|
+
{
|
|
109
|
+
"sourcemap": true
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
}
|
|
79
113
|
}
|
|
80
114
|
}
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as fsp from "node:fs/promises";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as TOML from "@iarna/toml";
|
|
6
|
+
import { main } from "../index";
|
|
7
|
+
import { setMock, unsetAllMocks } from "./mock-cfetch";
|
|
8
|
+
import { confirm } from "../dialogs";
|
|
9
|
+
|
|
10
|
+
jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));
|
|
11
|
+
|
|
12
|
+
jest.mock("../dialogs", () => {
|
|
13
|
+
return {
|
|
14
|
+
...jest.requireActual<object>("../dialogs"),
|
|
15
|
+
confirm: jest
|
|
16
|
+
.fn()
|
|
17
|
+
.mockName("confirmMock")
|
|
18
|
+
.mockImplementation(() => {
|
|
19
|
+
// By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
|
|
20
|
+
throw new Error("Unexpected call to `confirm()`.");
|
|
21
|
+
}),
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Mock the implementation of `confirm()` that will respond with configured results
|
|
27
|
+
* for configured confirmation text messages.
|
|
28
|
+
*
|
|
29
|
+
* If there is a call to `confirm()` that does not match any of the expectations
|
|
30
|
+
* then an error is thrown.
|
|
31
|
+
*/
|
|
32
|
+
function mockConfirm(...expectations: { text: string; result: boolean }[]) {
|
|
33
|
+
(confirm as jest.Mock).mockImplementation((text: string) => {
|
|
34
|
+
for (const { text: expectedText, result } of expectations) {
|
|
35
|
+
if (text === expectedText) {
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new Error(`Unexpected confirmation message: ${text}`);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function w(cmd?: string) {
|
|
44
|
+
const logSpy = jest.spyOn(console, "log").mockImplementation();
|
|
45
|
+
const errorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
46
|
+
const warnSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
47
|
+
try {
|
|
48
|
+
await main(cmd?.split(" ") ?? []);
|
|
49
|
+
return {
|
|
50
|
+
stdout: logSpy.mock.calls.flat(2).join("\n"),
|
|
51
|
+
stderr: errorSpy.mock.calls.flat(2).join("\n"),
|
|
52
|
+
warnings: warnSpy.mock.calls.flat(2).join("\n"),
|
|
53
|
+
};
|
|
54
|
+
} finally {
|
|
55
|
+
logSpy.mockRestore();
|
|
56
|
+
errorSpy.mockRestore();
|
|
57
|
+
warnSpy.mockRestore();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe("wrangler", () => {
|
|
62
|
+
describe("no command", () => {
|
|
63
|
+
it("should display a list of available commands", async () => {
|
|
64
|
+
const { stdout, stderr } = await w();
|
|
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
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("invalid command", () => {
|
|
94
|
+
it("should display an error", async () => {
|
|
95
|
+
const { stdout, stderr } = await w("invalid-command");
|
|
96
|
+
|
|
97
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
98
|
+
"wrangler
|
|
99
|
+
|
|
100
|
+
Commands:
|
|
101
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
102
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
103
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
104
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
105
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
106
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
107
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
108
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
109
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
110
|
+
|
|
111
|
+
Flags:
|
|
112
|
+
-c, --config Path to .toml configuration file [string]
|
|
113
|
+
-h, --help Show help [boolean]
|
|
114
|
+
-v, --version Show version number [boolean]
|
|
115
|
+
|
|
116
|
+
Options:
|
|
117
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
118
|
+
`);
|
|
119
|
+
|
|
120
|
+
expect(stderr).toMatchInlineSnapshot(`
|
|
121
|
+
"
|
|
122
|
+
Unknown command: invalid-command."
|
|
123
|
+
`);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("init", () => {
|
|
128
|
+
const ogcwd = process.cwd();
|
|
129
|
+
let tmpDir: string;
|
|
130
|
+
|
|
131
|
+
beforeEach(async () => {
|
|
132
|
+
tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), "init-"));
|
|
133
|
+
process.chdir(tmpDir);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
afterEach(async () => {
|
|
137
|
+
process.chdir(ogcwd);
|
|
138
|
+
await fsp.rm(tmpDir, { recursive: true });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("should create a wrangler.toml", async () => {
|
|
142
|
+
mockConfirm({
|
|
143
|
+
text: "No package.json found. Would you like to create one?",
|
|
144
|
+
result: false,
|
|
145
|
+
});
|
|
146
|
+
await w("init");
|
|
147
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
148
|
+
expect(typeof parsed.compatibility_date).toBe("string");
|
|
149
|
+
expect(fs.existsSync("./package.json")).toBe(false);
|
|
150
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
|
|
154
|
+
fs.closeSync(fs.openSync("./wrangler.toml", "w"));
|
|
155
|
+
mockConfirm({
|
|
156
|
+
text: "Do you want to continue initializing this project?",
|
|
157
|
+
result: false,
|
|
158
|
+
});
|
|
159
|
+
const { stderr } = await w("init");
|
|
160
|
+
expect(stderr).toContain("wrangler.toml file already exists!");
|
|
161
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
162
|
+
expect(typeof parsed.compatibility_date).toBe("undefined");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
|
|
166
|
+
fs.closeSync(fs.openSync("./wrangler.toml", "w"));
|
|
167
|
+
mockConfirm(
|
|
168
|
+
{
|
|
169
|
+
text: "Do you want to continue initializing this project?",
|
|
170
|
+
result: true,
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
text: "No package.json found. Would you like to create one?",
|
|
174
|
+
result: false,
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
const { stderr } = await w("init");
|
|
178
|
+
expect(stderr).toContain("wrangler.toml file already exists!");
|
|
179
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
180
|
+
expect(typeof parsed.compatibility_date).toBe("string");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("should create a package.json if none is found and user confirms", async () => {
|
|
184
|
+
mockConfirm(
|
|
185
|
+
{
|
|
186
|
+
text: "No package.json found. Would you like to create one?",
|
|
187
|
+
result: true,
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
text: "Would you like to use typescript?",
|
|
191
|
+
result: false,
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
await w("init");
|
|
195
|
+
expect(fs.existsSync("./package.json")).toBe(true);
|
|
196
|
+
const packageJson = JSON.parse(
|
|
197
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
198
|
+
);
|
|
199
|
+
expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
|
|
200
|
+
expect(packageJson.version).toEqual("0.0.1");
|
|
201
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("should not touch an existing package.json in the same directory", async () => {
|
|
205
|
+
mockConfirm({
|
|
206
|
+
text: "Would you like to use typescript?",
|
|
207
|
+
result: false,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
fs.writeFileSync(
|
|
211
|
+
"./package.json",
|
|
212
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
213
|
+
"utf-8"
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
await w("init");
|
|
217
|
+
const packageJson = JSON.parse(
|
|
218
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
219
|
+
);
|
|
220
|
+
expect(packageJson.name).toEqual("test");
|
|
221
|
+
expect(packageJson.version).toEqual("1.0.0");
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("should not touch an existing package.json in an ancestor directory", async () => {
|
|
225
|
+
mockConfirm({
|
|
226
|
+
text: "Would you like to use typescript?",
|
|
227
|
+
result: false,
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
fs.writeFileSync(
|
|
231
|
+
"./package.json",
|
|
232
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
233
|
+
"utf-8"
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
237
|
+
process.chdir("./sub-1/sub-2");
|
|
238
|
+
|
|
239
|
+
await w("init");
|
|
240
|
+
expect(fs.existsSync("./package.json")).toBe(false);
|
|
241
|
+
expect(fs.existsSync("../../package.json")).toBe(true);
|
|
242
|
+
|
|
243
|
+
const packageJson = JSON.parse(
|
|
244
|
+
fs.readFileSync("../../package.json", "utf-8")
|
|
245
|
+
);
|
|
246
|
+
expect(packageJson.name).toEqual("test");
|
|
247
|
+
expect(packageJson.version).toEqual("1.0.0");
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
|
|
251
|
+
mockConfirm(
|
|
252
|
+
{
|
|
253
|
+
text: "No package.json found. Would you like to create one?",
|
|
254
|
+
result: true,
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
text: "Would you like to use typescript?",
|
|
258
|
+
result: true,
|
|
259
|
+
}
|
|
260
|
+
);
|
|
261
|
+
await w("init");
|
|
262
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(true);
|
|
263
|
+
const tsconfigJson = JSON.parse(
|
|
264
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
265
|
+
);
|
|
266
|
+
expect(tsconfigJson.compilerOptions.types).toEqual([
|
|
267
|
+
"@cloudflare/workers-types",
|
|
268
|
+
]);
|
|
269
|
+
const packageJson = JSON.parse(
|
|
270
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
271
|
+
);
|
|
272
|
+
expect(packageJson.devDependencies).toEqual({
|
|
273
|
+
"@cloudflare/workers-types": expect.any(String),
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("should not touch an existing tsconfig.json in the same directory", async () => {
|
|
278
|
+
fs.writeFileSync(
|
|
279
|
+
"./package.json",
|
|
280
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
281
|
+
"utf-8"
|
|
282
|
+
);
|
|
283
|
+
fs.writeFileSync(
|
|
284
|
+
"./tsconfig.json",
|
|
285
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
286
|
+
"utf-8"
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
await w("init");
|
|
290
|
+
const tsconfigJson = JSON.parse(
|
|
291
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
292
|
+
);
|
|
293
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("should not touch an existing package.json in an ancestor directory", async () => {
|
|
297
|
+
fs.writeFileSync(
|
|
298
|
+
"./package.json",
|
|
299
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
300
|
+
"utf-8"
|
|
301
|
+
);
|
|
302
|
+
fs.writeFileSync(
|
|
303
|
+
"./tsconfig.json",
|
|
304
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
305
|
+
"utf-8"
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
309
|
+
process.chdir("./sub-1/sub-2");
|
|
310
|
+
|
|
311
|
+
await w("init");
|
|
312
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
313
|
+
expect(fs.existsSync("../../tsconfig.json")).toBe(true);
|
|
314
|
+
|
|
315
|
+
const tsconfigJson = JSON.parse(
|
|
316
|
+
fs.readFileSync("../../tsconfig.json", "utf-8")
|
|
317
|
+
);
|
|
318
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("should error if `--type` is used", async () => {
|
|
322
|
+
const noValue = await w("init --type");
|
|
323
|
+
expect(noValue.stderr).toMatchInlineSnapshot(
|
|
324
|
+
`"The --type option is no longer supported."`
|
|
325
|
+
);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("should error if `--type javascript` is used", async () => {
|
|
329
|
+
const javascriptValue = await w("init --type javascript");
|
|
330
|
+
expect(javascriptValue.stderr).toMatchInlineSnapshot(
|
|
331
|
+
`"The --type option is no longer supported."`
|
|
332
|
+
);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("should error if `--type rust` is used", async () => {
|
|
336
|
+
const rustValue = await w("init --type rust");
|
|
337
|
+
expect(rustValue.stderr).toMatchInlineSnapshot(
|
|
338
|
+
`"The --type option is no longer supported."`
|
|
339
|
+
);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("should error if `--type webpack` is used", async () => {
|
|
343
|
+
const webpackValue = await w("init --type webpack");
|
|
344
|
+
expect(webpackValue.stderr).toMatchInlineSnapshot(`
|
|
345
|
+
"The --type option is no longer supported.
|
|
346
|
+
If you wish to use webpack then you will need to create a custom build."
|
|
347
|
+
`);
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
describe("kv:namespace", () => {
|
|
352
|
+
afterEach(() => {
|
|
353
|
+
unsetAllMocks();
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("can create a namespace", async () => {
|
|
357
|
+
const KVNamespaces: { title: string; id: string }[] = [];
|
|
358
|
+
setMock("/accounts/:accountId/storage/kv/namespaces", (uri, init) => {
|
|
359
|
+
expect(init.method === "POST");
|
|
360
|
+
expect(uri[0]).toEqual(
|
|
361
|
+
"/accounts/some-account-id/storage/kv/namespaces"
|
|
362
|
+
);
|
|
363
|
+
const { title } = JSON.parse(init.body);
|
|
364
|
+
expect(title).toEqual("worker-UnitTestNamespace");
|
|
365
|
+
KVNamespaces.push({ title, id: "some-namespace-id" });
|
|
366
|
+
return { id: "some-namespace-id" };
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
await w("kv:namespace create UnitTestNamespace");
|
|
370
|
+
|
|
371
|
+
expect(KVNamespaces).toEqual([
|
|
372
|
+
{
|
|
373
|
+
title: "worker-UnitTestNamespace",
|
|
374
|
+
id: "some-namespace-id",
|
|
375
|
+
},
|
|
376
|
+
]);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("can list namespaces", async () => {
|
|
380
|
+
const KVNamespaces: { title: string; id: string }[] = [
|
|
381
|
+
{ title: "title-1", id: "id-1" },
|
|
382
|
+
{ title: "title-2", id: "id-2" },
|
|
383
|
+
];
|
|
384
|
+
setMock(
|
|
385
|
+
"/accounts/:accountId/storage/kv/namespaces\\?:qs",
|
|
386
|
+
(uri, init) => {
|
|
387
|
+
expect(uri[0]).toContain(
|
|
388
|
+
"/accounts/some-account-id/storage/kv/namespaces"
|
|
389
|
+
);
|
|
390
|
+
expect(uri[2]).toContain("per_page=100");
|
|
391
|
+
expect(uri[2]).toContain("order=title");
|
|
392
|
+
expect(uri[2]).toContain("direction=asc");
|
|
393
|
+
expect(uri[2]).toContain("page=1");
|
|
394
|
+
expect(init).toBe(undefined);
|
|
395
|
+
return KVNamespaces;
|
|
396
|
+
}
|
|
397
|
+
);
|
|
398
|
+
const { stdout } = await w("kv:namespace list");
|
|
399
|
+
const namespaces = JSON.parse(stdout) as { id: string; title: string }[];
|
|
400
|
+
expect(namespaces).toEqual(KVNamespaces);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it("can delete a namespace", async () => {
|
|
404
|
+
let accountId = "";
|
|
405
|
+
let namespaceId = "";
|
|
406
|
+
setMock(
|
|
407
|
+
"/accounts/:accountId/storage/kv/namespaces/:namespaceId",
|
|
408
|
+
(uri, init) => {
|
|
409
|
+
accountId = uri[1];
|
|
410
|
+
namespaceId = uri[2];
|
|
411
|
+
expect(uri[0]).toEqual(
|
|
412
|
+
"/accounts/some-account-id/storage/kv/namespaces/some-namespace-id"
|
|
413
|
+
);
|
|
414
|
+
expect(init.method).toBe("DELETE");
|
|
415
|
+
}
|
|
416
|
+
);
|
|
417
|
+
await w(`kv:namespace delete --namespace-id some-namespace-id`);
|
|
418
|
+
expect(accountId).toEqual("some-account-id");
|
|
419
|
+
expect(namespaceId).toEqual("some-namespace-id");
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// This file mocks ../cfetch.ts
|
|
2
|
+
// so we can insert whatever responses we want from it
|
|
3
|
+
|
|
4
|
+
const { pathToRegexp } = require("path-to-regexp");
|
|
5
|
+
// TODO: add jsdoc style types here
|
|
6
|
+
|
|
7
|
+
// type MockHandler = (resource: string, init?: RequestInit) => any; // TODO: use a generic here
|
|
8
|
+
|
|
9
|
+
let mocks = [];
|
|
10
|
+
|
|
11
|
+
export function mockCfetch(resource, init) {
|
|
12
|
+
for (const { regexp, handler } of mocks) {
|
|
13
|
+
// The `resource` regular expression will extract the labelled groups from the URL.
|
|
14
|
+
// Let's pass these through to the handler, to allow it to do additional checks or behaviour.
|
|
15
|
+
const uri = regexp.exec(resource);
|
|
16
|
+
if (uri !== null) {
|
|
17
|
+
return handler(uri, init); // TODO: should we have some kind of fallthrough system? we'll see.
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
throw new Error(`no mocks found for ${resource}`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function setMock(resource, handler) {
|
|
24
|
+
const mock = {
|
|
25
|
+
resource,
|
|
26
|
+
handler,
|
|
27
|
+
regexp: pathToRegexp(resource),
|
|
28
|
+
};
|
|
29
|
+
mocks.push(mock);
|
|
30
|
+
return () => {
|
|
31
|
+
mocks = mocks.filter((x) => x !== mock);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function unsetAllMocks() {
|
|
36
|
+
mocks = [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const CF_API_BASE_URL =
|
|
40
|
+
process.env.CF_API_BASE_URL || "https://api.cloudflare.com/client/v4";
|
|
41
|
+
|
|
42
|
+
export default mockCfetch;
|