Skip to content

Commit

Permalink
fix(cli): improve error messages for node 17 or below incompatibility (
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Oct 25, 2024
1 parent de21672 commit 50bf6cb
Show file tree
Hide file tree
Showing 172 changed files with 321 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export declare namespace Fetcher {
withCredentials?: boolean;
abortSignal?: AbortSignal;
requestType?: "json" | "file" | "bytes";
responseType?: "json" | "blob" | "sse" | "streaming" | "text";
responseType?: "json" | "blob" | "sse" | "streaming" | "text" | "arrayBuffer";
duplex?: "half";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { chooseStreamWrapper } from "./stream-wrappers/chooseStreamWrapper";
export async function getResponseBody(response: Response, responseType?: string): Promise<unknown> {
if (response.body != null && responseType === "blob") {
return await response.blob();
} else if (response.body != null && responseType === "arrayBuffer") {
return await response.arrayBuffer();
} else if (response.body != null && responseType === "sse") {
return response.body;
} else if (response.body != null && responseType === "streaming") {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"@fern-api/workspace-loader": "workspace:*",
"@fern-fern/fiddle-sdk": "0.0.584",
"@fern-fern/generators-sdk": "0.114.0-5745f9e74",
"@fern-typescript/fetcher": "workspace:*",
"@types/axios": "^0.14.0",
"@types/boxen": "^3.0.1",
"@types/get-port": "^4.2.0",
Expand Down Expand Up @@ -113,5 +114,5 @@
"vitest": "^2.0.5",
"yaml": "^2.4.5",
"yargs": "^17.4.1"
}
}
}
8 changes: 7 additions & 1 deletion packages/cli/cli/src/cli-context/CliContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export class CliContext {
return context;
}

private readonly USE_NODE_18_OR_ABOVE_MESSAGE = "The Fern CLI requires Node 18+ or above.";
private async runTaskWithInit<T>(
init: TaskContextImpl.Init,
run: (context: TaskContext) => T | Promise<T>
Expand All @@ -183,7 +184,12 @@ export class CliContext {
try {
result = await run(context);
} catch (error) {
context.failWithoutThrowing(undefined, error);
if ((error as Error).message.includes("globalThis")) {
context.logger.error(this.USE_NODE_18_OR_ABOVE_MESSAGE);
context.failWithoutThrowing();
} else {
context.failWithoutThrowing(undefined, error);
}
throw new FernCliError();
} finally {
context.finish();
Expand Down
17 changes: 13 additions & 4 deletions packages/cli/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,25 @@ import { rerunFernCliAtVersion } from "./rerunFernCliAtVersion";
import { isURL } from "./utils/isUrl";
import { generateJsonschemaForWorkspaces } from "./commands/jsonschema/generateJsonschemaForWorkspace";
import { generateDynamicIrForWorkspaces } from "./commands/generate-dynamic-ir/generateDynamicIrForWorkspaces";
import { setGlobalDispatcher, Agent } from "undici";
import { writeDocsDefinitionForProject } from "./commands/write-docs-definition/writeDocsDefinitionForProject";

setGlobalDispatcher(new Agent({ connect: { timeout: 5_000 } }));
import { RUNTIME } from "@fern-typescript/fetcher";

void runCli();

const USE_NODE_18_OR_ABOVE_MESSAGE = "The Fern CLI requires Node 18+ or above.";

async function runCli() {
const cliContext = new CliContext(process.stdout);

const exit = async () => {
await cliContext.exit();
};

if (RUNTIME.type === "node" && RUNTIME.parsedVersion != null && RUNTIME.parsedVersion >= 18) {
const { setGlobalDispatcher, Agent } = await import("undici");
setGlobalDispatcher(new Agent({ connect: { timeout: 5_000 } }));
}

// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.on("SIGINT", async () => {
cliContext.suppressUpgradeMessage();
Expand Down Expand Up @@ -85,7 +91,10 @@ async function runCli() {
error
}
});
if (error instanceof FernCliError) {
if ((error as Error)?.message.includes("globalThis")) {
cliContext.logger.error(USE_NODE_18_OR_ABOVE_MESSAGE);
cliContext.failWithoutThrowing();
} else if (error instanceof FernCliError) {
// thrower is responsible for logging, so we generally don't need to log here.
cliContext.failWithoutThrowing();
} else if (error instanceof LoggableFernCliError) {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
{ "path": "../cli-migrations" },
{ "path": "../fern-definition/schema" },
{ "path": "../fern-definition/ir-to-jsonschema" },
{ "path": "../docs-resolver" }
{ "path": "../docs-resolver" },
{ "path": "../../../generators/typescript/utils/commons"}
]
}
6 changes: 6 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
- changelogEntry:
- summary: |
`fern docs dev` now runs in Node 16 - Node 22 environments.
type: fix
irVersion: 53
version: 0.45.0-rc24

- changelogEntry:
- summary: |
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/docs-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"@fern-api/project-loader": "workspace:*",
"@fern-api/register": "workspace:*",
"@fern-api/task-context": "workspace:*",
"@fern-typescript/fetcher": "workspace:*",
"chalk": "^5.3.0",
"cors": "^2.8.5",
"decompress": "^4.2.1",
"express": "^4.20.0",
"readable-stream": "^4.5.2",
"tmp-promise": "^3.0.3",
"uuid": "^9.0.1",
"watcher": "^2.3.1",
Expand All @@ -51,6 +53,7 @@
"@types/decompress": "^4.2.7",
"@types/express": "^4.17.21",
"@types/node": "18.7.18",
"@types/readable-stream": "^4.0.15",
"@types/uuid": "^9.0.8",
"@types/ws": "^8.5.10",
"@types/xml2js": "^0.4.14",
Expand All @@ -62,4 +65,4 @@
"typescript": "4.6.4",
"vitest": "^2.0.5"
}
}
}
32 changes: 25 additions & 7 deletions packages/cli/docs-preview/src/downloadLocalDocsBundle.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils";
import { Logger } from "@fern-api/logger";
import decompress from "decompress";
import { createWriteStream } from "fs";
import { Readable } from "readable-stream";
import { mkdir, readFile, rm, writeFile } from "fs/promises";
import { homedir } from "os";
import path from "path";
import { pipeline } from "stream/promises";
import tmp from "tmp-promise";
import xml2js from "xml2js";
import { fetcher } from "@fern-typescript/fetcher";
import fs from "fs";

const ETAG_FILENAME = "etag";
const PREVIEW_FOLDER_NAME = "preview";
Expand Down Expand Up @@ -66,8 +67,18 @@ export async function downloadBundle({
preferCached: boolean;
}): Promise<DownloadLocalBundle.Result> {
logger.debug("Setting up docs preview bundle...");
const response = await fetch(bucketUrl);
const body = await response.text();
const response = await fetcher<string>({
url: bucketUrl,
method: "GET",
responseType: "text",
duplex: "half"
});
if (!response.ok) {
return {
type: "failure"
};
}
const body = response.body;
const parser = new xml2js.Parser();
const parsedResponse = await parser.parseStringPromise(body);
const eTag = parsedResponse?.ListBucketResult?.Contents?.[0]?.ETag?.[0];
Expand Down Expand Up @@ -99,10 +110,15 @@ export async function downloadBundle({

logger.debug(`Downloading docs preview bundle from ${path.join(bucketUrl, key)}`);
// download docs bundle
const docsBundleZipResponse = await fetch(`${path.join(bucketUrl, key)}`);
const docsBundleZipResponse = await fetcher<unknown>({
url: `${path.join(bucketUrl, key)}`,
method: "GET",
responseType: "arrayBuffer",
duplex: "half"
});

if (!docsBundleZipResponse.ok) {
logger.error(`Failed to download docs preview bundle. ${docsBundleZipResponse.statusText}`);
logger.error("Failed to download docs preview bundle.");
return {
type: "failure"
};
Expand All @@ -115,8 +131,10 @@ export async function downloadBundle({
type: "failure"
};
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
await pipeline(contents as any, createWriteStream(outputZipPath));
const nodeBuffer = Buffer.from(contents as any);
await writeFile(outputZipPath, nodeBuffer);
logger.debug(`Wrote output.zip to ${outputZipPath}`);

const absolutePathToPreviewFolder = getPathToPreviewFolder();
Expand Down
9 changes: 5 additions & 4 deletions packages/cli/docs-preview/src/runPreviewServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ export async function runPreviewServer({
await downloadBundle({ bucketUrl: url, logger: context.logger, preferCached: true });
} catch (err) {
const pathToBundle = getPathToBundleFolder();
if (err instanceof Error) {
context.logger.debug(`Failed to download latest docs bundle: ${(err as Error).message}`);
}
if (await doesPathExist(pathToBundle)) {
context.logger.warn("Failed to download latest docs application. Falling back to existing bundle.");
context.logger.warn("Falling back to cached bundle...");
} else {
context.logger.warn(
"Failed to download docs application. Please reach out to [email protected]."
);
context.logger.warn("Please reach out to [email protected].");
return;
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/docs-preview/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{ "path": "../logger" },
{ "path": "../project-loader" },
{ "path": "../register" },
{ "path": "../task-context" }
{ "path": "../task-context" },
{ "path": "../../../generators/typescript/utils/core-utilities/fetcher"}
]
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion seed/ts-sdk/alias-extends/src/core/fetcher/Fetcher.ts

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

2 changes: 2 additions & 0 deletions seed/ts-sdk/alias-extends/src/core/fetcher/getResponseBody.ts

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

2 changes: 1 addition & 1 deletion seed/ts-sdk/alias/src/core/fetcher/Fetcher.ts

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

2 changes: 2 additions & 0 deletions seed/ts-sdk/alias/src/core/fetcher/getResponseBody.ts

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

2 changes: 1 addition & 1 deletion seed/ts-sdk/any-auth/src/core/fetcher/Fetcher.ts

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

2 changes: 2 additions & 0 deletions seed/ts-sdk/any-auth/src/core/fetcher/getResponseBody.ts

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

2 changes: 1 addition & 1 deletion seed/ts-sdk/api-wide-base-path/src/core/fetcher/Fetcher.ts

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

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

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

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

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

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

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

Loading

0 comments on commit 50bf6cb

Please sign in to comment.