Skip to content

Commit

Permalink
(feature): Implement fern generate --preview (#3363)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Apr 12, 2024
1 parent fe417a5 commit f1a0ecc
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 83 deletions.
20 changes: 10 additions & 10 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
2 changes: 1 addition & 1 deletion packages/cli/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@fern-api/yaml-formatter": "workspace:*",
"@fern-api/yaml-migrations": "workspace:*",
"@fern-api/yaml-schema": "workspace:*",
"@fern-fern/fiddle-sdk": "^0.0.503",
"@fern-fern/fiddle-sdk": "^0.0.508",
"ansi-escapes": "^5.0.0",
"axios": "^0.28.0",
"boxen": "^7.0.0",
Expand Down
46 changes: 26 additions & 20 deletions packages/cli/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
generatorsYml,
fernConfigJson,
generatorsYml,
GENERATORS_CONFIGURATION_FILENAME,
getFernDirectory,
PROJECT_CONFIG_FILENAME
Expand Down Expand Up @@ -352,8 +352,12 @@ function addGenerateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
async (argv) => {
if (argv.api != null && argv.docs != null) {
return cliContext.failWithoutThrowing("Cannot specify both --api and --docs. Please choose one.");
} else if (argv.api != null) {
await generateAPIWorkspaces({
}
if (argv.local && argv.preview) {
return cliContext.failWithoutThrowing("The --local flag is incompatible with --preview.");
}
if (argv.api != null) {
return await generateAPIWorkspaces({
project: await loadProjectAndRegisterWorkspacesWithContext(cliContext, {
commandLineApiWorkspace: argv.api,
defaultToAllApiWorkspaces: false
Expand All @@ -363,16 +367,18 @@ function addGenerateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
groupName: argv.group,
shouldLogS3Url: argv.printZipUrl,
keepDocker: argv.keepDocker,
useLocalDocker: argv.local
useLocalDocker: argv.local,
preview: argv.preview
});
} else if (argv.docs != null) {
}
if (argv.docs != null) {
if (argv.group != null) {
cliContext.logger.warn("--group is ignored when generating docs");
}
if (argv.version != null) {
cliContext.logger.warn("--version is ignored when generating docs");
}
await generateDocsWorkspace({
return await generateDocsWorkspace({
project: await loadProjectAndRegisterWorkspacesWithContext(
cliContext,
{
Expand All @@ -385,21 +391,21 @@ function addGenerateCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext)
instance: argv.instance,
preview: argv.preview
});
} else {
// default to loading api workspace to preserve legacy behavior
await generateAPIWorkspaces({
project: await loadProjectAndRegisterWorkspacesWithContext(cliContext, {
commandLineApiWorkspace: argv.api,
defaultToAllApiWorkspaces: false
}),
cliContext,
version: argv.version,
groupName: argv.group,
shouldLogS3Url: argv.printZipUrl,
keepDocker: argv.keepDocker,
useLocalDocker: argv.local
});
}
// default to loading api workspace to preserve legacy behavior
return await generateAPIWorkspaces({
project: await loadProjectAndRegisterWorkspacesWithContext(cliContext, {
commandLineApiWorkspace: argv.api,
defaultToAllApiWorkspaces: false
}),
cliContext,
version: argv.version,
groupName: argv.group,
shouldLogS3Url: argv.printZipUrl,
keepDocker: argv.keepDocker,
useLocalDocker: argv.local,
preview: argv.preview
});
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
fernConfigJson,
GENERATORS_CONFIGURATION_FILENAME
} from "@fern-api/configuration";
import { AbsoluteFilePath } from "@fern-api/fs-utils";
import { runLocalGenerationForWorkspace } from "@fern-api/local-workspace-runner";
import { runRemoteGenerationForAPIWorkspace } from "@fern-api/remote-workspace-runner";
import { TaskContext } from "@fern-api/task-context";
Expand All @@ -21,7 +22,8 @@ export async function generateWorkspace({
shouldLogS3Url,
token,
useLocalDocker,
keepDocker
keepDocker,
absolutePathToPreview
}: {
organization: string;
workspace: FernWorkspace;
Expand All @@ -33,6 +35,7 @@ export async function generateWorkspace({
token: FernToken;
useLocalDocker: boolean;
keepDocker: boolean;
absolutePathToPreview: AbsoluteFilePath | undefined;
}): Promise<void> {
if (workspace.generatorsConfiguration == null) {
context.logger.warn("This workspaces has no generators.yml");
Expand Down Expand Up @@ -77,7 +80,8 @@ export async function generateWorkspace({
version,
shouldLogS3Url,
token,
whitelabel: workspace.generatorsConfiguration.whitelabel
whitelabel: workspace.generatorsConfiguration.whitelabel,
absolutePathToPreview
});
}
}
17 changes: 15 additions & 2 deletions packages/cli/cli/src/commands/generate/generateAPIWorkspaces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createOrganizationIfDoesNotExist } from "@fern-api/auth";
import { join, RelativeFilePath } from "@fern-api/fs-utils";
import { askToLogin } from "@fern-api/login";
import { Project } from "@fern-api/project-loader";
import { convertOpenApiWorkspaceToFernWorkspace, FernWorkspace } from "@fern-api/workspace-loader";
import { CliContext } from "../../cli-context/CliContext";
import { PREVIEW_DIRECTORY } from "../../constants";
import { generateWorkspace } from "./generateAPIWorkspace";

export async function generateAPIWorkspaces({
Expand All @@ -12,7 +14,8 @@ export async function generateAPIWorkspaces({
groupName,
shouldLogS3Url,
keepDocker,
useLocalDocker
useLocalDocker,
preview
}: {
project: Project;
cliContext: CliContext;
Expand All @@ -21,6 +24,7 @@ export async function generateAPIWorkspaces({
shouldLogS3Url: boolean;
useLocalDocker: boolean;
keepDocker: boolean;
preview: boolean;
}): Promise<void> {
const token = await cliContext.runTask(async (context) => {
return askToLogin(context);
Expand Down Expand Up @@ -69,6 +73,14 @@ export async function generateAPIWorkspaces({
? workspace
: await convertOpenApiWorkspaceToFernWorkspace(workspace, context);

const absolutePathToPreview = preview
? join(fernWorkspace.absoluteFilepath, RelativeFilePath.of(PREVIEW_DIRECTORY))
: undefined;

if (absolutePathToPreview != null) {
context.logger.info(`Writing preview to ${absolutePathToPreview}`);
}

await generateWorkspace({
organization: project.config.organization,
workspace: fernWorkspace,
Expand All @@ -79,7 +91,8 @@ export async function generateAPIWorkspaces({
shouldLogS3Url,
token,
useLocalDocker,
keepDocker
keepDocker,
absolutePathToPreview
});
});
})
Expand Down
1 change: 1 addition & 0 deletions packages/cli/cli/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const API_CLI_OPTION = "api";
export const GROUP_CLI_OPTION = "group";
export const TOKEN_STDIN_OPTION = "token-stdin";
export const PREVIEW_DIRECTORY = ".preview";
2 changes: 1 addition & 1 deletion packages/cli/configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@fern-api/fdr-sdk": "0.73.1-9d67ec828",
"@fern-api/fs-utils": "workspace:*",
"@fern-api/task-context": "workspace:*",
"@fern-fern/fiddle-sdk": "^0.0.503",
"@fern-fern/fiddle-sdk": "^0.0.508",
"find-up": "^6.3.0",
"immer": "^9.0.15",
"js-yaml": "^4.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@fern-api/logging-execa": "workspace:*",
"@fern-api/task-context": "workspace:*",
"@fern-api/workspace-loader": "workspace:*",
"@fern-fern/fiddle-sdk": "^0.0.503",
"@fern-fern/fiddle-sdk": "^0.0.508",
"@fern-fern/generator-exec-sdk": "0.0.687",
"chalk": "^5.0.1",
"decompress": "^4.2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@fern-api/register": "workspace:*",
"@fern-api/task-context": "workspace:*",
"@fern-api/workspace-loader": "workspace:*",
"@fern-fern/fiddle-sdk": "^0.0.503",
"@fern-fern/fiddle-sdk": "^0.0.508",
"axios": "^0.28.0",
"chalk": "^5.0.1",
"decompress": "^4.2.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generatorsYml } from "@fern-api/configuration";
import { noop } from "@fern-api/core-utils";
import { AbsoluteFilePath, doesPathExist } from "@fern-api/fs-utils";
import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils";
import { LogLevel } from "@fern-api/logger";
import { InteractiveTaskContext } from "@fern-api/task-context";
import { FernFiddle } from "@fern-fern/fiddle-sdk";
Expand All @@ -20,6 +20,7 @@ export declare namespace RemoteTaskHandler {
taskId: FernFiddle.remoteGen.RemoteGenTaskId;
interactiveTaskContext: InteractiveTaskContext;
generatorInvocation: generatorsYml.GeneratorInvocation;
absolutePathToPreview: AbsoluteFilePath | undefined;
}
export interface Response {
createdSnippets: boolean;
Expand All @@ -30,11 +31,13 @@ export declare namespace RemoteTaskHandler {
export class RemoteTaskHandler {
private context: InteractiveTaskContext;
private generatorInvocation: generatorsYml.GeneratorInvocation;
private absolutePathToPreview: AbsoluteFilePath | undefined;
private lengthOfLastLogs = 0;

constructor({ interactiveTaskContext, generatorInvocation }: RemoteTaskHandler.Init) {
constructor({ interactiveTaskContext, generatorInvocation, absolutePathToPreview }: RemoteTaskHandler.Init) {
this.context = interactiveTaskContext;
this.generatorInvocation = generatorInvocation;
this.absolutePathToPreview = absolutePathToPreview;
}

public async processUpdate(
Expand All @@ -54,15 +57,17 @@ export class RemoteTaskHandler {
});
});

this.context.setSubtitle(
coordinates.length > 0
? coordinates
.map((coordinate) => {
return `◦ ${coordinate}`;
})
.join("\n")
: undefined
);
if (this.absolutePathToPreview == null) {
this.context.setSubtitle(
coordinates.length > 0
? coordinates
.map((coordinate) => {
return `◦ ${coordinate}`;
})
.join("\n")
: undefined
);
}

for (const newLog of remoteTask.logs.slice(this.lengthOfLastLogs)) {
this.context.logger.log(convertLogLevel(newLog.level), newLog.message);
Expand All @@ -89,16 +94,19 @@ export class RemoteTaskHandler {
finished: async (finishedStatus) => {
if (finishedStatus.s3PreSignedReadUrlV2 != null) {
logS3Url(finishedStatus.s3PreSignedReadUrlV2);
if (this.generatorInvocation.absolutePathToLocalOutput != null) {
const absolutePathToLocalOutput = this.getAbsolutePathToLocalOutput();
if (absolutePathToLocalOutput != null) {
await downloadFilesForTask({
s3PreSignedReadUrl: finishedStatus.s3PreSignedReadUrlV2,
absolutePathToLocalOutput: this.generatorInvocation.absolutePathToLocalOutput,
absolutePathToLocalOutput,
context: this.context
});
}
}
for (const coordinate of coordinates) {
this.context.logger.info(`Published ${coordinate}`);
if (this.absolutePathToPreview == null) {
for (const coordinate of coordinates) {
this.context.logger.info(`Published ${coordinate}`);
}
}
this.#isFinished = true;
this.#createdSnippets = finishedStatus.createdSnippets != null ? finishedStatus.createdSnippets : false;
Expand All @@ -117,6 +125,12 @@ export class RemoteTaskHandler {
: undefined;
}

private getAbsolutePathToLocalOutput(): AbsoluteFilePath | undefined {
return this.absolutePathToPreview != null
? join(this.absolutePathToPreview, RelativeFilePath.of(path.basename(this.generatorInvocation.name)))
: this.generatorInvocation.absolutePathToLocalOutput;
}

#isFinished = false;
public get isFinished(): boolean {
return this.#isFinished;
Expand Down
Loading

0 comments on commit f1a0ecc

Please sign in to comment.