Skip to content

Commit

Permalink
feat: download & initialize a wrangler project from dashboard worker
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JacobMGEvans committed Aug 12, 2022
1 parent 249361c commit f514540
Show file tree
Hide file tree
Showing 6 changed files with 735 additions and 397 deletions.
12 changes: 12 additions & 0 deletions .changeset/six-needles-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"wrangler": patch
---

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](https://github.com/nodejs/undici/issues/974), so a temporary workaround to slice off top and bottom boundaries is in place.
33 changes: 33 additions & 0 deletions packages/wrangler/src/__tests__/helpers/mock-cfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export function unsetAllMocks() {

const kvGetMocks = new Map<string, string | Buffer>();
const r2GetMocks = new Map<string, string | undefined>();
const dashScriptMocks = new Map<string, string | undefined>();

/**
* @mocked typeof fetchKVGetValue
Expand Down Expand Up @@ -260,4 +261,36 @@ export function setMockFetchR2Objects({
export function unsetSpecialMockFns() {
kvGetMocks.clear();
r2GetMocks.clear();
dashScriptMocks.clear();
}

/**
* @mocked typeof fetchDashScript
* multipart/form-data is the response for modules and raw text for the Script endpoint.
*/
export async function mockFetchDashScript(resource: string): Promise<string> {
if (dashScriptMocks.has(resource)) {
const value = dashScriptMocks.get(resource) ?? "";

return value;
}
throw new Error(`no mock found for \`init from-dash\` - ${resource}`);
}

/**
* Mock setter for usage within test blocks, companion helper to `mockFetchDashScript`
*/
export function setMockFetchDashScript({
accountId,
fromDashScriptName,
mockResponse,
}: {
accountId: string;
fromDashScriptName: string;
mockResponse?: string;
}) {
dashScriptMocks.set(
`/accounts/${accountId}/workers/scripts/${fromDashScriptName}`,
mockResponse
);
}

0 comments on commit f514540

Please sign in to comment.