Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 feat: download of Worker script --from-dash #1645

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:/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
);
}
Loading