Skip to content

Commit

Permalink
Add support for anonymous usage of the generate CLI (#4239)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomdk authored Aug 9, 2024
1 parent ab1ea1a commit 3e50297
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function generateWorkspace({
version: string | undefined;
groupName: string | undefined;
shouldLogS3Url: boolean;
token: FernToken;
token: FernToken | undefined;
useLocalDocker: boolean;
keepDocker: boolean;
absolutePathToPreview: AbsoluteFilePath | undefined;
Expand Down Expand Up @@ -79,6 +79,9 @@ export async function generateWorkspace({
context
});
} else {
if (!token) {
return context.failAndThrow("Please run fern login");
}
await runRemoteGenerationForAPIWorkspace({
projectConfig,
organization,
Expand Down
26 changes: 15 additions & 11 deletions packages/cli/cli/src/commands/generate/generateAPIWorkspaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createOrganizationIfDoesNotExist } from "@fern-api/auth";
import { createOrganizationIfDoesNotExist, FernToken, FernUserToken } from "@fern-api/auth";
import { Values } from "@fern-api/core-utils";
import { join, RelativeFilePath } from "@fern-api/fs-utils";
import { askToLogin } from "@fern-api/login";
Expand Down Expand Up @@ -34,18 +34,22 @@ export async function generateAPIWorkspaces({
preview: boolean;
mode: GenerationMode | undefined;
}): Promise<void> {
const token = await cliContext.runTask(async (context) => {
return askToLogin(context);
});
let token: FernToken | undefined = undefined;

if (token.type === "user") {
await cliContext.runTask(async (context) => {
await createOrganizationIfDoesNotExist({
organization: project.config.organization,
token,
context
});
if (!useLocalDocker) {
const currentToken = await cliContext.runTask(async (context) => {
return askToLogin(context);
});
if (currentToken.type === "user") {
await cliContext.runTask(async (context) => {
await createOrganizationIfDoesNotExist({
organization: project.config.organization,
token: currentToken,
context
});
});
}
token = currentToken;
}

cliContext.instrumentPostHogEvent({
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/posthog-manager/src/getPosthogManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export async function getPosthogManager(): Promise<PosthogManager> {
async function createPosthogManager(): Promise<PosthogManager> {
try {
const posthogApiKey = process.env.POSTHOG_API_KEY;
if (posthogApiKey == null) {
const disableTelemetry = process.env.FERN_DISABLE_TELEMETRY === "true";
if (posthogApiKey == null || disableTelemetry) {
return new NoopPosthogManager();
}
const userToken = await getUserToken();
Expand Down

0 comments on commit 3e50297

Please sign in to comment.