Skip to content

Commit 8694e57

Browse files
committed
Fix CLI logout and add list-profiles command
1 parent b271742 commit 8694e57

File tree

5 files changed

+100
-14
lines changed

5 files changed

+100
-14
lines changed

.changeset/smart-olives-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trigger.dev": patch
3+
---
4+
5+
Fix CLI logout and add list-profiles command

packages/cli-v3/src/cli/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { configureLogoutCommand } from "../commands/logout.js";
77
import { configureWhoamiCommand } from "../commands/whoami.js";
88
import { COMMAND_NAME } from "../consts.js";
99
import { getVersion } from "../utilities/getVersion.js";
10+
import { configureListProfilesCommand } from "../commands/list-profiles.js";
1011

1112
export const program = new Command();
1213

@@ -21,3 +22,4 @@ configureDevCommand(program);
2122
configureDeployCommand(program);
2223
configureWhoamiCommand(program);
2324
configureLogoutCommand(program);
25+
configureListProfilesCommand(program);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Command } from "commander";
2+
import {
3+
deleteAuthConfigProfile,
4+
readAuthConfigFile,
5+
readAuthConfigProfile,
6+
writeAuthConfigProfile,
7+
} from "../utilities/configFiles.js";
8+
import { logger } from "../utilities/logger.js";
9+
import {
10+
CommonCommandOptions,
11+
commonOptions,
12+
handleTelemetry,
13+
wrapCommandAction,
14+
} from "../cli/common.js";
15+
import { printInitialBanner } from "../utilities/initialBanner.js";
16+
import { z } from "zod";
17+
import { chalkGrey } from "../utilities/cliOutput.js";
18+
import { log, outro, text } from "@clack/prompts";
19+
20+
const ListProfilesOptions = CommonCommandOptions;
21+
22+
type ListProfilesOptions = z.infer<typeof ListProfilesOptions>;
23+
24+
export function configureListProfilesCommand(program: Command) {
25+
return program
26+
.command("list-profiles")
27+
.description("List all of your CLI profiles")
28+
.option(
29+
"-l, --log-level <level>",
30+
"The CLI log level to use (debug, info, log, warn, error, none). This does not effect the log level of your trigger.dev tasks.",
31+
"log"
32+
)
33+
.option("--skip-telemetry", "Opt-out of sending telemetry")
34+
.action(async (options) => {
35+
await handleTelemetry(async () => {
36+
await printInitialBanner(true);
37+
await listProfilesCommand(options);
38+
});
39+
});
40+
}
41+
42+
export async function listProfilesCommand(options: unknown) {
43+
return await wrapCommandAction("listProfiles", ListProfilesOptions, options, async (opts) => {
44+
return await listProfiles(opts);
45+
});
46+
}
47+
48+
export async function listProfiles(options: ListProfilesOptions) {
49+
const authConfig = readAuthConfigFile();
50+
51+
if (!authConfig) {
52+
logger.info("No profiles found");
53+
return;
54+
}
55+
56+
const profiles = Object.keys(authConfig);
57+
58+
log.message("Profiles:");
59+
60+
for (const profile of profiles) {
61+
const profileConfig = authConfig[profile];
62+
63+
log.info(`${profile}${profileConfig?.apiUrl ? ` - ${chalkGrey(profileConfig.apiUrl)}` : ""}`);
64+
}
65+
66+
outro("Retrieve account info by running whoami --profile <profile>");
67+
}
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
import { Command } from "commander";
2-
import { readAuthConfigProfile, writeAuthConfigProfile } from "../utilities/configFiles.js";
3-
import { logger } from "../utilities/logger.js";
4-
import { CommonCommandOptions, commonOptions, handleTelemetry, wrapCommandAction } from "../cli/common.js";
5-
import { printInitialBanner } from "../utilities/initialBanner.js";
62
import { z } from "zod";
3+
import {
4+
CommonCommandOptions,
5+
commonOptions,
6+
handleTelemetry,
7+
wrapCommandAction,
8+
} from "../cli/common.js";
9+
import { deleteAuthConfigProfile, readAuthConfigProfile } from "../utilities/configFiles.js";
10+
import { printInitialBanner } from "../utilities/initialBanner.js";
11+
import { logger } from "../utilities/logger.js";
712

813
const LogoutCommandOptions = CommonCommandOptions;
914

1015
type LogoutCommandOptions = z.infer<typeof LogoutCommandOptions>;
1116

1217
export function configureLogoutCommand(program: Command) {
13-
return commonOptions(program
14-
.command("logout")
15-
.description("Logout of Trigger.dev"))
16-
.action(async (options) => {
18+
return commonOptions(program.command("logout").description("Logout of Trigger.dev")).action(
19+
async (options) => {
1720
await handleTelemetry(async () => {
1821
await printInitialBanner(false);
1922
await logoutCommand(options);
2023
});
21-
});
24+
}
25+
);
2226
}
2327

2428
export async function logoutCommand(options: unknown) {
@@ -31,11 +35,11 @@ export async function logout(options: LogoutCommandOptions) {
3135
const config = readAuthConfigProfile(options.profile);
3236

3337
if (!config?.accessToken) {
34-
logger.info(`You are already logged out [${options.profile ?? "default"}]`);
38+
logger.info(`You are already logged out [${options.profile}]`);
3539
return;
3640
}
3741

38-
writeAuthConfigProfile({ ...config, accessToken: undefined, apiUrl: undefined }, options.profile);
42+
deleteAuthConfigProfile(options.profile);
3943

40-
logger.info(`Logged out of Trigger.dev [${options.profile ?? "default"}]`);
44+
logger.info(`Logged out of Trigger.dev [${options.profile}]`);
4145
}

packages/cli-v3/src/utilities/configFiles.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ export function readAuthConfigProfile(profile: string = "default"): UserAuthConf
5656
}
5757
}
5858

59-
function readAuthConfigFile(): UserAuthConfigFile | undefined {
59+
export function deleteAuthConfigProfile(profile: string = "default") {
60+
const existingConfig = readAuthConfigFile() || {};
61+
62+
delete existingConfig[profile];
63+
64+
writeAuthConfigFile(existingConfig);
65+
}
66+
67+
export function readAuthConfigFile(): UserAuthConfigFile | undefined {
6068
try {
6169
const authConfigFilePath = getAuthConfigFilePath();
6270

@@ -71,7 +79,7 @@ function readAuthConfigFile(): UserAuthConfigFile | undefined {
7179
}
7280
}
7381

74-
function writeAuthConfigFile(config: UserAuthConfigFile) {
82+
export function writeAuthConfigFile(config: UserAuthConfigFile) {
7583
const authConfigFilePath = getAuthConfigFilePath();
7684
mkdirSync(path.dirname(authConfigFilePath), {
7785
recursive: true,

0 commit comments

Comments
 (0)