From 6c44274e7792e775d5c4139ca870fa7e324c5f96 Mon Sep 17 00:00:00 2001 From: Sunil Pai Date: Thu, 3 Mar 2022 16:00:59 +0000 Subject: [PATCH] feat: `--local` mode only applies in `wrangler dev` We'd originally planned for `--local` mode to be a thing across all wrangler commands. In hindsight, that didn't make much sense, since every command other than `wrangler dev` assumes some interaction with cloudflare and their API. The only command other than dev where this "worked" was `kv`, but even that didn't make sense because wrangler dev wouldn't even read from it. We also have `--experimental-enable-local-persistence` there anyway. So this moves the `--local` flag to only apply for `wrangler dev` and removes any trace from other commands. --- .changeset/gentle-islands-complain.md | 9 + packages/wrangler/src/__tests__/index.test.ts | 8 +- packages/wrangler/src/__tests__/kv.test.ts | 12 - packages/wrangler/src/__tests__/r2.test.ts | 12 - .../wrangler/src/__tests__/secret.test.ts | 40 - packages/wrangler/src/index.tsx | 717 +++++++----------- 6 files changed, 276 insertions(+), 522 deletions(-) create mode 100644 .changeset/gentle-islands-complain.md diff --git a/.changeset/gentle-islands-complain.md b/.changeset/gentle-islands-complain.md new file mode 100644 index 000000000000..15ea4accfa26 --- /dev/null +++ b/.changeset/gentle-islands-complain.md @@ -0,0 +1,9 @@ +--- +"wrangler": patch +--- + +feat: `--local` mode only applies in `wrangler dev` + +We'd originally planned for `--local` mode to be a thing across all wrangler commands. In hindsight, that didn't make much sense, since every command other than `wrangler dev` assumes some interaction with cloudflare and their API. The only command other than dev where this "worked" was `kv`, but even that didn't make sense because wrangler dev wouldn't even read from it. We also have `--experimental-enable-local-persistence` there anyway. + +So this moves the `--local` flag to only apply for `wrangler dev` and removes any trace from other commands. diff --git a/packages/wrangler/src/__tests__/index.test.ts b/packages/wrangler/src/__tests__/index.test.ts index 2ee28dba7953..4635fd74b6b7 100644 --- a/packages/wrangler/src/__tests__/index.test.ts +++ b/packages/wrangler/src/__tests__/index.test.ts @@ -48,10 +48,7 @@ describe("wrangler", () => { -c, --config Path to .toml configuration file [string] -h, --help Show help [boolean] -v, --version Show version number [boolean] - --legacy-env Use legacy environments [boolean] - - Options: - -l, --local Run on my machine [boolean] [default: false]" + --legacy-env Use legacy environments [boolean]" `); expect(std.err).toMatchInlineSnapshot(`""`); @@ -89,9 +86,6 @@ describe("wrangler", () => { -v, --version Show version number [boolean] --legacy-env Use legacy environments [boolean] - Options: - -l, --local Run on my machine [boolean] [default: false] - Unknown command: invalid-command." `); }); diff --git a/packages/wrangler/src/__tests__/kv.test.ts b/packages/wrangler/src/__tests__/kv.test.ts index 2c5767d01391..6a4cda330f16 100644 --- a/packages/wrangler/src/__tests__/kv.test.ts +++ b/packages/wrangler/src/__tests__/kv.test.ts @@ -59,7 +59,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --env Perform on a specific environment [string] --preview Interact with a preview namespace [boolean] @@ -89,7 +88,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --env Perform on a specific environment [string] --preview Interact with a preview namespace [boolean] @@ -120,7 +118,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --env Perform on a specific environment [string] --preview Interact with a preview namespace [boolean] @@ -285,7 +282,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The name of the namespace to delete [string] --namespace-id The id of the namespace to delete [string] --env Perform on a specific environment [string] @@ -481,7 +477,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The binding of the namespace to write to [string] --namespace-id The id of the namespace to write to [string] --env Perform on a specific environment [string] @@ -518,7 +513,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The binding of the namespace to write to [string] --namespace-id The id of the namespace to write to [string] --env Perform on a specific environment [string] @@ -555,7 +549,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The binding of the namespace to write to [string] --namespace-id The id of the namespace to write to [string] --env Perform on a specific environment [string] @@ -592,7 +585,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The binding of the namespace to write to [string] --namespace-id The id of the namespace to write to [string] --env Perform on a specific environment [string] @@ -629,7 +621,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The binding of the namespace to write to [string] --namespace-id The id of the namespace to write to [string] --env Perform on a specific environment [string] @@ -955,7 +946,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The name of the namespace to get from [string] --namespace-id The id of the namespace to get from [string] --env Perform on a specific environment [string] @@ -987,7 +977,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The name of the namespace to get from [string] --namespace-id The id of the namespace to get from [string] --env Perform on a specific environment [string] @@ -1020,7 +1009,6 @@ describe("wrangler", () => { --legacy-env Use legacy environments [boolean] Options: - -l, --local Run on my machine [boolean] [default: false] --binding The name of the namespace to get from [string] --namespace-id The id of the namespace to get from [string] --env Perform on a specific environment [string] diff --git a/packages/wrangler/src/__tests__/r2.test.ts b/packages/wrangler/src/__tests__/r2.test.ts index 2d78b60ba166..5679725bebb5 100644 --- a/packages/wrangler/src/__tests__/r2.test.ts +++ b/packages/wrangler/src/__tests__/r2.test.ts @@ -82,9 +82,6 @@ describe("wrangler", () => { -v, --version Show version number [boolean] --legacy-env Use legacy environments [boolean] - Options: - -l, --local Run on my machine [boolean] [default: false] - Not enough non-option arguments: got 0, need at least 1" `); }); @@ -110,9 +107,6 @@ describe("wrangler", () => { -v, --version Show version number [boolean] --legacy-env Use legacy environments [boolean] - Options: - -l, --local Run on my machine [boolean] [default: false] - Unexpected additional positional arguments \\"def ghi\\"." `); }); @@ -164,9 +158,6 @@ describe("wrangler", () => { -v, --version Show version number [boolean] --legacy-env Use legacy environments [boolean] - Options: - -l, --local Run on my machine [boolean] [default: false] - Not enough non-option arguments: got 0, need at least 1" `); }); @@ -192,9 +183,6 @@ describe("wrangler", () => { -v, --version Show version number [boolean] --legacy-env Use legacy environments [boolean] - Options: - -l, --local Run on my machine [boolean] [default: false] - Unexpected additional positional arguments \\"def ghi\\"." `); }); diff --git a/packages/wrangler/src/__tests__/secret.test.ts b/packages/wrangler/src/__tests__/secret.test.ts index 69e21c6f08c2..46443e85717d 100644 --- a/packages/wrangler/src/__tests__/secret.test.ts +++ b/packages/wrangler/src/__tests__/secret.test.ts @@ -123,22 +123,6 @@ describe("wrangler secret", () => { `); expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`); }); - - it("warns about being a no-op in local mode", async () => { - mockPrompt({ - text: "Enter a secret value:", - type: "password", - result: "the-secret", - }); - - mockPutRequest({ name: "the-secret-name", text: "the-secret" }); - await runWrangler("secret put the-key --name script-name --local"); - expect(std.out).toMatchInlineSnapshot(`""`); - expect(std.err).toMatchInlineSnapshot(`""`); - expect(std.warn).toMatchInlineSnapshot( - `"\`wrangler secret put\` is a no-op in --local mode"` - ); - }); }); describe("delete", () => { @@ -244,21 +228,6 @@ describe("wrangler secret", () => { `); expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`); }); - - it("warns about being a no-op in local mode", async () => { - mockConfirm({ - text: "Are you sure you want to permanently delete the variable the-key on the script script-name?", - result: true, - }); - await runWrangler("secret delete the-key --name script-name --local"); - expect(std.out).toMatchInlineSnapshot( - `"🌀 Deleting the secret the-key on script script-name"` - ); - expect(std.err).toMatchInlineSnapshot(`""`); - expect(std.warn).toMatchInlineSnapshot( - `"\`wrangler secret delete\` is a no-op in --local mode"` - ); - }); }); describe("list", () => { @@ -353,14 +322,5 @@ describe("wrangler secret", () => { `); expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`); }); - - it("warns about being a no-op in local mode", async () => { - await runWrangler("secret list --name script-name --local"); - expect(std.out).toMatchInlineSnapshot(`""`); - expect(std.err).toMatchInlineSnapshot(`""`); - expect(std.warn).toMatchInlineSnapshot( - `"\`wrangler secret list\` is a no-op in --local mode"` - ); - }); }); }); diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 60791b983dd0..175b42948e11 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -131,7 +131,6 @@ class DeprecationError extends Error { super(`DEPRECATION WARNING:\n${message}`); } } -class NotImplementedError extends Error {} export async function main(argv: string[]): Promise { const wrangler = makeCLI(argv) @@ -649,6 +648,12 @@ export async function main(argv: string[]): Promise { describe: "The function that is called for each JSX fragment", type: "string", }) + .option("local", { + alias: "l", + describe: "Run on my machine", + type: "boolean", + default: false, // I bet this will a point of contention. We'll revisit it. + }) .option("experimental-enable-local-persistence", { describe: "Enable persistence for this session (only for local mode)", type: "boolean", @@ -853,12 +858,6 @@ export async function main(argv: string[]): Promise { }); }, async (args) => { - if (args.local) { - throw new NotImplementedError( - "🚫 Local publishing is not yet supported" - ); - } - if (args["experimental-public"]) { console.warn( "🚨 The --experimental-public field is experimental and will change in the future." @@ -882,23 +881,21 @@ export async function main(argv: string[]): Promise { ); } - if (!args.local) { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- } + // -- snip, end -- const assetPaths = getAssetPaths( config, @@ -987,12 +984,6 @@ export async function main(argv: string[]): Promise { ); }, async (args) => { - if (args.local) { - throw new NotImplementedError( - `local mode is not yet supported for this command` - ); - } - const config = await readConfig(args.config as ConfigPath); const scriptName = getScriptName(args, config); @@ -1125,23 +1116,21 @@ export async function main(argv: string[]): Promise { const config = await readConfig(args.config as ConfigPath); const entry = getEntry(config, "dev"); - if (!args.local) { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- } + // -- snip, end -- const environments = config.env ?? {}; const envRootObj = args.env ? environments[args.env] || {} : config; @@ -1332,37 +1321,27 @@ export async function main(argv: string[]): Promise { throw new Error("Missing script name"); } - if (args.local) { - console.warn("`wrangler secret put` is a no-op in --local mode"); + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; } - if (!args.local) { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } - + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- } + // -- snip, end -- const secretValue = await prompt( "Enter a secret value:", "password" ); - if (args.local) { - return; - } - console.log( `🌀 Creating the secret for script ${scriptName} ${ args.env && !isLegacyEnv(args, config) ? `(${args.env})` : "" @@ -1459,29 +1438,21 @@ export async function main(argv: string[]): Promise { throw new Error("Missing script name"); } - if (args.local) { - console.warn( - "`wrangler secret delete` is a no-op in --local mode" - ); + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; } - if (!args.local) { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } - + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- } + // -- snip, end -- if ( await confirm( @@ -1498,10 +1469,6 @@ export async function main(argv: string[]): Promise { }` ); - if (args.local) { - return; - } - const url = !args.env || isLegacyEnv(args, config) ? `/accounts/${config.account_id}/workers/scripts/${scriptName}/secrets` @@ -1535,31 +1502,21 @@ export async function main(argv: string[]): Promise { throw new Error("Missing script name"); } - if (args.local) { - console.warn("`wrangler secret list` is a no-op in --local mode"); + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; } - if (!args.local) { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } - + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- - } - - if (args.local) { - return; } + // -- snip, end -- const url = !args.env || isLegacyEnv(args, config) @@ -1624,52 +1581,38 @@ export async function main(argv: string[]): Promise { const preview = args.preview ? "_preview" : ""; const title = `${name}${environment}-${args.namespace}${preview}`; - if (args.local) { - const { Miniflare } = await import("miniflare"); - const mf = new Miniflare({ - kvPersist: (args.kvPersist as string) || true, - // TODO: these options shouldn't be required - script: ` `, // has to be a string with at least one char - }); - await mf.getKVNamespace(title); // this should "create" the namespace - console.log(`✨ Success! Created KV namespace ${title}`); - } else { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- + } + // -- snip, end -- - // TODO: generate a binding name stripping non alphanumeric chars + // TODO: generate a binding name stripping non alphanumeric chars - console.log(`🌀 Creating namespace with title "${title}"`); - const namespaceId = await createNamespace( - config.account_id, - title - ); + console.log(`🌀 Creating namespace with title "${title}"`); + const namespaceId = await createNamespace(config.account_id, title); - console.log("✨ Success!"); - const envString = args.env ? ` under [env.${args.env}]` : ""; - const previewString = args.preview ? "preview_" : ""; - console.log( - `Add the following to your configuration file in your kv_namespaces array${envString}:` - ); - console.log( - `{ binding = "${args.namespace}", ${previewString}id = "${namespaceId}" }` - ); + console.log("✨ Success!"); + const envString = args.env ? ` under [env.${args.env}]` : ""; + const previewString = args.preview ? "preview_" : ""; + console.log( + `Add the following to your configuration file in your kv_namespaces array${envString}:` + ); + console.log( + `{ binding = "${args.namespace}", ${previewString}id = "${namespaceId}" }` + ); - // TODO: automatically write this block to the wrangler.toml config file?? - } + // TODO: automatically write this block to the wrangler.toml config file?? } ) .command( @@ -1679,37 +1622,31 @@ export async function main(argv: string[]): Promise { async (args) => { const config = await readConfig(args.config as ConfigPath); - if (args.local) { - throw new NotImplementedError( - `local mode is not yet supported for this command` - ); - } else { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- + } + // -- snip, end -- - // TODO: we should show bindings if they exist for given ids + // TODO: we should show bindings if they exist for given ids - console.log( - JSON.stringify( - await listNamespaces(config.account_id), - null, - " " - ) - ); - } + console.log( + JSON.stringify( + await listNamespaces(config.account_id), + null, + " " + ) + ); } ) .command( @@ -1738,60 +1675,53 @@ export async function main(argv: string[]): Promise { async (args) => { const config = await readConfig(args.config as ConfigPath); - if (args.local) { - throw new NotImplementedError( - `local mode is not yet supported for this command` + let id; + try { + id = getNamespaceId(args, config); + } catch (e) { + throw new CommandLineArgsError( + "Not able to delete namespace.\n" + ((e as Error).message ?? e) ); - } else { - let id; - try { - id = getNamespaceId(args, config); - } catch (e) { - throw new CommandLineArgsError( - "Not able to delete namespace.\n" + - ((e as Error).message ?? e) - ); - } + } - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- + } + // -- snip, end -- - await fetchResult<{ id: string }>( - `/accounts/${config.account_id}/storage/kv/namespaces/${id}`, - { method: "DELETE" } - ); + await fetchResult<{ id: string }>( + `/accounts/${config.account_id}/storage/kv/namespaces/${id}`, + { method: "DELETE" } + ); - // TODO: recommend they remove it from wrangler.toml + // TODO: recommend they remove it from wrangler.toml - // test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388 - // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n] - // n - // 💁 Not deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388 - // ➜ test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388 - // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n] - // y - // 🌀 Deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388 - // ✨ Success - // ⚠️ Make sure to remove this "kv-namespace" entry from your configuration file! - // ➜ test-mf + // test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388 + // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n] + // n + // 💁 Not deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388 + // ➜ test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388 + // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n] + // y + // 🌀 Deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388 + // ✨ Success + // ⚠️ Make sure to remove this "kv-namespace" entry from your configuration file! + // ➜ test-mf - // TODO: do it automatically + // TODO: do it automatically - // TODO: delete the preview namespace as well? - } + // TODO: delete the preview namespace as well? } ); } @@ -1868,37 +1798,26 @@ export async function main(argv: string[]): Promise { ); } - if (args.local) { - const { Miniflare } = await import("miniflare"); - const mf = new Miniflare({ - kvPersist: (args.kvPersist as string) || true, - // TODO: these options shouldn't be required - script: ` `, // has to be a string with at least one char - }); - const ns = await mf.getKVNamespace(namespaceId); - await ns.put(key, value, { expiration, expirationTtl: ttl }); - } else { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- - - await putKeyValue(config.account_id, namespaceId, key, value, { - expiration, - expiration_ttl: ttl, - }); } + // -- snip, end -- + + await putKeyValue(config.account_id, namespaceId, key, value, { + expiration, + expiration_ttl: ttl, + }); } ) .command( @@ -1935,40 +1854,28 @@ export async function main(argv: string[]): Promise { const config = await readConfig(args.config as ConfigPath); const namespaceId = getNamespaceId(args, config); - if (args.local) { - const { Miniflare } = await import("miniflare"); - const mf = new Miniflare({ - kvPersist: (args.kvPersist as string) || true, - // TODO: these options shouldn't be required - script: ` `, // has to be a string with at least one char - }); - const ns = await mf.getKVNamespace(namespaceId); - const listResponse = await ns.list({ prefix }); - console.log(JSON.stringify(listResponse.keys, null, " ")); // TODO: paginate, collate - } else { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- - - const results = await listNamespaceKeys( - config.account_id, - namespaceId, - prefix - ); - console.log(JSON.stringify(results, undefined, 2)); } + // -- snip, end -- + + const results = await listNamespaceKeys( + config.account_id, + namespaceId, + prefix + ); + console.log(JSON.stringify(results, undefined, 2)); } ) .command( @@ -2009,39 +1916,23 @@ export async function main(argv: string[]): Promise { const config = await readConfig(args.config as ConfigPath); const namespaceId = getNamespaceId(args, config); - if (args.local) { - const { Miniflare } = await import("miniflare"); - const mf = new Miniflare({ - kvPersist: (args.kvPersist as string) || true, - // TODO: these options shouldn't be required - script: ` `, // has to be a string with at least one char - }); - const ns = await mf.getKVNamespace(namespaceId); - console.log(await ns.get(key)); + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); return; } - if (!args.local) { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } - + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- - - console.log( - await getKeyValue(config.account_id, namespaceId, key) - ); } + // -- snip, end -- + + console.log(await getKeyValue(config.account_id, namespaceId, key)); } ) .command( @@ -2080,35 +1971,21 @@ export async function main(argv: string[]): Promise { `deleting the key "${key}" on namespace ${namespaceId}` ); - if (args.local) { - const { Miniflare } = await import("miniflare"); - const mf = new Miniflare({ - kvPersist: (args.kvPersist as string) || true, - // TODO: these options shouldn't be required - script: ` `, // has to be a string with at least one char - }); - const ns = await mf.getKVNamespace(namespaceId); - console.log(await ns.delete(key)); + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); return; } - if (!args.local) { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } - + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- } + // -- snip, end -- await fetchResult( `/accounts/${config.account_id}/storage/kv/namespaces/${namespaceId}/values/${key}`, @@ -2162,46 +2039,25 @@ export async function main(argv: string[]): Promise { const namespaceId = getNamespaceId(args, config); const content = parseJSON(await readFile(filename), filename); - if (args.local) { - const { Miniflare } = await import("miniflare"); - const mf = new Miniflare({ - kvPersist: (args.kvPersist as string) || true, - // TODO: these options shouldn't be required - script: ` `, // has to be a string with at least one char - }); - const ns = await mf.getKVNamespace(namespaceId); - for (const { - key, - value, - expiration, - expiration_ttl, - } of content) { - await ns.put(key, value, { - expiration, - expirationTtl: expiration_ttl, - }); - } - } else { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- - - console.log( - await putBulkKeyValue(config.account_id, namespaceId, content) - ); } + // -- snip, end -- + + console.log( + await putBulkKeyValue(config.account_id, namespaceId, content) + ); } ) .command( @@ -2237,42 +2093,25 @@ export async function main(argv: string[]): Promise { const namespaceId = getNamespaceId(args, config); const content = parseJSON(await readFile(filename), filename); - if (args.local) { - const { Miniflare } = await import("miniflare"); - const mf = new Miniflare({ - kvPersist: (args.kvPersist as string) || true, - // TODO: these options shouldn't be required - script: ` `, // has to be a string with at least one char - }); - const ns = await mf.getKVNamespace(namespaceId); - for (const key of content) { - await ns.delete(key); - } - } else { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- - - console.log( - await deleteBulkKeyValue( - config.account_id, - namespaceId, - content - ) - ); } + // -- snip, end -- + + console.log( + await deleteBulkKeyValue(config.account_id, namespaceId, content) + ); } ); } @@ -2303,42 +2142,6 @@ export async function main(argv: string[]): Promise { const config = await readConfig(args.config as ConfigPath); - if (args.local) { - throw new NotImplementedError( - `local mode is not yet supported for this command` - ); - } else { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } - - if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } - } - // -- snip, end -- - - console.log(`Creating bucket ${args.name}.`); - await createR2Bucket(config.account_id, args.name); - console.log(`Created bucket ${args.name}.`); - } - } - ); - - r2BucketYargs.command("list", "List R2 buckets", {}, async (args) => { - const config = await readConfig(args.config as ConfigPath); - - if (args.local) { - throw new NotImplementedError( - `local mode is not yet supported for this command` - ); - } else { // -- snip, extract -- const loggedIn = await loginOrRefreshIfRequired(); if (!loggedIn) { @@ -2355,10 +2158,34 @@ export async function main(argv: string[]): Promise { } // -- snip, end -- - console.log( - JSON.stringify(await listR2Buckets(config.account_id), null, 2) - ); + console.log(`Creating bucket ${args.name}.`); + await createR2Bucket(config.account_id, args.name); + console.log(`Created bucket ${args.name}.`); } + ); + + r2BucketYargs.command("list", "List R2 buckets", {}, async (args) => { + const config = await readConfig(args.config as ConfigPath); + + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + + if (!config.account_id) { + config.account_id = await getAccountId(); + if (!config.account_id) { + throw new Error("No account id found, quitting..."); + } + } + // -- snip, end -- + + console.log( + JSON.stringify(await listR2Buckets(config.account_id), null, 2) + ); }); r2BucketYargs.command( @@ -2382,31 +2209,25 @@ export async function main(argv: string[]): Promise { const config = await readConfig(args.config as ConfigPath); - if (args.local) { - throw new NotImplementedError( - `local mode is not yet supported for this command` - ); - } else { - // -- snip, extract -- - const loggedIn = await loginOrRefreshIfRequired(); - if (!loggedIn) { - // didn't login, let's just quit - console.log("Did not login, quitting..."); - return; - } + // -- snip, extract -- + const loggedIn = await loginOrRefreshIfRequired(); + if (!loggedIn) { + // didn't login, let's just quit + console.log("Did not login, quitting..."); + return; + } + if (!config.account_id) { + config.account_id = await getAccountId(); if (!config.account_id) { - config.account_id = await getAccountId(); - if (!config.account_id) { - throw new Error("No account id found, quitting..."); - } + throw new Error("No account id found, quitting..."); } - // -- snip, end -- - - console.log(`Deleting bucket ${args.name}.`); - await deleteR2Bucket(config.account_id, args.name); - console.log(`Deleted bucket ${args.name}.`); } + // -- snip, end -- + + console.log(`Deleting bucket ${args.name}.`); + await deleteR2Bucket(config.account_id, args.name); + console.log(`Deleted bucket ${args.name}.`); } ); return r2BucketYargs; @@ -2422,12 +2243,6 @@ export async function main(argv: string[]): Promise { alias: "c", describe: "Path to .toml configuration file", type: "string", - }) - .option("local", { - alias: "l", - describe: "Run on my machine", - type: "boolean", - default: false, // I bet this will a point of contention. We'll revisit it. }); wrangler.group(["config", "help", "version", "legacy-env"], "Flags:");