From 4880d54341ab442d7dca81ae0e0374ef8032fea3 Mon Sep 17 00:00:00 2001 From: Jacob M-G Evans <27247160+JacobMGEvans@users.noreply.github.com> Date: Tue, 21 Jun 2022 17:13:56 -0500 Subject: [PATCH] feat: resolve `--site` cli arg relative to current working directory (#1327) Before we were resolving the Site directory relative to the location of `wrangler.toml` at all times. Now the `--site` cli arg is resolved relative to current working directory. resolves #1243 --- .changeset/large-lizards-enjoy.md | 10 +++++ .../wrangler/src/__tests__/publish.test.ts | 42 +++++++++++++++++++ packages/wrangler/src/sites.tsx | 30 +++++++------ 3 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 .changeset/large-lizards-enjoy.md diff --git a/.changeset/large-lizards-enjoy.md b/.changeset/large-lizards-enjoy.md new file mode 100644 index 000000000000..3d337a18c662 --- /dev/null +++ b/.changeset/large-lizards-enjoy.md @@ -0,0 +1,10 @@ +--- +"wrangler": patch +--- + +feat: resolve `--site` cli arg relative to current working directory + +Before we were resolving the Site directory relative to the location of `wrangler.toml` at all times. +Now the `--site` cli arg is resolved relative to current working directory. + +resolves #1243 diff --git a/packages/wrangler/src/__tests__/publish.test.ts b/packages/wrangler/src/__tests__/publish.test.ts index ed52501724d8..012afd4ec2a9 100644 --- a/packages/wrangler/src/__tests__/publish.test.ts +++ b/packages/wrangler/src/__tests__/publish.test.ts @@ -2585,6 +2585,48 @@ addEventListener('fetch', event => {});` `); expect(std.err).toMatchInlineSnapshot(`""`); }); + + it("should use the relative path from current working directory to Worker directory when using --site", async () => { + writeWranglerToml({ + main: "./index.js", + }); + const assets = [ + { filePath: "file-1.txt", content: "Content of file-1" }, + { filePath: "file-2.txt", content: "Content of file-2" }, + ]; + writeAssets(assets, "my-assets"); + + const kvNamespace = { + title: "__test-name-workers_sites_assets", + id: "__test-name-workers_sites_assets-id", + }; + + mockSubDomainRequest(); + writeWorkerSource(); + mockUploadWorkerRequest(); + mockListKVNamespacesRequest(kvNamespace); + mockKeyListRequest(kvNamespace.id, []); + mockUploadAssetsToKVRequest(kvNamespace.id, assets); + process.chdir("./my-assets"); + await runWrangler("publish --site ."); + + expect(std).toMatchInlineSnapshot(` + Object { + "debug": "", + "err": "", + "out": "Reading file-1.txt... + Uploading as file-1.2ca234f380.txt... + Reading file-2.txt... + Uploading as file-2.5938485188.txt... + ↗️ Done syncing assets + Total Upload: 0xx KiB / gzip: 0xx KiB + Uploaded test-name (TIMINGS) + Published test-name (TIMINGS) + test-name.test-sub-domain.workers.dev", + "warn": "", + } + `); + }); }); describe("workers_dev setting", () => { diff --git a/packages/wrangler/src/sites.tsx b/packages/wrangler/src/sites.tsx index f5c25941862b..4e60be93fa3f 100644 --- a/packages/wrangler/src/sites.tsx +++ b/packages/wrangler/src/sites.tsx @@ -349,20 +349,24 @@ export function getAssetPaths( */ export function getSiteAssetPaths( config: Config, - assetDirectory = config.site?.bucket, + assetDirectory?: string, includePatterns = config.site?.include ?? [], excludePatterns = config.site?.exclude ?? [] ): AssetPaths | undefined { - const baseDirectory = path.resolve( - path.dirname(config.configPath ?? "wrangler.toml") - ); - - return assetDirectory - ? { - baseDirectory, - assetDirectory, - includePatterns, - excludePatterns, - } - : undefined; + const baseDirectory = assetDirectory + ? process.cwd() + : path.resolve(path.dirname(config.configPath ?? "wrangler.toml")); + + assetDirectory ??= config.site?.bucket; + + if (assetDirectory) { + return { + baseDirectory, + assetDirectory, + includePatterns, + excludePatterns, + }; + } else { + return undefined; + } }