-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat, local preview): setup dynamic local preview (#3634)
- Loading branch information
Showing
18 changed files
with
272 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/cli/docs-preview/src/__test__/downloadLocalDocsBundle.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { CONSOLE_LOGGER } from "@fern-api/logger"; | ||
import { downloadBundle } from "../downloadLocalDocsBundle"; | ||
|
||
describe("preview", () => { | ||
it("download frontend", async () => { | ||
await downloadBundle({ | ||
bucketUrl: "https://dev2-local-preview-bundle2.s3.amazonaws.com/", | ||
logger: CONSOLE_LOGGER, | ||
preferCached: false | ||
}); | ||
|
||
expect(true).toEqual(true); | ||
}, 60_000); | ||
}); |
132 changes: 132 additions & 0 deletions
132
packages/cli/docs-preview/src/downloadLocalDocsBundle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
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 { 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"; | ||
|
||
const ETAG_FILENAME = "etag"; | ||
const PREVIEW_FOLDER_NAME = "preview"; | ||
const BUNDLE_FOLDER_NAME = "bundle"; | ||
const LOCAL_STORAGE_FOLDER = process.env.LOCAL_STORAGE_FOLDER ?? ".fern"; | ||
|
||
export function getLocalStorageFolder(): AbsoluteFilePath { | ||
return join(AbsoluteFilePath.of(homedir()), RelativeFilePath.of(LOCAL_STORAGE_FOLDER)); | ||
} | ||
|
||
export function getPathToPreviewFolder(): AbsoluteFilePath { | ||
return join( | ||
AbsoluteFilePath.of(homedir()), | ||
RelativeFilePath.of(LOCAL_STORAGE_FOLDER), | ||
RelativeFilePath.of(PREVIEW_FOLDER_NAME) | ||
); | ||
} | ||
|
||
export function getPathToBundleFolder(): AbsoluteFilePath { | ||
return join( | ||
AbsoluteFilePath.of(homedir()), | ||
RelativeFilePath.of(LOCAL_STORAGE_FOLDER), | ||
RelativeFilePath.of(PREVIEW_FOLDER_NAME), | ||
RelativeFilePath.of(BUNDLE_FOLDER_NAME) | ||
); | ||
} | ||
|
||
export function getPathToEtagFile(): AbsoluteFilePath { | ||
return join( | ||
AbsoluteFilePath.of(homedir()), | ||
RelativeFilePath.of(LOCAL_STORAGE_FOLDER), | ||
RelativeFilePath.of(PREVIEW_FOLDER_NAME), | ||
RelativeFilePath.of(ETAG_FILENAME) | ||
); | ||
} | ||
|
||
export declare namespace DownloadLocalBundle { | ||
type Result = SuccesResult | FailureResult; | ||
|
||
interface SuccesResult { | ||
type: "success"; | ||
} | ||
|
||
interface FailureResult { | ||
type: "failure"; | ||
} | ||
} | ||
|
||
export async function downloadBundle({ | ||
bucketUrl, | ||
logger, | ||
preferCached | ||
}: { | ||
bucketUrl: string; | ||
logger: Logger; | ||
preferCached: boolean; | ||
}): Promise<DownloadLocalBundle.Result> { | ||
logger.debug("Setting up docs preview bundle..."); | ||
const response = await fetch(bucketUrl); | ||
const body = await response.text(); | ||
const parser = new xml2js.Parser(); | ||
const parsedResponse = await parser.parseStringPromise(body); | ||
const eTag = parsedResponse?.ListBucketResult?.Contents?.[0]?.ETag?.[0]; | ||
const key = parsedResponse?.ListBucketResult?.Contents?.[0]?.Key?.[0]; | ||
|
||
const eTagFilepath = getPathToEtagFile(); | ||
if (preferCached) { | ||
const currentETagExists = await doesPathExist(eTagFilepath); | ||
let currentETag = undefined; | ||
if (currentETagExists) { | ||
logger.debug("Reading existing ETag"); | ||
currentETag = (await readFile(eTagFilepath)).toString(); | ||
} | ||
if (currentETag != null && currentETag === eTag) { | ||
logger.debug("ETag matches. Using already downloaded bundle"); | ||
// The bundle is already downloaded | ||
return { | ||
type: "success" | ||
}; | ||
} else { | ||
logger.debug("ETag is different. Downloading latest preview bundle"); | ||
} | ||
} | ||
|
||
// create tmp directory | ||
const dir = await tmp.dir({ prefix: "fern" }); | ||
const absoluteDirectoryToTmpDir = AbsoluteFilePath.of(dir.path); | ||
logger.debug(`Created tmp directory ${absoluteDirectoryToTmpDir}`); | ||
|
||
// download docs bundle | ||
const docsBundleZipResponse = await fetch(`${path.join(bucketUrl, key)}`); | ||
const outputZipPath = join(absoluteDirectoryToTmpDir, RelativeFilePath.of("output.zip")); | ||
|
||
const contents = docsBundleZipResponse.body; | ||
if (contents == null) { | ||
return { | ||
type: "failure" | ||
}; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await pipeline(contents as any, createWriteStream(outputZipPath)); | ||
logger.debug(`Wrote output.zip to ${outputZipPath}`); | ||
|
||
const absolutePathToPreviewFolder = getPathToPreviewFolder(); | ||
if (await doesPathExist(absolutePathToPreviewFolder)) { | ||
await rm(absolutePathToPreviewFolder, { recursive: true }); | ||
} | ||
await mkdir(absolutePathToPreviewFolder, { recursive: true }); | ||
logger.debug(`rm -rf ${absolutePathToPreviewFolder}`); | ||
|
||
const absolutePathToBundleFolder = getPathToBundleFolder(); | ||
await mkdir(absolutePathToBundleFolder, { recursive: true }); | ||
await decompress(outputZipPath, absolutePathToBundleFolder); | ||
|
||
// write etag | ||
await writeFile(eTagFilepath, eTag); | ||
logger.debug(`Downloaded bundle to ${absolutePathToBundleFolder}`); | ||
|
||
return { | ||
type: "success" | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ import { TaskContext } from "@fern-api/task-context"; | |
import { APIWorkspace, DocsWorkspace } from "@fern-api/workspace-loader"; | ||
import cors from "cors"; | ||
import express from "express"; | ||
import path from "path"; | ||
import { downloadBundle, getPathToBundleFolder } from "./downloadLocalDocsBundle"; | ||
import { getPreviewDocsDefinition } from "./previewDocs"; | ||
|
||
export async function runPreviewServer({ | ||
|
@@ -14,6 +16,14 @@ export async function runPreviewServer({ | |
apiWorkspaces: APIWorkspace[]; | ||
context: TaskContext; | ||
}): Promise<void> { | ||
const url = process.env.DOCS_PREVIEW_BUCKET; | ||
if (url == null) { | ||
context.failAndThrow("Failed to connect to the docs preview server. Please contact [email protected]"); | ||
return; | ||
} | ||
|
||
await downloadBundle({ bucketUrl: url, logger: context.logger, preferCached: true }); | ||
|
||
const app = express(); | ||
app.use(cors()); | ||
|
||
|
@@ -36,7 +46,13 @@ export async function runPreviewServer({ | |
}); | ||
app.listen(3000); | ||
|
||
context.logger.info("Running server on https://localhost:3000"); | ||
app.use("/_next", express.static(path.join(getPathToBundleFolder(), "/_next"))); | ||
|
||
app.use("*", async (_req, res) => { | ||
return res.sendFile(path.join(getPathToBundleFolder(), "/[[...slug]].html")); | ||
}); | ||
|
||
context.logger.info("Running server on http://localhost:3000"); | ||
|
||
// await infiinitely | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.