Skip to content

Commit

Permalink
fix(cli): support uploading files for docs deployments on windows (#4674
Browse files Browse the repository at this point in the history
)

* add utility to convert paths

* export utilites from fs-utils

* update imports

* add windows test

* run in pr for now

* move execution policies

* ps env var

* don't run in background

* don't run in background

* don't run in background

* debug

* try claude script

* proper invocation

* different gh action

* move function above invocation

* add silent removal

* run on main push

* add changelog entry
  • Loading branch information
RohinBhargava authored Sep 20, 2024
1 parent effc59b commit 9f96f7b
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 6 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,38 @@ jobs:
cli_path="$(pwd)/packages/cli/cli/dist/dev/bundle.cjs"
./scripts/live-test.sh "$cli_path" "$FERN_ORG_TOKEN_DEV"
live-test-dev-windows:
environment: Fern Dev
if: github.ref == 'refs/heads/main'
runs-on: windows-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Install
uses: ./.github/actions/install

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'

- name: Check API definition is valid
env:
FORCE_COLOR: "2"
FERN_ORG_TOKEN_DEV: ${{ secrets.FERN_ORG_TOKEN_DEV }}
AUTH0_DOMAIN: ${{ secrets.AUTH0_DOMAIN }}
AUTH0_CLIENT_ID: ${{ secrets.AUTH0_CLIENT_ID }}
run: |
pnpm --filter @fern-api/cli dist:cli:dev
$cliPath = Join-Path $env:GITHUB_WORKSPACE "packages\cli\cli\dist\dev\bundle.cjs"
if (-not (Test-Path $cliPath)) {
Write-Error "CLI path does not exist: $cliPath"
exit 1
}
$scriptPath = Join-Path $env:GITHUB_WORKSPACE "scripts\live-test.ps1"
& $scriptPath -cli_path $cliPath -token $env:FERN_ORG_TOKEN_DEV
ts-generator-changes:
runs-on: ubuntu-latest
outputs:
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
- changelogEntry:
- summary: |
Previously, deploying docs from Windows machines led to bad asset paths.
Now, the CLI respects Windows paths during run and web paths for retrieving
assets.
type: fix
irVersion: 53
version: 0.42.12

- changelogEntry:
- summary: |
The API V2 configuration now supports disabling using titles as schema
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/docs-preview/src/previewDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FdrAPI,
SDKSnippetHolder
} from "@fern-api/fdr-sdk";
import { convertToFernHostAbsoluteFilePath } from "@fern-api/fs-utils";
import { IntermediateRepresentation } from "@fern-api/ir-sdk";
import { Project } from "@fern-api/project-loader";
import { convertIrToFdrApi } from "@fern-api/register";
Expand Down Expand Up @@ -56,7 +57,7 @@ export async function getPreviewDocsDefinition({
const fileId = uuidv4();
filesV2[fileId] = {
type: "url",
url: `/_local${file.absoluteFilePath}`
url: `/_local${convertToFernHostAbsoluteFilePath(file.absoluteFilePath)}`
};
return {
absoluteFilePath: file.absoluteFilePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createFdrService } from "@fern-api/core";
import { MediaType } from "@fern-api/core-utils";
import { DocsDefinitionResolver, UploadedFile, wrapWithHttps } from "@fern-api/docs-resolver";
import { AbsoluteFilePath, RelativeFilePath, resolve } from "@fern-api/fs-utils";
import { convertToFernHostRelativeFilePath } from "@fern-api/fs-utils";
import { convertIrToFdrApi } from "@fern-api/register";
import { TaskContext } from "@fern-api/task-context";
import { DocsWorkspace, FernWorkspace } from "@fern-api/workspace-loader";
Expand Down Expand Up @@ -87,7 +88,7 @@ export async function publishDocs({
return;
}
const imageFilePath = {
filePath: filePath.relativeFilePath,
filePath: convertToFernHostRelativeFilePath(filePath.relativeFilePath),
width: image.width,
height: image.height,
blurDataUrl: image.blurDataUrl
Expand All @@ -97,7 +98,7 @@ export async function publishDocs({

const filepaths = files
.filter(({ absoluteFilePath }) => !measuredImages.has(absoluteFilePath))
.map(({ relativeFilePath }) => relativeFilePath);
.map(({ relativeFilePath }) => convertToFernHostRelativeFilePath(relativeFilePath));

if (preview) {
const startDocsRegisterResponse = await fdr.docs.v2.write.startDocsPreviewRegister({
Expand Down
3 changes: 2 additions & 1 deletion packages/commons/fs-utils/src/AbsoluteFilePath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "path";
import { convertToOsPath } from "./osPathConverter";

export type AbsoluteFilePath = string & {
__AbsoluteFilePath: void;
Expand All @@ -9,6 +10,6 @@ export const AbsoluteFilePath = {
if (!path.isAbsolute(value)) {
throw new Error("Filepath is not absolute: " + value);
}
return value as AbsoluteFilePath;
return convertToOsPath(value) as AbsoluteFilePath;
}
};
3 changes: 2 additions & 1 deletion packages/commons/fs-utils/src/RelativeFilePath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "path";
import { convertToOsPath } from "./osPathConverter";

export type RelativeFilePath = string & {
__RelativeFilePath: void;
Expand All @@ -9,6 +10,6 @@ export const RelativeFilePath = {
if (path.isAbsolute(value)) {
throw new Error("Filepath is not relative: " + value);
}
return value as RelativeFilePath;
return convertToOsPath(value) as RelativeFilePath;
}
};
5 changes: 5 additions & 0 deletions packages/commons/fs-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ export { streamObjectToFile } from "./streamObjectToFile";
export { stringifyLargeObject } from "./stringifyLargeObject";
export { waitUntilPathExists } from "./waitUntilPathExists";
export { streamObjectFromFile } from "./streamObjectFromFile";
export {
convertToOsPath,
convertToFernHostAbsoluteFilePath,
convertToFernHostRelativeFilePath
} from "./osPathConverter";
29 changes: 29 additions & 0 deletions packages/commons/fs-utils/src/osPathConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AbsoluteFilePath } from "./AbsoluteFilePath";
import { RelativeFilePath } from "./RelativeFilePath";

// in this function, we ignore drive paths and roots, since many strings are passed as partial relative paths
export function convertToOsPath(path: string): string {
if (process.platform === "win32") {
return path.replace(/\//g, "\\");
} else {
return path.replace(/\\/g, "/");
}
}

function convertToFernHostPath(path: string): string {
let unixPath = path;
if (/^[a-zA-Z]:\\/.test(path)) {
unixPath = path.substring(2);
}

return unixPath.replace(/\\/g, "/");
}

export function convertToFernHostAbsoluteFilePath(path: AbsoluteFilePath): AbsoluteFilePath {
// Don't use 'of' here, as it will use OS path, we want fern path
return convertToFernHostPath(path) as AbsoluteFilePath;
}
export function convertToFernHostRelativeFilePath(path: RelativeFilePath): RelativeFilePath {
// Don't use 'of' here, as it will use OS path, we want fern path
return convertToFernHostPath(path) as RelativeFilePath;
}
3 changes: 2 additions & 1 deletion packages/commons/fs-utils/src/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from "path";
import { AbsoluteFilePath } from "./AbsoluteFilePath";
import { convertToOsPath } from "./osPathConverter";

export function resolve(first: AbsoluteFilePath, ...rest: string[]): AbsoluteFilePath;
export function resolve(...paths: string[]): string {
return path.resolve(...paths);
return path.resolve(...paths.map(convertToOsPath));
}
38 changes: 38 additions & 0 deletions scripts/live-test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
param(
[Parameter(Mandatory=$true)]
[string]$cli_path,

[Parameter(Mandatory=$true)]
[string]$token
)

$ErrorActionPreference = "Stop"

function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
$name = [System.Guid]::NewGuid().ToString()
$path = Join-Path $parent $name
New-Item -ItemType Directory -Path $path
}

$original_dir = Get-Location
$test_dir = New-TemporaryDirectory
Set-Location $test_dir

try {
$env:FERN_TOKEN = $token

Write-Host "Running Fern Commands!"
$DebugPreference = "Continue"
& node $cli_path init --organization fern
& node $cli_path add fern-java-sdk
& node $cli_path add fern-python-sdk
& node $cli_path add fern-postman
& node $cli_path generate --log-level debug
$DebugPreference = "SilentlyContinue"
& node $cli_path register --log-level debug
}
finally {
Set-Location $original_dir
Remove-Item -Recurse -Force $test_dir -ErrorAction SilentlyContinue
}

0 comments on commit 9f96f7b

Please sign in to comment.