Skip to content

Commit

Permalink
refactor: move the check for no asset folder into syncAssets()
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Jan 24, 2022
1 parent eb54f23 commit daabbbc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
19 changes: 7 additions & 12 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,18 +571,13 @@ function useWorker(props: {
console.log("⎔ Detected changes, restarting server...");
}

const assets = sitesFolder
? await syncAssets(
accountId,
path.basename(bundle.path),
sitesFolder,
true,
undefined // TODO: env
)
: {
manifest: undefined,
namespace: undefined,
}; // TODO: cancellable?
const assets = await syncAssets(
accountId,
path.basename(bundle.path),
sitesFolder,
true,
undefined // TODO: env
); // TODO: cancellable?

const content = await readFile(bundle.path, "utf-8");
const init: CfWorkerInit = {
Expand Down
4 changes: 1 addition & 3 deletions packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ export default async function publish(props: Props): Promise<void> {
}

const assetPath = props.public || props.site || props.config.site?.bucket; // TODO: allow both
const assets = assetPath
? await syncAssets(accountId, scriptName, assetPath, false)
: { manifest: undefined, namespace: undefined };
const assets = await syncAssets(accountId, scriptName, assetPath, false);

const bindings: CfWorkerInit["bindings"] = {
kv_namespaces: envRootObj.kv_namespaces?.concat(
Expand Down
23 changes: 21 additions & 2 deletions packages/wrangler/src/sites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,32 @@ async function createKVNamespaceIfNotAlreadyExisting(
};
}

/**
* Upload the assets found within the `dirPath` directory to the sites assets KV namespace for
* the worker given by `scriptName`.
*
* @param accountId the account to upload to.
* @param scriptName the name of the worker whose assets we are uploading.
* @param dirPath the path to the directory of assets to upload, or undefined if there are no assets to upload.
* @param preview if true then upload to a "preview" KV namespace.
* @param _env (not implemented).
* @returns a promise for an object mapping the relative paths of the assets to the key of that
* asset in the KV namespace.
*/
export async function syncAssets(
accountId: string,
scriptName: string,
dirPath: string,
dirPath: string | undefined,
preview: boolean,
_env?: string
) {
): Promise<{
manifest: { [filePath: string]: string } | undefined;
namespace: string | undefined;
}> {
if (dirPath === undefined) {
return { manifest: undefined, namespace: undefined };
}

const title = `__${scriptName}_sites_assets${preview ? "_preview" : ""}`;
const { id: namespace } = await createKVNamespaceIfNotAlreadyExisting(
title,
Expand Down

0 comments on commit daabbbc

Please sign in to comment.