Skip to content

Commit f514540

Browse files
committed
feat: download & initialize a wrangler project from dashboard worker
Added `wrangler init --from-dash <worker-name>`, which allows initializing a wrangler project from a pre-existing worker in the dashboard. Resolves #1624 Discussion: #1623 Notes: `multiplart/form-data` parsing is [not currently supported in Undici](nodejs/undici#974), so a temporary workaround to slice off top and bottom boundaries is in place.
1 parent 249361c commit f514540

File tree

6 files changed

+735
-397
lines changed

6 files changed

+735
-397
lines changed

.changeset/six-needles-begin.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
feat: download & initialize a wrangler project from dashboard worker
6+
7+
Added `wrangler init --from-dash <worker-name>`, which allows initializing a wrangler project from a pre-existing worker in the dashboard.
8+
9+
Resolves #1624
10+
Discussion: #1623
11+
12+
Notes: `multiplart/form-data` parsing is [not currently supported in Undici](https://github.com/nodejs/undici/issues/974), so a temporary workaround to slice off top and bottom boundaries is in place.

packages/wrangler/src/__tests__/helpers/mock-cfetch.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export function unsetAllMocks() {
183183

184184
const kvGetMocks = new Map<string, string | Buffer>();
185185
const r2GetMocks = new Map<string, string | undefined>();
186+
const dashScriptMocks = new Map<string, string | undefined>();
186187

187188
/**
188189
* @mocked typeof fetchKVGetValue
@@ -260,4 +261,36 @@ export function setMockFetchR2Objects({
260261
export function unsetSpecialMockFns() {
261262
kvGetMocks.clear();
262263
r2GetMocks.clear();
264+
dashScriptMocks.clear();
265+
}
266+
267+
/**
268+
* @mocked typeof fetchDashScript
269+
* multipart/form-data is the response for modules and raw text for the Script endpoint.
270+
*/
271+
export async function mockFetchDashScript(resource: string): Promise<string> {
272+
if (dashScriptMocks.has(resource)) {
273+
const value = dashScriptMocks.get(resource) ?? "";
274+
275+
return value;
276+
}
277+
throw new Error(`no mock found for \`init from-dash\` - ${resource}`);
278+
}
279+
280+
/**
281+
* Mock setter for usage within test blocks, companion helper to `mockFetchDashScript`
282+
*/
283+
export function setMockFetchDashScript({
284+
accountId,
285+
fromDashScriptName,
286+
mockResponse,
287+
}: {
288+
accountId: string;
289+
fromDashScriptName: string;
290+
mockResponse?: string;
291+
}) {
292+
dashScriptMocks.set(
293+
`/accounts/${accountId}/workers/scripts/${fromDashScriptName}`,
294+
mockResponse
295+
);
263296
}

0 commit comments

Comments
 (0)