Skip to content

Commit

Permalink
Add generators config v2 (#630)
Browse files Browse the repository at this point in the history
* Add generators config v2

* Fix compile
  • Loading branch information
zachkirsch authored Aug 23, 2022
1 parent d2e928b commit 33265c0
Show file tree
Hide file tree
Showing 26 changed files with 409 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .pnp.cjs

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

1 change: 1 addition & 0 deletions packages/_root/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@fern-api/docs": "workspace:*",
"@fern-api/ete-tests": "workspace:*",
"@fern-api/generators-configuration": "workspace:*",
"@fern-api/generators-configuration-v2": "workspace:*",
"@fern-api/init": "workspace:*",
"@fern-api/ir-generator": "workspace:*",
"@fern-api/json-schema": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/_root/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{ "path": "../cli/cli" },
{ "path": "../cli/config-management/commons" },
{ "path": "../cli/config-management/generators-configuration" },
{ "path": "../cli/config-management/generators-configuration-v2" },
{ "path": "../cli/config-management/manage-generator" },
{ "path": "../cli/config-management/project-configuration" },
{ "path": "../cli/ete-tests" },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ignores": ["@types/jest", "@types/node", "@babel/core", "@babel/preset-env", "@babel/preset-typescript"],
"ignore-patterns": ["lib"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "library",
"private": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../../../.prettierrc.json");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../../../babel.config.json");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../../../../shared/jest.config.shared";
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@fern-api/generators-configuration-v2",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern.git",
"directory": "packages/cli/config-management/generators-configuration-v2"
},
"files": [
"lib"
],
"type": "module",
"source": "src/index.ts",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"sideEffects": false,
"scripts": {
"clean": "rm -rf ./lib && tsc --build --clean",
"compile": "tsc --build",
"test": "yarn compile && jest --passWithNoTests",
"lint:eslint": "eslint --max-warnings 0 . --ignore-path=../../../../.eslintignore",
"lint:eslint:fix": "eslint --max-warnings 0 . --ignore-path=../../../../.eslintignore --fix",
"format": "prettier --write --ignore-unknown --ignore-path ../../../../shared/.prettierignore \"**\"",
"format:check": "prettier --check --ignore-unknown --ignore-path ../../../../shared/.prettierignore \"**\"",
"depcheck": "depcheck"
},
"dependencies": {
"@fern-api/config-management-commons": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/project-configuration": "workspace:*",
"js-yaml": "^4.1.0",
"lodash-es": "^4.17.21",
"zod": "^3.14.3"
},
"devDependencies": {
"@babel/core": "^7.18.10",
"@babel/preset-env": "^7.18.10",
"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^28.1.7",
"@types/js-yaml": "^4.0.5",
"@types/lodash-es": "^4.17.6",
"@types/node": "^18.7.7",
"depcheck": "^1.4.3",
"eslint": "^8.22.0",
"jest": "^28.1.3",
"prettier": "^2.7.1",
"typescript": "4.6.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { AbsoluteFilePath } from "@fern-api/core-utils";
import { GeneratorsConfigurationSchema } from "./schemas/GeneratorsConfigurationSchema";

export interface GeneratorsConfiguration {
absolutePathToConfiguration: AbsoluteFilePath;
rawConfiguration: GeneratorsConfigurationSchema;
draft: DraftGeneratorInvocation[];
release: ReleaseGeneratorInvocation[];
}

export interface DraftGeneratorInvocation {
absolutePathToLocalOutput: AbsoluteFilePath | undefined;
}

export interface ReleaseGeneratorInvocation {
outputs: GeneratorOutputs;
}

export interface BaseGeneratorInvocation {
name: string;
version: string;
config: unknown;
}

export interface GeneratorOutputs {
npm: NpmGeneratorOutput | undefined;
maven: MavenGeneratorOutput | undefined;
github: GithubGeneratorOutput | undefined;
}

export interface NpmGeneratorOutput {
packageName: string;
token: string;
}

export interface MavenGeneratorOutput {
coordinate: string;
username: string;
password: string;
}

export interface GithubGeneratorOutput {
repository: string;
token: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import yaml from "js-yaml";
import { substituteEnvVariables } from "../substituteEnvVariables";

describe("substituteEnvVariables", () => {
it("basic", () => {
process.env.ENV_VAR = "test";
const rawYaml = `
foo: bar
baz:
qux:
thud: \${ENV_VAR}
plugh: \${ENV_VAR}`;
const parsedYaml = yaml.load(rawYaml);
const substitutedYaml = substituteEnvVariables(parsedYaml);
expect(substitutedYaml).toEqual({ foo: "bar", baz: { qux: { thud: "test" } }, plugh: "test" });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { AbsoluteFilePath, dirname, resolve } from "@fern-api/core-utils";
import { GeneratorsConfiguration } from "./GeneratorsConfiguration";
import { GeneratorsConfigurationSchema } from "./schemas/GeneratorsConfigurationSchema";

export function convertGeneratorsConfiguration({
absolutePathToGeneratorsConfiguration,
rawGeneratorsConfiguration,
}: {
absolutePathToGeneratorsConfiguration: AbsoluteFilePath;
rawGeneratorsConfiguration: GeneratorsConfigurationSchema;
}): GeneratorsConfiguration {
return {
absolutePathToConfiguration: absolutePathToGeneratorsConfiguration,
rawConfiguration: rawGeneratorsConfiguration,
draft:
rawGeneratorsConfiguration.draft != null
? rawGeneratorsConfiguration.draft.map((draftInvocation) => {
return {
name: draftInvocation.name,
version: draftInvocation.version,
absolutePathToLocalOutput:
draftInvocation["local-output"] != null
? resolve(
dirname(absolutePathToGeneratorsConfiguration),
draftInvocation["local-output"]
)
: undefined,
config: draftInvocation.config,
};
})
: [],
release:
rawGeneratorsConfiguration.release != null
? rawGeneratorsConfiguration.release.map((draftInvocation) => {
return {
name: draftInvocation.name,
version: draftInvocation.version,
outputs: {
npm:
draftInvocation.outputs.npm != null
? {
packageName: draftInvocation.outputs.npm["package-name"],
token: draftInvocation.outputs.npm.token,
}
: undefined,
maven:
draftInvocation.outputs.maven != null
? {
coordinate: draftInvocation.outputs.maven.coordinate,
username: draftInvocation.outputs.maven.username,
password: draftInvocation.outputs.maven.password,
}
: undefined,
github:
draftInvocation.outputs.github != null
? {
repository: draftInvocation.outputs.github.repository,
token: draftInvocation.outputs.github.token,
}
: undefined,
},
config: draftInvocation.config,
};
})
: [],
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export {
type DraftGeneratorInvocation,
type GeneratorOutputs,
type GeneratorsConfiguration,
type GithubGeneratorOutput,
type MavenGeneratorOutput,
type NpmGeneratorOutput,
type ReleaseGeneratorInvocation,
} from "./GeneratorsConfiguration";
export { loadGeneratorsConfiguration, loadRawGeneratorsConfiguration } from "./loadGeneratorsConfiguration";
export { type GeneratorsConfigurationSchema } from "./schemas/GeneratorsConfigurationSchema";
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { validateSchema } from "@fern-api/config-management-commons";
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/core-utils";
import { GENERATORS_CONFIGURATION_FILENAME } from "@fern-api/project-configuration";
import { readFile } from "fs/promises";
import yaml from "js-yaml";
import { convertGeneratorsConfiguration } from "./convertGeneratorsConfiguration";
import { GeneratorsConfiguration } from "./GeneratorsConfiguration";
import { GeneratorsConfigurationSchema } from "./schemas/GeneratorsConfigurationSchema";
import { substituteEnvVariables } from "./substituteEnvVariables";

export async function loadRawGeneratorsConfiguration({
absolutePathToWorkspace,
}: {
absolutePathToWorkspace: AbsoluteFilePath;
}): Promise<GeneratorsConfigurationSchema> {
const contentsStr = await readFile(getPathToGeneratorsConfiguration({ absolutePathToWorkspace }));
const contentsParsed = substituteEnvVariables(yaml.load(contentsStr.toString()));
return await validateSchema(GeneratorsConfigurationSchema, contentsParsed);
}

export async function loadGeneratorsConfiguration({
absolutePathToWorkspace,
}: {
absolutePathToWorkspace: AbsoluteFilePath;
}): Promise<GeneratorsConfiguration> {
const rawGeneratorsConfiguration = await loadRawGeneratorsConfiguration({ absolutePathToWorkspace });
return convertGeneratorsConfiguration({
absolutePathToGeneratorsConfiguration: getPathToGeneratorsConfiguration({ absolutePathToWorkspace }),
rawGeneratorsConfiguration,
});
}

function getPathToGeneratorsConfiguration({
absolutePathToWorkspace,
}: {
absolutePathToWorkspace: AbsoluteFilePath;
}): AbsoluteFilePath {
return join(absolutePathToWorkspace, RelativeFilePath.of(GENERATORS_CONFIGURATION_FILENAME));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from "zod";

export const BaseGeneratorInvocationSchema = z.strictObject({
name: z.string(),
version: z.string(),
config: z.unknown(),
});

export type BaseGeneratorInvocationSchema = z.infer<typeof BaseGeneratorInvocationSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FilePath } from "@fern-api/core-utils";
import { z } from "zod";
import { BaseGeneratorInvocationSchema } from "./BaseGeneratorInvocationSchema";

export const DraftGeneratorInvocationSchema = BaseGeneratorInvocationSchema.extend({
"local-output": z.optional(z.string().transform(FilePath.of)),
});

export type DraftGeneratorInvocationSchema = z.infer<typeof DraftGeneratorInvocationSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from "zod";
import { GithubRepositoryOutputSchema } from "./GithubRepositoryOutputSchema";
import { MavenRegistryOutputSchema } from "./MavenRegistryOutputSchema";
import { NpmRegistryOutputSchema } from "./NpmRegistryOutputSchema";

export const GeneratorOutputsSchema = z.strictObject({
npm: z.optional(NpmRegistryOutputSchema),
maven: z.optional(MavenRegistryOutputSchema),
github: z.optional(GithubRepositoryOutputSchema),
});

export type GeneratorOutputsSchema = z.infer<typeof GeneratorOutputsSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from "zod";
import { DraftGeneratorInvocationSchema } from "./DraftGeneratorInvocationSchema";
import { ReleaseGeneratorInvocationSchema } from "./ReleaseGeneratorInvocationSchema";

export const GeneratorsConfigurationSchema = z.strictObject({
draft: z.optional(z.array(DraftGeneratorInvocationSchema)),
release: z.optional(z.array(ReleaseGeneratorInvocationSchema)),
});

export type GeneratorsConfigurationSchema = z.infer<typeof GeneratorsConfigurationSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";

export const GithubRepositoryOutputSchema = z.strictObject({
repository: z.string(),
token: z.string(),
});

export type GithubRepositoryOutputSchema = z.infer<typeof GithubRepositoryOutputSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from "zod";

export const MavenRegistryOutputSchema = z.strictObject({
coordinate: z.string(),
username: z.string(),
password: z.string(),
});

export type MavenRegistryOutputSchema = z.infer<typeof MavenRegistryOutputSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";

export const NpmRegistryOutputSchema = z.strictObject({
"package-name": z.string(),
token: z.string(),
});

export type NpmRegistryOutputSchema = z.infer<typeof NpmRegistryOutputSchema>;
Loading

0 comments on commit 33265c0

Please sign in to comment.