Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat, typescript): copy over zurg unit tests to the generated SDK #4045

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion generators/typescript/express/cli/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ RUN /cache_heavy_dependencies.sh && rm /cache_heavy_dependencies.sh

COPY generators/typescript/utils/core-utilities/zurg/src/ /assets/zurg
COPY generators/typescript/utils/core-utilities/base/src/ /assets/base-core-utilities
RUN find /assets -name __test__ -exec rm -rf {} \+

COPY generators/typescript/express/cli/docker/dist/cli.cjs /cli.cjs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export class ExpressGeneratorCli extends AbstractGeneratorCli<ExpressCustomConfi
const typescriptProject = await expressGenerator.generate();
const persistedTypescriptProject = await typescriptProject.persist();
await expressGenerator.copyCoreUtilities({
pathToSrc: persistedTypescriptProject.getSrcDirectory()
pathToSrc: persistedTypescriptProject.getSrcDirectory(),
pathToRoot: persistedTypescriptProject.getRootDirectory()
});

return persistedTypescriptProject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,14 @@ export class ExpressGenerator {
});
}

public async copyCoreUtilities({ pathToSrc }: { pathToSrc: AbsoluteFilePath }): Promise<void> {
await this.coreUtilitiesManager.copyCoreUtilities({ pathToSrc });
public async copyCoreUtilities({
pathToSrc,
pathToRoot
}: {
pathToSrc: AbsoluteFilePath;
pathToRoot: AbsoluteFilePath;
}): Promise<void> {
await this.coreUtilitiesManager.copyCoreUtilities({ pathToSrc, pathToRoot });
}

private generateTypeDeclarations() {
Expand Down
22 changes: 14 additions & 8 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.36.0] - 2024-07-16

- Fix: Now, there are generated unit tests for the `schemas` core directory which makes sure that
Fern's request + response validation will work as expected!

## [0.35.0] - 2024-07-16

- Fix: Support Multipart Form uploads where `fs.createReadStream` is passed. This requires
coercing the stream into a `File`.
coercing the stream into a `File`.

## [0.34.0] - 2024-07-16

Expand Down Expand Up @@ -54,15 +60,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.33.0] - 2024-07-16

- Fix: This release comes with numerous improvements to multipart uploads:
- Fix: This release comes with numerous improvements to multipart uploads:

1. `Fetcher.ts` no longer depends on form-data and formdata-node which reduces
the size of the SDK for all consumers that are not leveraging multipart form
data uploads.
2. The SDK now accepts `fs.ReadStream`, `Blob` and `File` as inputs and handles
parsing them appropriately.
3. By accepting a `Blob` as a file parameter, the SDK now supports sending the
filename when making a request.
the size of the SDK for all consumers that are not leveraging multipart form
data uploads.
2. The SDK now accepts `fs.ReadStream`, `Blob` and `File` as inputs and handles
parsing them appropriately.
3. By accepting a `Blob` as a file parameter, the SDK now supports sending the
filename when making a request.

## [0.32.0] - 2024-07-15

Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.35.0
0.36.0
1 change: 0 additions & 1 deletion generators/typescript/sdk/cli/browser-docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ COPY generators/typescript/utils/core-utilities/auth/src/ /assets/auth
COPY generators/typescript/utils/core-utilities/zurg/src/ /assets/zurg
COPY generators/typescript/utils/core-utilities/base/src/ /assets/base-core-utilities
COPY generators/typescript/utils/core-utilities/utils/src/ /assets/utils
RUN find /assets -name __test__ -exec rm -rf {} \+

COPY generators/typescript/sdk/cli/browser-docker/dist/browserCli.cjs /browserCli.cjs

Expand Down
1 change: 0 additions & 1 deletion generators/typescript/sdk/cli/node-docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ COPY generators/typescript/utils/core-utilities/zurg/src/ /assets/zurg
COPY generators/typescript/utils/core-utilities/base/src/ /assets/base-core-utilities
COPY generators/typescript/utils/core-utilities/utils/src/ /assets/utils
COPY generators/typescript/sdk/features.yml /assets/features.yml
RUN find /assets -name __test__ -exec rm -rf {} \+

COPY generators/typescript/sdk/cli/node-docker/dist/nodeCli.cjs /nodeCli.cjs

Expand Down
5 changes: 3 additions & 2 deletions generators/typescript/sdk/cli/src/SdkGeneratorCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { JavaScriptRuntime, NpmPackage, PersistedTypescriptProject } from "@fern
import { GeneratorContext } from "@fern-typescript/contexts";
import { SdkGenerator } from "@fern-typescript/sdk-generator";
import { camelCase, upperFirst } from "lodash-es";
import { SdkCustomConfigSchema } from "./custom-config/schema/SdkCustomConfigSchema";
import { SdkCustomConfig } from "./custom-config/SdkCustomConfig";
import { SdkCustomConfigSchema } from "./custom-config/schema/SdkCustomConfigSchema";

export declare namespace SdkGeneratorCli {
export interface Init {
Expand Down Expand Up @@ -139,7 +139,8 @@ export class SdkGeneratorCli extends AbstractGeneratorCli<SdkCustomConfig> {
const typescriptProject = await sdkGenerator.generate();
const persistedTypescriptProject = await typescriptProject.persist();
await sdkGenerator.copyCoreUtilities({
pathToSrc: persistedTypescriptProject.getSrcDirectory()
pathToSrc: persistedTypescriptProject.getSrcDirectory(),
pathToRoot: persistedTypescriptProject.getRootDirectory()
});

return persistedTypescriptProject;
Expand Down
10 changes: 8 additions & 2 deletions generators/typescript/sdk/generator/src/SdkGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,14 @@ export class SdkGenerator {
});
}

public async copyCoreUtilities({ pathToSrc }: { pathToSrc: AbsoluteFilePath }): Promise<void> {
await this.coreUtilitiesManager.copyCoreUtilities({ pathToSrc });
public async copyCoreUtilities({
pathToSrc,
pathToRoot
}: {
pathToSrc: AbsoluteFilePath;
pathToRoot: AbsoluteFilePath;
}): Promise<void> {
await this.coreUtilitiesManager.copyCoreUtilities({ pathToSrc, pathToRoot });
}

private generateTypeDeclarations() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils";
import { cp, rm, writeFile } from "fs/promises";
import {
AbsoluteFilePath,
dirname,
File,
FileOrDirectory,
getDirectoryContents,
join,
RelativeFilePath
} from "@fern-api/fs-utils";
import { cp, mkdir, rm, writeFile } from "fs/promises";
import glob from "glob-promise";
import path from "path";
import { SourceFile } from "ts-morph";
Expand Down Expand Up @@ -56,7 +64,13 @@ export class CoreUtilitiesManager {
}
}

public async copyCoreUtilities({ pathToSrc }: { pathToSrc: AbsoluteFilePath }): Promise<void> {
public async copyCoreUtilities({
pathToSrc,
pathToRoot
}: {
pathToSrc: AbsoluteFilePath;
pathToRoot: AbsoluteFilePath;
}): Promise<void> {
await Promise.all(
[...Object.entries(this.referencedCoreUtilities)].map(async ([utilityName, utility]) => {
const fromPath =
Expand All @@ -68,7 +82,13 @@ export class CoreUtilitiesManager {
...this.getPathToUtility(utility).map((directory) => RelativeFilePath.of(directory.nameOnDisk))
);
await cp(fromPath, toPath, {
recursive: true
recursive: true,
filter: (source) => {
if (source.includes("__test__")) {
return false;
}
return true;
}
});
if (utilityName === "auth") {
// TODO(amckinney): Find a better way to add utility-scoped overrides. The way we designed the
Expand All @@ -87,10 +107,53 @@ export class CoreUtilitiesManager {
});
await Promise.all(filesToDelete.map((filepath) => rm(filepath, { recursive: true })));
}

// Copy over unit tests
if (utility.unitTests) {
const toUnitTestPath = join(
pathToRoot,
RelativeFilePath.of("tests/unit"),
RelativeFilePath.of(utility.name)
);
await mkdir(toUnitTestPath, { recursive: true });

const fromUnitTestPath = join(utility.originalPathOnDocker, RelativeFilePath.of("__test__"));
const files: { path: AbsoluteFilePath; file: File }[] = [];
const contents = await getDirectoryContents(fromUnitTestPath);
for (const fileOrDirectory of contents) {
this.getAllFiles(fileOrDirectory, toUnitTestPath, files);
}

for (const file of files) {
await mkdir(dirname(file.path), { recursive: true });
let contents = file.file.contents;
for (const [find, replace] of Object.entries(utility.unitTests.findAndReplace)) {
contents = contents.replaceAll(find, replace);
}
await writeFile(file.path, contents);
}
}
})
);
}

private getAllFiles(
fileOrDirectory: FileOrDirectory,
absoluteFilePath: AbsoluteFilePath,
files: { path: AbsoluteFilePath; file: File }[]
): void {
if (fileOrDirectory.type === "directory") {
for (const content of fileOrDirectory.contents) {
this.getAllFiles(content, join(absoluteFilePath, RelativeFilePath.of(fileOrDirectory.name)), files);
}
} else {
files.push({
file: fileOrDirectory,
path: join(absoluteFilePath, RelativeFilePath.of(fileOrDirectory.name))
});
}
}

public addAuthOverride({ filepath, content }: { filepath: RelativeFilePath; content: string }): void {
this.authOverrides[filepath] = content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export declare namespace CoreUtility {
path: RelativeFilePath;
ignoreGlob?: string;
};
unitTests?: {
/**
* Import strings that we want to find + replace
*/
findAndReplace: Record<string, string>;
};
originalPathOnDocker: AbsoluteFilePath;
pathInCoreUtilities: ExportedDirectory[];
addDependencies?: (dependencyManager: DependencyManager) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export class FetcherImpl extends CoreUtility implements Fetcher {
});
}
};

public readonly Fetcher: Fetcher["Fetcher"] = {
Args: {
properties: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export class ZurgImpl extends CoreUtility implements Zurg {
path: RelativeFilePath.of("generators/typescript/utils/core-utilities/zurg/src"),
ignoreGlob: "**/__test__"
},
unitTests: {
findAndReplace: {
"../../builders": "../../../../src/core/schemas/builders",
"../builders": "../../../src/core/schemas/builders",
"../../Schema": "../../../../src/core/schemas/Schema"
}
},
originalPathOnDocker: AbsoluteFilePath.of("/assets/zurg"),
pathInCoreUtilities: [{ nameOnDisk: "schemas", exportDeclaration: { namespaceExport: "serialization" } }]
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export class PersistedTypescriptProject {
return join(this.directory, this.srcDirectory);
}

public getRootDirectory(): AbsoluteFilePath {
return this.directory;
}

public async installDependencies(logger: Logger): Promise<void> {
if (this.hasInstalled) {
return;
Expand Down
10 changes: 0 additions & 10 deletions seed/ts-sdk/alias/.mock/definition/__package__.yml

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

Loading
Loading