wrangler 0.0.0-e14e589 → 0.0.0-e2a26a0
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 +2 -2
- package/package.json +49 -30
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +47 -0
- package/pages/functions/filepath-routing.ts +231 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +160 -0
- package/pages/functions/template-worker.ts +136 -0
- package/src/__tests__/dev.test.tsx +71 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/mock-account-id.ts +30 -0
- package/src/__tests__/helpers/mock-bin.ts +33 -0
- package/src/__tests__/helpers/mock-cfetch.ts +207 -0
- package/src/__tests__/helpers/mock-console.ts +56 -0
- package/src/__tests__/helpers/mock-dialogs.ts +65 -0
- package/src/__tests__/helpers/mock-kv.ts +40 -0
- package/src/__tests__/helpers/mock-user.ts +27 -0
- package/src/__tests__/helpers/run-in-tmp.ts +29 -0
- package/src/__tests__/helpers/run-wrangler.ts +14 -0
- package/src/__tests__/index.test.ts +509 -119
- package/src/__tests__/jest.setup.ts +37 -0
- package/src/__tests__/kv.test.ts +1139 -0
- package/src/__tests__/logout.test.ts +50 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/publish.test.ts +1222 -0
- package/src/__tests__/secret.test.ts +210 -0
- package/src/__tests__/whoami.test.tsx +86 -0
- package/src/api/form_data.ts +85 -76
- package/src/api/preview.ts +29 -24
- package/src/api/worker.ts +50 -35
- package/src/cfetch/index.ts +102 -0
- package/src/cfetch/internal.ts +101 -0
- package/src/cli.ts +6 -7
- package/src/config.ts +630 -85
- package/src/dev.tsx +446 -282
- package/src/dialogs.tsx +6 -14
- package/src/index.tsx +773 -426
- package/src/inspect.ts +596 -0
- package/src/kv.tsx +118 -45
- package/src/module-collection.ts +63 -0
- package/src/package-manager.ts +120 -0
- package/src/pages.tsx +917 -211
- package/src/paths.ts +26 -0
- package/src/proxy.ts +370 -0
- package/src/publish.ts +220 -88
- package/src/sites.tsx +180 -55
- package/src/tail.tsx +6 -4
- package/src/user.tsx +77 -87
- package/src/whoami.tsx +69 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
- package/wrangler-dist/cli.js +125330 -557
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/inspect.ts +0 -441
- package/src/cfetch.ts +0 -72
- package/src/util/fetch.ts +0 -74
|
@@ -1,153 +1,543 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import * as fsp from "node:fs/promises";
|
|
3
|
-
import * as path from "node:path";
|
|
4
3
|
import * as TOML from "@iarna/toml";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
}
|
|
19
|
-
|
|
20
|
-
function tap() {
|
|
21
|
-
const oldLog = console.log;
|
|
22
|
-
const oldError = console.error;
|
|
23
|
-
|
|
24
|
-
const toReturn = {
|
|
25
|
-
off: () => {
|
|
26
|
-
console.log = oldLog;
|
|
27
|
-
console.error = oldError;
|
|
28
|
-
},
|
|
29
|
-
out: "",
|
|
30
|
-
err: "",
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
console.log = (...args) => {
|
|
34
|
-
toReturn.out += args.join("");
|
|
35
|
-
oldLog.apply(console, args);
|
|
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;
|
|
44
|
-
}
|
|
4
|
+
import { version as wranglerVersion } from "../../package.json";
|
|
5
|
+
import { getPackageManager } from "../package-manager";
|
|
6
|
+
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
7
|
+
import { mockConfirm } from "./helpers/mock-dialogs";
|
|
8
|
+
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
9
|
+
import { runWrangler } from "./helpers/run-wrangler";
|
|
10
|
+
import type { PackageManager } from "../package-manager";
|
|
45
11
|
|
|
46
12
|
describe("wrangler", () => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
57
|
-
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
58
|
-
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
59
|
-
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
60
|
-
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
61
|
-
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
62
|
-
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
63
|
-
|
|
64
|
-
Flags:
|
|
65
|
-
--config Path to .toml configuration file [string]
|
|
66
|
-
--help Show help [boolean]
|
|
67
|
-
--version Show version number [boolean]
|
|
68
|
-
|
|
69
|
-
Options:
|
|
70
|
-
--local Run on my machine [boolean] [default: false]"
|
|
71
|
-
`);
|
|
13
|
+
let mockPackageManager: PackageManager;
|
|
14
|
+
runInTempDir();
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
mockPackageManager = {
|
|
18
|
+
addDevDeps: jest.fn(),
|
|
19
|
+
install: jest.fn(),
|
|
20
|
+
};
|
|
21
|
+
(getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
|
|
72
22
|
});
|
|
73
23
|
|
|
74
|
-
|
|
75
|
-
|
|
24
|
+
const std = mockConsoleMethods();
|
|
25
|
+
|
|
26
|
+
describe("no command", () => {
|
|
27
|
+
it("should display a list of available commands", async () => {
|
|
28
|
+
await runWrangler();
|
|
29
|
+
|
|
30
|
+
expect(std.out).toMatchInlineSnapshot(`
|
|
31
|
+
"wrangler
|
|
32
|
+
|
|
33
|
+
Commands:
|
|
34
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
35
|
+
wrangler whoami 🕵️ Retrieve your user info and test your auth config
|
|
36
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
37
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
38
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
39
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
40
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
41
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
42
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
43
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
76
44
|
|
|
77
|
-
|
|
78
|
-
|
|
45
|
+
Flags:
|
|
46
|
+
-c, --config Path to .toml configuration file [string]
|
|
47
|
+
-h, --help Show help [boolean]
|
|
48
|
+
-v, --version Show version number [boolean]
|
|
49
|
+
|
|
50
|
+
Options:
|
|
51
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
52
|
+
`);
|
|
53
|
+
|
|
54
|
+
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
79
55
|
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("invalid command", () => {
|
|
59
|
+
it("should display an error", async () => {
|
|
60
|
+
await expect(
|
|
61
|
+
runWrangler("invalid-command")
|
|
62
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
63
|
+
`"Unknown command: invalid-command."`
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(std.out).toMatchInlineSnapshot(`""`);
|
|
67
|
+
expect(std.err).toMatchInlineSnapshot(`
|
|
68
|
+
"wrangler
|
|
69
|
+
|
|
70
|
+
Commands:
|
|
71
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
72
|
+
wrangler whoami 🕵️ Retrieve your user info and test your auth config
|
|
73
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
74
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
75
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
76
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
77
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
78
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
79
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
80
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
80
81
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
Flags:
|
|
83
|
+
-c, --config Path to .toml configuration file [string]
|
|
84
|
+
-h, --help Show help [boolean]
|
|
85
|
+
-v, --version Show version number [boolean]
|
|
86
|
+
|
|
87
|
+
Options:
|
|
88
|
+
-l, --local Run on my machine [boolean] [default: false]
|
|
89
|
+
|
|
90
|
+
Unknown command: invalid-command."
|
|
91
|
+
`);
|
|
84
92
|
});
|
|
93
|
+
});
|
|
85
94
|
|
|
95
|
+
describe("init", () => {
|
|
86
96
|
it("should create a wrangler.toml", async () => {
|
|
87
|
-
|
|
97
|
+
mockConfirm({
|
|
98
|
+
text: "No package.json found. Would you like to create one?",
|
|
99
|
+
result: false,
|
|
100
|
+
});
|
|
101
|
+
await runWrangler("init");
|
|
88
102
|
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
89
103
|
expect(typeof parsed.compatibility_date).toBe("string");
|
|
104
|
+
expect(fs.existsSync("./package.json")).toBe(false);
|
|
105
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
90
106
|
});
|
|
91
107
|
|
|
92
|
-
it("should
|
|
93
|
-
fs.
|
|
94
|
-
|
|
95
|
-
|
|
108
|
+
it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
|
|
109
|
+
fs.writeFileSync(
|
|
110
|
+
"./wrangler.toml",
|
|
111
|
+
'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
|
|
112
|
+
"utf-8"
|
|
113
|
+
);
|
|
114
|
+
mockConfirm({
|
|
115
|
+
text: "Do you want to continue initializing this project?",
|
|
116
|
+
result: false,
|
|
117
|
+
});
|
|
118
|
+
await runWrangler("init");
|
|
119
|
+
expect(std.warn).toContain("wrangler.toml file already exists!");
|
|
120
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
121
|
+
expect(parsed.compatibility_date).toBe("something-else");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
|
|
125
|
+
fs.writeFileSync(
|
|
126
|
+
"./wrangler.toml",
|
|
127
|
+
`compatibility_date="something-else"`,
|
|
128
|
+
"utf-8"
|
|
129
|
+
);
|
|
130
|
+
mockConfirm(
|
|
131
|
+
{
|
|
132
|
+
text: "Do you want to continue initializing this project?",
|
|
133
|
+
result: true,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
text: "No package.json found. Would you like to create one?",
|
|
137
|
+
result: false,
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
await runWrangler("init");
|
|
141
|
+
expect(std.warn).toContain("wrangler.toml file already exists!");
|
|
142
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
143
|
+
expect(parsed.compatibility_date).toBe("something-else");
|
|
96
144
|
});
|
|
97
|
-
});
|
|
98
145
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
146
|
+
it("should create a package.json if none is found and user confirms", async () => {
|
|
147
|
+
mockConfirm(
|
|
148
|
+
{
|
|
149
|
+
text: "No package.json found. Would you like to create one?",
|
|
150
|
+
result: true,
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
text: "Would you like to use TypeScript?",
|
|
154
|
+
result: false,
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
text: "Would you like to create a Worker at src/index.js?",
|
|
158
|
+
result: false,
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
await runWrangler("init");
|
|
162
|
+
expect(fs.existsSync("./package.json")).toBe(true);
|
|
163
|
+
const packageJson = JSON.parse(
|
|
164
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
165
|
+
);
|
|
166
|
+
expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
|
|
167
|
+
expect(packageJson.version).toEqual("0.0.0");
|
|
168
|
+
expect(packageJson.devDependencies).toEqual({
|
|
169
|
+
wrangler: expect.any(String),
|
|
111
170
|
});
|
|
171
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
172
|
+
expect(mockPackageManager.install).toHaveBeenCalled();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("should not touch an existing package.json in the same directory", async () => {
|
|
176
|
+
mockConfirm(
|
|
177
|
+
{
|
|
178
|
+
text: "Would you like to install wrangler into your package.json?",
|
|
179
|
+
result: false,
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
text: "Would you like to use TypeScript?",
|
|
183
|
+
result: false,
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
text: "Would you like to create a Worker at src/index.js?",
|
|
187
|
+
result: false,
|
|
188
|
+
}
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
fs.writeFileSync(
|
|
192
|
+
"./package.json",
|
|
193
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
194
|
+
"utf-8"
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
await runWrangler("init");
|
|
198
|
+
const packageJson = JSON.parse(
|
|
199
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
200
|
+
);
|
|
201
|
+
expect(packageJson.name).toEqual("test");
|
|
202
|
+
expect(packageJson.version).toEqual("1.0.0");
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("should offer to install wrangler into an existing package.json", async () => {
|
|
206
|
+
mockConfirm(
|
|
207
|
+
{
|
|
208
|
+
text: "Would you like to install wrangler into your package.json?",
|
|
209
|
+
result: true,
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
text: "Would you like to use TypeScript?",
|
|
213
|
+
result: false,
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
text: "Would you like to create a Worker at src/index.js?",
|
|
217
|
+
result: false,
|
|
218
|
+
}
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
fs.writeFileSync(
|
|
222
|
+
"./package.json",
|
|
223
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
224
|
+
"utf-8"
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
await runWrangler("init");
|
|
228
|
+
const packageJson = JSON.parse(
|
|
229
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
230
|
+
);
|
|
231
|
+
expect(packageJson.name).toEqual("test");
|
|
232
|
+
expect(packageJson.version).toEqual("1.0.0");
|
|
233
|
+
expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
|
|
234
|
+
`wrangler@${wranglerVersion}`
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("should not touch an existing package.json in an ancestor directory", async () => {
|
|
239
|
+
mockConfirm(
|
|
240
|
+
{
|
|
241
|
+
text: "Would you like to install wrangler into your package.json?",
|
|
242
|
+
result: false,
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
text: "Would you like to use TypeScript?",
|
|
246
|
+
result: false,
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
text: "Would you like to create a Worker at src/index.js?",
|
|
250
|
+
result: false,
|
|
251
|
+
}
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
fs.writeFileSync(
|
|
255
|
+
"./package.json",
|
|
256
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
257
|
+
"utf-8"
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
261
|
+
process.chdir("./sub-1/sub-2");
|
|
262
|
+
|
|
263
|
+
await runWrangler("init");
|
|
264
|
+
expect(fs.existsSync("./package.json")).toBe(false);
|
|
265
|
+
expect(fs.existsSync("../../package.json")).toBe(true);
|
|
266
|
+
|
|
267
|
+
const packageJson = JSON.parse(
|
|
268
|
+
fs.readFileSync("../../package.json", "utf-8")
|
|
269
|
+
);
|
|
270
|
+
expect(packageJson).toMatchInlineSnapshot(`
|
|
271
|
+
Object {
|
|
272
|
+
"name": "test",
|
|
273
|
+
"version": "1.0.0",
|
|
274
|
+
}
|
|
275
|
+
`);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("should offer to create a worker in a non-typescript project", async () => {
|
|
279
|
+
mockConfirm(
|
|
280
|
+
{
|
|
281
|
+
text: "Would you like to install wrangler into your package.json?",
|
|
282
|
+
result: false,
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
text: "Would you like to use TypeScript?",
|
|
286
|
+
result: false,
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
text: "Would you like to create a Worker at src/index.js?",
|
|
290
|
+
result: true,
|
|
291
|
+
}
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
fs.writeFileSync(
|
|
295
|
+
"./package.json",
|
|
296
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
297
|
+
"utf-8"
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
await runWrangler("init");
|
|
301
|
+
expect(fs.existsSync("./src/index.js")).toBe(true);
|
|
302
|
+
expect(fs.existsSync("./src/index.ts")).toBe(false);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("should offer to create a worker in a typescript project", async () => {
|
|
306
|
+
mockConfirm(
|
|
307
|
+
{
|
|
308
|
+
text: "Would you like to install wrangler into your package.json?",
|
|
309
|
+
result: false,
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
text: "Would you like to use TypeScript?",
|
|
313
|
+
result: true,
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
text: "Would you like to create a Worker at src/index.ts?",
|
|
317
|
+
result: true,
|
|
318
|
+
}
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
fs.writeFileSync(
|
|
322
|
+
"./package.json",
|
|
323
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
324
|
+
"utf-8"
|
|
325
|
+
);
|
|
112
326
|
|
|
113
|
-
await
|
|
114
|
-
expect(
|
|
115
|
-
|
|
116
|
-
).toBeTruthy();
|
|
327
|
+
await runWrangler("init");
|
|
328
|
+
expect(fs.existsSync("./src/index.js")).toBe(false);
|
|
329
|
+
expect(fs.existsSync("./src/index.ts")).toBe(true);
|
|
117
330
|
});
|
|
118
331
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
332
|
+
it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
|
|
333
|
+
mockConfirm(
|
|
334
|
+
{
|
|
335
|
+
text: "Would you like to install wrangler into your package.json?",
|
|
336
|
+
result: false,
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
text: "Would you like to use TypeScript?",
|
|
340
|
+
result: false,
|
|
126
341
|
}
|
|
127
342
|
);
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
(
|
|
343
|
+
|
|
344
|
+
fs.writeFileSync(
|
|
345
|
+
"./package.json",
|
|
346
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
347
|
+
"utf-8"
|
|
132
348
|
);
|
|
133
|
-
|
|
349
|
+
fs.mkdirSync("./src", { recursive: true });
|
|
350
|
+
const PLACEHOLDER = "/* placeholder text */";
|
|
351
|
+
fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
|
|
352
|
+
|
|
353
|
+
await runWrangler("init");
|
|
354
|
+
expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
|
|
355
|
+
expect(fs.existsSync("./src/index.ts")).toBe(false);
|
|
134
356
|
});
|
|
135
357
|
|
|
136
|
-
it("
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
358
|
+
it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
|
|
359
|
+
mockConfirm(
|
|
360
|
+
{
|
|
361
|
+
text: "Would you like to install wrangler into your package.json?",
|
|
362
|
+
result: false,
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
text: "Would you like to use TypeScript?",
|
|
366
|
+
result: true,
|
|
145
367
|
}
|
|
146
368
|
);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
369
|
+
|
|
370
|
+
fs.writeFileSync(
|
|
371
|
+
"./package.json",
|
|
372
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
373
|
+
"utf-8"
|
|
374
|
+
);
|
|
375
|
+
fs.mkdirSync("./src", { recursive: true });
|
|
376
|
+
const PLACEHOLDER = "/* placeholder text */";
|
|
377
|
+
fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
|
|
378
|
+
|
|
379
|
+
await runWrangler("init");
|
|
380
|
+
expect(fs.existsSync("./src/index.js")).toBe(false);
|
|
381
|
+
expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
|
|
385
|
+
mockConfirm(
|
|
386
|
+
{
|
|
387
|
+
text: "No package.json found. Would you like to create one?",
|
|
388
|
+
result: true,
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
text: "Would you like to use TypeScript?",
|
|
392
|
+
result: true,
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
text: "Would you like to create a Worker at src/index.ts?",
|
|
396
|
+
result: false,
|
|
397
|
+
}
|
|
150
398
|
);
|
|
399
|
+
await runWrangler("init");
|
|
400
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(true);
|
|
401
|
+
const tsconfigJson = JSON.parse(
|
|
402
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
403
|
+
);
|
|
404
|
+
expect(tsconfigJson.compilerOptions.types).toEqual([
|
|
405
|
+
"@cloudflare/workers-types",
|
|
406
|
+
]);
|
|
407
|
+
expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
|
|
408
|
+
"@cloudflare/workers-types",
|
|
409
|
+
"typescript"
|
|
410
|
+
);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
it("should not touch an existing tsconfig.json in the same directory", async () => {
|
|
414
|
+
fs.writeFileSync(
|
|
415
|
+
"./package.json",
|
|
416
|
+
JSON.stringify({
|
|
417
|
+
name: "test",
|
|
418
|
+
version: "1.0.0",
|
|
419
|
+
devDependencies: {
|
|
420
|
+
wrangler: "0.0.0",
|
|
421
|
+
"@cloudflare/workers-types": "0.0.0",
|
|
422
|
+
},
|
|
423
|
+
}),
|
|
424
|
+
"utf-8"
|
|
425
|
+
);
|
|
426
|
+
fs.writeFileSync(
|
|
427
|
+
"./tsconfig.json",
|
|
428
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
429
|
+
"utf-8"
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
await runWrangler("init");
|
|
433
|
+
const tsconfigJson = JSON.parse(
|
|
434
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
435
|
+
);
|
|
436
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("should offer to install type definitions in an existing typescript project", async () => {
|
|
440
|
+
mockConfirm(
|
|
441
|
+
{
|
|
442
|
+
text: "Would you like to install wrangler into your package.json?",
|
|
443
|
+
result: false,
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
text: "Would you like to install the type definitions for Workers into your package.json?",
|
|
447
|
+
result: true,
|
|
448
|
+
},
|
|
449
|
+
{
|
|
450
|
+
text: "Would you like to create a Worker at src/index.ts?",
|
|
451
|
+
result: false,
|
|
452
|
+
}
|
|
453
|
+
);
|
|
454
|
+
fs.writeFileSync(
|
|
455
|
+
"./package.json",
|
|
456
|
+
JSON.stringify({
|
|
457
|
+
name: "test",
|
|
458
|
+
version: "1.0.0",
|
|
459
|
+
}),
|
|
460
|
+
"utf-8"
|
|
461
|
+
);
|
|
462
|
+
fs.writeFileSync(
|
|
463
|
+
"./tsconfig.json",
|
|
464
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
465
|
+
"utf-8"
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
await runWrangler("init");
|
|
469
|
+
const tsconfigJson = JSON.parse(
|
|
470
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
471
|
+
);
|
|
472
|
+
// unchanged tsconfig
|
|
473
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
474
|
+
expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
|
|
475
|
+
"@cloudflare/workers-types"
|
|
476
|
+
);
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
|
|
480
|
+
fs.writeFileSync(
|
|
481
|
+
"./package.json",
|
|
482
|
+
JSON.stringify({
|
|
483
|
+
name: "test",
|
|
484
|
+
version: "1.0.0",
|
|
485
|
+
devDependencies: {
|
|
486
|
+
wrangler: "0.0.0",
|
|
487
|
+
"@cloudflare/workers-types": "0.0.0",
|
|
488
|
+
},
|
|
489
|
+
}),
|
|
490
|
+
"utf-8"
|
|
491
|
+
);
|
|
492
|
+
fs.writeFileSync(
|
|
493
|
+
"./tsconfig.json",
|
|
494
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
495
|
+
"utf-8"
|
|
496
|
+
);
|
|
497
|
+
|
|
498
|
+
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
499
|
+
process.chdir("./sub-1/sub-2");
|
|
500
|
+
|
|
501
|
+
await runWrangler("init");
|
|
502
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
503
|
+
expect(fs.existsSync("../../tsconfig.json")).toBe(true);
|
|
504
|
+
|
|
505
|
+
const tsconfigJson = JSON.parse(
|
|
506
|
+
fs.readFileSync("../../tsconfig.json", "utf-8")
|
|
507
|
+
);
|
|
508
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it("should error if `--type` is used", async () => {
|
|
512
|
+
await expect(
|
|
513
|
+
runWrangler("init --type")
|
|
514
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
515
|
+
`"The --type option is no longer supported."`
|
|
516
|
+
);
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it("should error if `--type javascript` is used", async () => {
|
|
520
|
+
await expect(
|
|
521
|
+
runWrangler("init --type javascript")
|
|
522
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
523
|
+
`"The --type option is no longer supported."`
|
|
524
|
+
);
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
it("should error if `--type rust` is used", async () => {
|
|
528
|
+
await expect(
|
|
529
|
+
runWrangler("init --type rust")
|
|
530
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
531
|
+
`"The --type option is no longer supported."`
|
|
532
|
+
);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
it("should error if `--type webpack` is used", async () => {
|
|
536
|
+
await expect(runWrangler("init --type webpack")).rejects
|
|
537
|
+
.toThrowErrorMatchingInlineSnapshot(`
|
|
538
|
+
"The --type option is no longer supported.
|
|
539
|
+
If you wish to use webpack then you will need to create a custom build."
|
|
540
|
+
`);
|
|
151
541
|
});
|
|
152
542
|
});
|
|
153
543
|
});
|