Skip to content

Commit

Permalink
Have deployment view output in table
Browse files Browse the repository at this point in the history
  • Loading branch information
jspspike committed Mar 2, 2023
1 parent aff4c8f commit 70da2b9
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 206 deletions.
54 changes: 32 additions & 22 deletions .changeset/brown-wasps-fail.md
Expand Up @@ -14,27 +14,37 @@ in a developer's local setup.
example of `view <deployment-id>` output:

```ts
{
Tag: '',
Number: 0,
'Metadata.author_id': 'Picard-Gamma-6-0-7-3',
'Metadata.author_email': 'picard@vinyard.com',
'Metadata.source': 'wrangler',
'Metadata.created_on': '2021-01-01T00:00:00.000000Z',
'Metadata.modified_on': '2021-01-01T00:00:00.000000Z',
'resources.script': {
etag: 'mock-e-tag',
handlers: [ 'fetch' ],
last_deployed_from: 'wrangler'
},
'resources.bindings': []
Deployment ID: 07d7143d-0284-427e-ba22-2d5e6e91b479
Created on: 2023-03-02T21:05:15.622446Z
Author: jspspike@gmail.com
Source: Upload from Wrangler 🤠
------------------------------------------------------------
Author ID: e5a3ca86e08fb0940d3a05691310bb42
Usage Model: bundled
Handlers: fetch
Compatibility Date: 2022-10-03
--------------------------bindings--------------------------
[[r2_buckets]]
binding = "MY_BUCKET"
bucket_name = "testr2"

[[kv_namespaces]]
id = "79300c6d17eb4180a07270f450efe53f"
binding = "yeee"

---------------------------script---------------------------

// index.js
var worker_default = {
fetch(request) {
const base = "https://example.com";
const statusCode = 301;
const destination = new URL(request.url, base);
return Response.redirect(destination.toString(), statusCode);
}



export default {
async fetch(request) {
return new Response('Hello World from Deployment 1701-E');
},
};
};
export {
worker_default as default
};
//# sourceMappingURL=index.js.map
```
44 changes: 21 additions & 23 deletions packages/wrangler/src/__tests__/deployments.test.ts
Expand Up @@ -69,23 +69,23 @@ describe("deployments", () => {
Deployment ID: Constitution-Class
Created on: 2021-01-01T00:00:00.000000Z
Author: Jean-Luc-Picard@federation.org
Source: Upload from Wrangler 🤠
Source: Upload from Wrangler 🤠
Deployment ID: Intrepid-Class
Created on: 2021-02-02T00:00:00.000000Z
Author: Kathryn-Janeway@federation.org
Source: Rollback from Wrangler 🤠
Source: Rollback from Wrangler 🤠
Rollback from: MOCK-DEPLOYMENT-ID-1111
Deployment ID: 3mEgaU1T-Intrepid-someThing
Created on: 2021-02-03T00:00:00.000000Z
Author: Kathryn-Janeway@federation.org
Source: Wrangler 🤠
Source: Wrangler 🤠
Deployment ID: Galaxy-Class
Created on: 2021-01-04T00:00:00.000000Z
Author: Jean-Luc-Picard@federation.org
Source: Rollback from Wrangler 🤠
Source: Rollback from Wrangler 🤠
Rollback from: MOCK-DEPLOYMENT-ID-2222
🟩 Active"
`);
Expand All @@ -100,23 +100,23 @@ describe("deployments", () => {
Deployment ID: Constitution-Class
Created on: 2021-01-01T00:00:00.000000Z
Author: Jean-Luc-Picard@federation.org
Source: Upload from Wrangler 🤠
Source: Upload from Wrangler 🤠
Deployment ID: Intrepid-Class
Created on: 2021-02-02T00:00:00.000000Z
Author: Kathryn-Janeway@federation.org
Source: Rollback from Wrangler 🤠
Source: Rollback from Wrangler 🤠
Rollback from: MOCK-DEPLOYMENT-ID-1111
Deployment ID: 3mEgaU1T-Intrepid-someThing
Created on: 2021-02-03T00:00:00.000000Z
Author: Kathryn-Janeway@federation.org
Source: Wrangler 🤠
Source: Wrangler 🤠
Deployment ID: Galaxy-Class
Created on: 2021-01-04T00:00:00.000000Z
Author: Jean-Luc-Picard@federation.org
Source: Rollback from Wrangler 🤠
Source: Rollback from Wrangler 🤠
Rollback from: MOCK-DEPLOYMENT-ID-2222
🟩 Active"
`);
Expand All @@ -138,21 +138,19 @@ describe("deployments", () => {
expect(std.out).toMatchInlineSnapshot(`
"🚧\`wrangler deployments\` is a beta command. Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose
{
Tag: '',
Number: 0,
'Metadata.author_id': 'Picard-Gamma-6-0-7-3',
'Metadata.author_email': 'Jean-Luc-Picard@federation.org',
'Metadata.source': 'wrangler',
'Metadata.created_on': '2021-01-01T00:00:00.000000Z',
'Metadata.modified_on': '2021-01-01T00:00:00.000000Z',
'resources.script': {
etag: 'mock-e-tag',
handlers: [ 'fetch' ],
last_deployed_from: 'wrangler'
},
'resources.bindings': []
}
Deployment ID: undefined
Created on: 2021-01-01T00:00:00.000000Z
Author: Jean-Luc-Picard@federation.org
Source: Wrangler 🤠
------------------------------------------------------------
Author ID: Picard-Gamma-6-0-7-3
Usage Model: bundled
Handlers: fetch
--------------------------bindings--------------------------
---------------------------script---------------------------
export default {
async fetch(request) {
Expand Down
Expand Up @@ -103,7 +103,7 @@ export const mswSuccessDeploymentDetails = [
createFetchResult({
Tag: "",
Number: 0,
Metadata: {
metadata: {
author_id: "Picard-Gamma-6-0-7-3",
author_email: "Jean-Luc-Picard@federation.org",
source: "wrangler",
Expand All @@ -116,6 +116,9 @@ export const mswSuccessDeploymentDetails = [
handlers: ["fetch"],
last_deployed_from: "wrangler",
},
script_runtime: {
usage_model: "bundled",
},
bindings: [],
},
})
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/create-worker-upload-form.ts
Expand Up @@ -23,7 +23,7 @@ export function toMimeType(type: CfModuleType): string {
}
}

type WorkerMetadataBinding =
export type WorkerMetadataBinding =
// If you add any new binding types here, also add it to safeBindings
// under validateUnsafeBinding in config/validation.ts
| { type: "plain_text"; name: string; text: string }
Expand Down
77 changes: 53 additions & 24 deletions packages/wrangler/src/deployments.ts
@@ -1,4 +1,8 @@
import type { WorkerMetadataBinding } from "./create-worker-upload-form";
import type { ServiceMetadataRes } from "./init";

import { URLSearchParams } from "url";
import TOML from "@iarna/toml";
import chalk from "chalk";
import { fetchResult, fetchScriptContent } from "./cfetch";
import { readConfig } from "./config";
Expand All @@ -7,9 +11,9 @@ import { logger } from "./logger";
import * as metrics from "./metrics";
import { requireAuth } from "./user";
import { getScriptName, printWranglerBanner } from ".";
import { mapBindings } from "./init";

import type { Config } from "./config";
import type { ServiceMetadataRes } from "./init";
import type { CommonYargsOptions } from "./yargs-types";
import type { ArgumentsCamelCase } from "yargs";

Expand All @@ -28,8 +32,15 @@ type DeploymentDetails = {
modified_on: string;
};
resources: {
script: string;
bindings: unknown[];
script: {
handlers: string[];
};
bindings: WorkerMetadataBinding[];
script_runtime: {
compatibility_date: string | undefined;
compatibility_flags: string[] | undefined;
usage_model: string | undefined;
};
};
};

Expand Down Expand Up @@ -81,7 +92,7 @@ export async function deployments(
Deployment ID: ${versions.id}
Created on: ${versions.metadata.created_on}
Author: ${versions.metadata.author_email}
Source: ${triggerStr}`;
Source: ${triggerStr}`;

if (versions.annotations?.["workers/rollback_from"]) {
version += `\nRollback from: ${versions.annotations["workers/rollback_from"]}`;
Expand Down Expand Up @@ -221,26 +232,44 @@ export async function viewDeployment(
`/accounts/${accountId}/workers/deployments/by-script/${scriptTag}/detail/${deploymentId}`
);

const flatObj: Record<string, unknown> = {};
for (const deployDetailsKey in deploymentDetails) {
if (
Object.prototype.hasOwnProperty.call(deploymentDetails, deployDetailsKey)
) {
//@ts-expect-error flattening objects causes the index signature to error
const value = deploymentDetails[deployDetailsKey];
if (typeof value === "object" && value !== null) {
for (const subKey in value) {
if (Object.prototype.hasOwnProperty.call(value, subKey)) {
flatObj[`${deployDetailsKey}.${subKey}`] = value[subKey];
}
}
} else {
flatObj[deployDetailsKey] = value;
}
}
}

logger.log(flatObj);
const triggerStr = deploymentDetails.annotations?.["workers/triggered_by"]
? `${formatTrigger(
deploymentDetails.annotations["workers/triggered_by"]
)} from ${formatSource(deploymentDetails.metadata.source)}`
: `${formatSource(deploymentDetails.metadata.source)}`;

const rollbackStr = deploymentDetails.annotations?.["workers/rollback_from"]
? `\nRollback from: ${deploymentDetails.annotations["workers/rollback_from"]}`
: ``;

const compatDateStr = deploymentDetails.resources.script_runtime
?.compatibility_date
? `\nCompatibility Date: ${deploymentDetails.resources.script_runtime?.compatibility_date}`
: ``;
const compatFlagsStr = deploymentDetails.resources.script_runtime
?.compatibility_flags
? `\nCompatibility Flags: ${deploymentDetails.resources.script_runtime?.compatibility_flags}`
: ``;

const bindings = mapBindings(deploymentDetails.resources.bindings);

const version = `
Deployment ID: ${deploymentDetails.id}
Created on: ${deploymentDetails.metadata.created_on}
Author: ${deploymentDetails.metadata.author_email}
Source: ${triggerStr}${rollbackStr}
------------------------------------------------------------
Author ID: ${deploymentDetails.metadata.author_id}
Usage Model: ${deploymentDetails.resources.script_runtime.usage_model}
Handlers: ${
deploymentDetails.resources.script.handlers
}${compatDateStr}${compatFlagsStr}
--------------------------bindings--------------------------
${TOML.stringify(bindings as TOML.JsonMap)}
---------------------------script---------------------------
`;

logger.log(version);
logger.log(scriptContent);

// early return to skip the deployments listings
Expand Down

0 comments on commit 70da2b9

Please sign in to comment.