import type * as sunbeam from "https://deno.land/x/sunbeam/mod.ts";
import * as dates from "npm:date-fns";
const manifest = {
title: "Deno Deploy",
description: "Manage your Deno Deploy projects",
preferences: [
{
name: "token",
label: "Access Token",
type: "text",
},
],
commands: [
{
name: "projects",
title: "List Projects",
mode: "filter",
},
{
name: "dashboard",
title: "Open Dashboard",
mode: "silent",
},
{
name: "deployments",
title: "List Deployments",
hidden: true,
mode: "filter",
params: [{ name: "project", label: "Project", type: "text" }],
},
{
name: "playground",
title: "View Playground",
hidden: true,
mode: "detail",
params: [{ name: "project", label: "Project", type: "text" }],
},
],
} as const satisfies sunbeam.Manifest;
if (Deno.args.length == 0) {
console.log(JSON.stringify(manifest));
Deno.exit(0);
}
const payload: sunbeam.Payload<typeof manifest> = JSON.parse(Deno.args[0]);
const deployToken = payload.preferences.token;
if (!deployToken) {
console.error("Missing deploy token");
Deno.exit(1);
}
try {
const res = await run(payload);
if (res) {
console.log(JSON.stringify(res));
}
} catch (e) {
console.error(e);
Deno.exit(1);
}
async function run(payload: sunbeam.Payload<typeof manifest>) {
switch (payload.command) {
case "dashboard": {
await new Deno.Command("sunbeam", {
args: ["open", "https://dash.deno.com"],
}).output();
return;
}
case "projects": {
const resp = await fetchDeployAPI("/projects");
if (resp.status != 200) {
throw new Error("Failed to fetch projects");
}
const projects = await resp.json();
return {
items: projects.map((project: any) => {
const item: sunbeam.ListItem = {
title: project.name,
accessories: [project.type],
actions: [
{
title: "Open Dashboard",
type: "open",
open: {
url: `https://dash.deno.com/projects/${project.id}`,
},
},
],
};
if (project.hasProductionDeployment) {
const domains = project.productionDeployment.deployment.domainMappings;
const domain = domains.length