Skip to content

Commit

Permalink
[TRI-1006] New @trigger.dev/cli whoami command. (#316)
Browse files Browse the repository at this point in the history
* 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 <matt@mattaitken.com>
  • Loading branch information
neo773 and matt-aitken authored Aug 18, 2023
1 parent e20fa3c commit bbaa6ba
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
3 changes: 3 additions & 0 deletions examples/nextjs-12/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compilerOptions": {}
}
3 changes: 2 additions & 1 deletion examples/nextjs-12/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"devDependencies": {
"eslint": "8.44.0",
"eslint-config-next": "12.3.4"
"eslint-config-next": "12.3.4",
"@trigger.dev/cli": "workspace:*"
},
"trigger.dev": {
"endpointId": "nextjs-12"
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import inquirer from "inquirer";
import pathModule from "node:path";
import { createIntegrationCommand } from "../commands/createIntegration";
import { devCommand } from "../commands/dev";
import { whoamiCommand } from "../commands/whoami.js";
import { initCommand } from "../commands/init";
import { CLOUD_TRIGGER_URL, COMMAND_NAME } from "../consts";
import { telemetryClient } from "../telemetry/telemetry";
Expand Down Expand Up @@ -78,6 +79,21 @@ program
await createIntegrationCommand(path, options);
});

program
.command("whoami")
.description("display the current logged in user and project details")
.argument("[path]", "The path to the project", ".")
.option("-p, --port <port>", "The local port your server is on", "3000")
.option("-e, --env-file <name>", "The name of the env file to load", ".env.local")
.version(getVersion(), "-v, --version", "Display the version number")
.action(async (path, options) => {
try {
await whoamiCommand(path, options);
} catch (e) {
throw e;
}
});

export const promptTriggerUrl = async (): Promise<string> => {
const { instanceType } = await inquirer.prompt<{
instanceType: "cloud" | "self-hosted";
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export async function devCommand(path: string, anyOptions: any) {
throttle(refresh, throttleTimeMs);
}

async function getEndpointIdFromPackageJson(path: string, options: DevCommandOptions) {
export async function getEndpointIdFromPackageJson(path: string, options: DevCommandOptions) {
if (options.clientId) {
return options.clientId;
}
Expand All @@ -221,7 +221,7 @@ async function getEndpointIdFromPackageJson(path: string, options: DevCommandOpt
return value as string;
}

async function readEnvFilesWithBackups(
export async function readEnvFilesWithBackups(
path: string,
envFile: string,
backups: string[]
Expand Down Expand Up @@ -249,7 +249,7 @@ async function readEnvFilesWithBackups(
return;
}

async function getTriggerApiDetails(path: string, envFile: string) {
export async function getTriggerApiDetails(path: string, envFile: string) {
const resolvedEnvFile = await readEnvFilesWithBackups(path, envFile, [
".env",
".env.local",
Expand Down
62 changes: 62 additions & 0 deletions packages/cli/src/commands/whoami.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { z } from "zod";
import { logger } from "../utils/logger.js";
import { resolvePath } from "../utils/parseNameAndPath.js";
import { TriggerApi } from "../utils/triggerApi.js";
import { DevCommandOptions, getEndpointIdFromPackageJson, getTriggerApiDetails } from "./dev.js";
import ora from "ora";

export const WhoAmICommandOptionsSchema = z.object({
envFile: z.string()
});

export type WhoAmICommandOptions = z.infer<typeof WhoAmICommandOptionsSchema>;

export async function whoamiCommand(path: string, anyOptions: any) {
const loadingSpinner = ora(`Hold while we fetch your data`);
loadingSpinner.start();

const result = WhoAmICommandOptionsSchema.safeParse(anyOptions);
if (!result.success) {
logger.error(result.error.message);
return;
}
const options = result.data;

const resolvedPath = resolvePath(path);

// Read from package.json to get the endpointId
const endpointId = await getEndpointIdFromPackageJson(resolvedPath, options as DevCommandOptions);
if (!endpointId) {
logger.error(
"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"
);
loadingSpinner.stop();
return;
}
// Read from .env.local or .env to get the TRIGGER_API_KEY and TRIGGER_API_URL
const apiDetails = await getTriggerApiDetails(resolvedPath, options.envFile);

if (!apiDetails) {
return;
}

const triggerAPI = new TriggerApi(apiDetails.apiKey, apiDetails.apiUrl);
const userData = await triggerAPI.whoami(apiDetails.apiKey);

loadingSpinner.stop();

logger.info(`
environment: ${userData?.type}
Trigger Client Id: ${endpointId}
User ID: ${userData?.userId}
Project:
id: ${userData?.project.id}
slug: ${userData?.project.slug}
name: ${userData?.project.name}
Organization:
id: ${userData?.organization.id}
slug: ${userData?.organization.slug}
title: ${userData?.organization.title}
`);
process.exit(1);
}

0 comments on commit bbaa6ba

Please sign in to comment.