Skip to content

Commit bbaa6ba

Browse files
neo773matt-aitken
andauthored
[TRI-1006] New @trigger.dev/cli whoami command. (#316)
* FIx: [TRI-1006] New @trigger.dev/cli whoami command. * revert dev.ts zod schema to original * Removed telemetry * remove all telemetry * Made the clientId optional again --------- Co-authored-by: Matt Aitken <[email protected]>
1 parent e20fa3c commit bbaa6ba

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

examples/nextjs-12/jsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"compilerOptions": {}
3+
}

examples/nextjs-12/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
},
1818
"devDependencies": {
1919
"eslint": "8.44.0",
20-
"eslint-config-next": "12.3.4"
20+
"eslint-config-next": "12.3.4",
21+
"@trigger.dev/cli": "workspace:*"
2122
},
2223
"trigger.dev": {
2324
"endpointId": "nextjs-12"

packages/cli/src/cli/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import inquirer from "inquirer";
33
import pathModule from "node:path";
44
import { createIntegrationCommand } from "../commands/createIntegration";
55
import { devCommand } from "../commands/dev";
6+
import { whoamiCommand } from "../commands/whoami.js";
67
import { initCommand } from "../commands/init";
78
import { CLOUD_TRIGGER_URL, COMMAND_NAME } from "../consts";
89
import { telemetryClient } from "../telemetry/telemetry";
@@ -78,6 +79,21 @@ program
7879
await createIntegrationCommand(path, options);
7980
});
8081

82+
program
83+
.command("whoami")
84+
.description("display the current logged in user and project details")
85+
.argument("[path]", "The path to the project", ".")
86+
.option("-p, --port <port>", "The local port your server is on", "3000")
87+
.option("-e, --env-file <name>", "The name of the env file to load", ".env.local")
88+
.version(getVersion(), "-v, --version", "Display the version number")
89+
.action(async (path, options) => {
90+
try {
91+
await whoamiCommand(path, options);
92+
} catch (e) {
93+
throw e;
94+
}
95+
});
96+
8197
export const promptTriggerUrl = async (): Promise<string> => {
8298
const { instanceType } = await inquirer.prompt<{
8399
instanceType: "cloud" | "self-hosted";

packages/cli/src/commands/dev.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export async function devCommand(path: string, anyOptions: any) {
206206
throttle(refresh, throttleTimeMs);
207207
}
208208

209-
async function getEndpointIdFromPackageJson(path: string, options: DevCommandOptions) {
209+
export async function getEndpointIdFromPackageJson(path: string, options: DevCommandOptions) {
210210
if (options.clientId) {
211211
return options.clientId;
212212
}
@@ -221,7 +221,7 @@ async function getEndpointIdFromPackageJson(path: string, options: DevCommandOpt
221221
return value as string;
222222
}
223223

224-
async function readEnvFilesWithBackups(
224+
export async function readEnvFilesWithBackups(
225225
path: string,
226226
envFile: string,
227227
backups: string[]
@@ -249,7 +249,7 @@ async function readEnvFilesWithBackups(
249249
return;
250250
}
251251

252-
async function getTriggerApiDetails(path: string, envFile: string) {
252+
export async function getTriggerApiDetails(path: string, envFile: string) {
253253
const resolvedEnvFile = await readEnvFilesWithBackups(path, envFile, [
254254
".env",
255255
".env.local",

packages/cli/src/commands/whoami.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { z } from "zod";
2+
import { logger } from "../utils/logger.js";
3+
import { resolvePath } from "../utils/parseNameAndPath.js";
4+
import { TriggerApi } from "../utils/triggerApi.js";
5+
import { DevCommandOptions, getEndpointIdFromPackageJson, getTriggerApiDetails } from "./dev.js";
6+
import ora from "ora";
7+
8+
export const WhoAmICommandOptionsSchema = z.object({
9+
envFile: z.string()
10+
});
11+
12+
export type WhoAmICommandOptions = z.infer<typeof WhoAmICommandOptionsSchema>;
13+
14+
export async function whoamiCommand(path: string, anyOptions: any) {
15+
const loadingSpinner = ora(`Hold while we fetch your data`);
16+
loadingSpinner.start();
17+
18+
const result = WhoAmICommandOptionsSchema.safeParse(anyOptions);
19+
if (!result.success) {
20+
logger.error(result.error.message);
21+
return;
22+
}
23+
const options = result.data;
24+
25+
const resolvedPath = resolvePath(path);
26+
27+
// Read from package.json to get the endpointId
28+
const endpointId = await getEndpointIdFromPackageJson(resolvedPath, options as DevCommandOptions);
29+
if (!endpointId) {
30+
logger.error(
31+
"You must run the `init` command first to setup the project – you are missing \n'trigger.dev': { 'endpointId': 'your-client-id' } from your package.json file, or pass in the --client-id option to this command"
32+
);
33+
loadingSpinner.stop();
34+
return;
35+
}
36+
// Read from .env.local or .env to get the TRIGGER_API_KEY and TRIGGER_API_URL
37+
const apiDetails = await getTriggerApiDetails(resolvedPath, options.envFile);
38+
39+
if (!apiDetails) {
40+
return;
41+
}
42+
43+
const triggerAPI = new TriggerApi(apiDetails.apiKey, apiDetails.apiUrl);
44+
const userData = await triggerAPI.whoami(apiDetails.apiKey);
45+
46+
loadingSpinner.stop();
47+
48+
logger.info(`
49+
environment: ${userData?.type}
50+
Trigger Client Id: ${endpointId}
51+
User ID: ${userData?.userId}
52+
Project:
53+
id: ${userData?.project.id}
54+
slug: ${userData?.project.slug}
55+
name: ${userData?.project.name}
56+
Organization:
57+
id: ${userData?.organization.id}
58+
slug: ${userData?.organization.slug}
59+
title: ${userData?.organization.title}
60+
`);
61+
process.exit(1);
62+
}

0 commit comments

Comments
 (0)