Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/helpers/command.ts

This file was deleted.

8 changes: 2 additions & 6 deletions src/helpers/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import process from "node:process";
import * as console from "node:console";
import { getCommandBase } from "./command.ts";
import { clearAuthFromConfig } from "./config.ts";

export function logAndQuit(message: string): never {
Expand All @@ -19,17 +18,14 @@ export function logSupportCTAAndQuit(): never {
logAndQuit(supportCTA);
}

const base = getCommandBase();
const loginCommand = `${base} login`;

export function logLoginMessageAndQuit(): never {
logAndQuit(`You need to login first.\n\n\t$ ${loginCommand}\n`);
logAndQuit(`You need to login first.\n\n\t$ sf login\n`);
}

export async function logSessionTokenExpiredAndQuit(): Promise<never> {
await clearAuthFromConfig();
logAndQuit(
`\nYour session has expired. Please login again.\n\n\t$ ${loginCommand}\n`,
`\nYour session has expired. Please login again.\n\n\t$ sf login\n`,
);
}

Expand Down
29 changes: 8 additions & 21 deletions src/lib/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import type { Command } from "@commander-js/extra-typings";
import { confirm, input, select } from "@inquirer/prompts";
import { gray, green, magenta, red, white } from "jsr:@std/fmt/colors";
import { cyan, gray, green, magenta, red, white } from "jsr:@std/fmt/colors";
import Table from "cli-table3";
import dayjs from "dayjs";
import * as console from "node:console";
import process from "node:process";
import ora from "ora";
import { getCommandBase } from "../helpers/command.ts";
import { getAuthToken, isLoggedIn } from "../helpers/config.ts";
import {
logLoginMessageAndQuit,
logSessionTokenExpiredAndQuit,
} from "../helpers/errors.ts";
import { getApiUrl } from "../helpers/urls.ts";
import { formatDate } from "../helpers/format-date.ts";

export const TOKEN_EXPIRATION_SECONDS = {
IN_7_DAYS: 7 * 24 * 60 * 60,
Expand Down Expand Up @@ -138,7 +137,6 @@ async function createTokenAction() {
// display token to user
const data = await response.json();
loadingSpinner.succeed(gray("Access token created 🎉"));
// @ts-ignore: Deno has narrower types for fetch responses, but we know this code works atm.
console.log(`${green(data.token)}\n`);

// tell them they will set this in the Authorization header
Expand Down Expand Up @@ -170,13 +168,11 @@ async function createTokenAction() {
console.log("\n");

// tip user on other commands
const base = getCommandBase();

const table = new Table({
colWidths: [20, 30],
});
table.push(["View All Tokens", magenta(`${base} tokens list`)]);
table.push(["Delete a Token", magenta(`${base} tokens delete`)]);
table.push(["View All Tokens", magenta("sf tokens list")]);
table.push(["Delete a Token", magenta("sf tokens delete")]);

console.log(`${gray("And other commands you can try:")}`);
console.log(table.toString());
Expand Down Expand Up @@ -232,11 +228,10 @@ async function listTokensAction() {
console.log(`${table.toString()}\n`);

// prompt user that they can generate one
const base = getCommandBase();
console.log(
`${gray("Generate your first token with: ")}${
magenta(
`${base} tokens create`,
`sf tokens create`,
)
}`,
);
Expand All @@ -249,30 +244,22 @@ async function listTokensAction() {
head: [
gray("Token ID"),
gray("Name"),
// gray("Last active"),
gray("Expires"),
],
colWidths: [40, 15, 25, 25],
colWidths: [40, 15, 25],
});
for (const token of tokens) {
tokensTable.push([
gray(token.id),
cyan(token.id),
token.name ? token.name : gray("(empty)"),
// green(formatDate(token.last_active_at)),
white(formatDate(token.expires_at)),
white(formatDate(new Date(token.expires_at))),
]);
}
console.log(tokensTable.toString());

process.exit(0);
}

function formatDate(isoString: string): string {
return dayjs(isoString).format("MMM D, YYYY [at] h:mma").toLowerCase();
}

// --

async function deleteTokenAction({
id,
yes,
Expand Down