Skip to content

Commit

Permalink
feat(cli): add language and repo metadata to fern generator get (#4678)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo authored Sep 18, 2024
1 parent 822e392 commit 19b57e0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 5 deletions.
58 changes: 57 additions & 1 deletion packages/cli/cli/src/cliV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ export function addGeneratorCommands(cli: Argv<GlobalCliOptions>, cliContext: Cl
boolean: true,
default: false,
description: "Get the version of the specified generator."
})
.option("language", {
boolean: true,
default: false,
description: "Get the language of the specified generator."
})
.option("repository", {
boolean: true,
default: false,
description: "Get repository for the generator invocation, if one is specified."
}),
async (argv) => {
await cliContext.instrumentPostHogEvent({
Expand All @@ -241,20 +251,66 @@ export function addGeneratorCommands(cli: Argv<GlobalCliOptions>, cliContext: Cl
defaultToAllApiWorkspaces: true
})
});

interface GeneratorMetadata {
version?: string;
language?: string;
repository?: string;
}

if (generator == null) {
const maybeApiFilter = argv.api ? ` for API ${argv.api}` : "";
cliContext.failAndThrow(
`Generator ${argv.generator}, in group ${argv.group}${maybeApiFilter} was not found.`
);
}

const generatorMetadata: GeneratorMetadata = {};
if (argv.version) {
generatorMetadata.version = generator.version;
if (argv.output == null) {
process.stdout.write(generator.version);
return;
}
}

if (argv.language) {
if (generator.language != null) {
generatorMetadata.language = generator.language;
if (argv.output == null) {
process.stdout.write(generator.language);
return;
}
} else {
cliContext.logger.warn(
`Language information is not available for generator ${generator.name} in group ${argv.group}`
);
}
}

if (argv.repository) {
const repository =
generator.outputMode.type === "github"
? generator.outputMode.repo
: generator.outputMode.type === "githubV2"
? generator.outputMode.githubV2.repo
: undefined;
if (repository != null) {
generatorMetadata.repository = repository;
if (argv.output == null) {
process.stdout.write(generatorMetadata.repository);
return;
}
} else {
cliContext.logger.warn(
`Repository information is not available for generator ${generator.name} in group ${argv.group}`
);
}
}

if (argv.output) {
try {
await writeFile(argv.output, generator.version);
await writeFile(argv.output, JSON.stringify(generatorMetadata, null, 2));
} catch (error) {
cliContext.failAndThrow(
`Could not write file to the specified location: ${argv.output}`,
Expand Down
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: Adds additional metadata retrievable by `fern generator get` so you can now get the langauage and the target repo.
type: feat
irVersion: 53
version: 0.42.5

- changelogEntry:
- summary: |
Namespaced APIs now:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`fern generator get > fern generator get --language 1`] = `"typescript"`;

exports[`fern generator get > fern generator get --version 1`] = `"0.39.3"`;

exports[`fern generator get > fern generator get to file 1`] = `
"{
"version": "0.39.3",
"language": "typescript"
}"
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { runFernCli } from "../../utils/runFernCli";
import { init } from "../init/init";
import tmp from "tmp-promise";
import { readFile } from "fs/promises";

// Ensure that the generators list command works and the format doesn't change, since fern-bot consumes this
describe("fern generator get", () => {
Expand All @@ -15,4 +17,43 @@ describe("fern generator get", () => {

expect(out.stdout).toMatchSnapshot();
}, 60_000);

it("fern generator get --language", async () => {
const pathOfDirectory = await init();

const out = await runFernCli(
["generator", "get", "--generator", "fernapi/fern-typescript-node-sdk", "--group", "local", "--language"],
{
cwd: pathOfDirectory
}
);

expect(out.stdout).toMatchSnapshot();
}, 60_000);

it("fern generator get to file", async () => {
const pathOfDirectory = await init();
const tmpFile = await tmp.file();
await runFernCli(
[
"generator",
"get",
"--generator",
"fernapi/fern-typescript-node-sdk",
"--group",
"local",
"--language",
"--version",
"--repository",
"-o",
tmpFile.path
],
{
cwd: pathOfDirectory
}
);

const out = await readFile(tmpFile.path, "utf-8");
expect(out).toMatchSnapshot();
}, 60_000);
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("fern generator upgrade", () => {
}
);

expect((await readFile(outputFile)).toString()).not.toEqual("3.0.0");
expect(JSON.parse((await readFile(outputFile)).toString()).version).not.toEqual("3.0.0");
}, 60_000);

it("fern generator upgrade with filters", async () => {
Expand Down Expand Up @@ -80,7 +80,7 @@ describe("fern generator upgrade", () => {
}
);

expect((await readFile(outputFile)).toString()).not.toEqual("3.0.0");
expect(JSON.parse((await readFile(outputFile)).toString()).version).not.toEqual("3.0.0");
}, 60_000);

it("fern generator help commands", async () => {
Expand Down Expand Up @@ -141,7 +141,7 @@ describe("fern generator upgrade", () => {
}
);

expect((await readFile(outputFile)).toString()).toEqual("2.16.0");
expect(JSON.parse((await readFile(outputFile)).toString()).version).toEqual("2.16.0");

await runFernCli(
[
Expand Down Expand Up @@ -176,7 +176,7 @@ describe("fern generator upgrade", () => {
}
);

expect((await readFile(outputFileNewMajor)).toString()).not.toEqual("2.16.0");
expect(JSON.parse((await readFile(outputFileNewMajor)).toString()).version).not.toEqual("2.16.0");
}, 60_000);

it("fern generator upgrade list", async () => {
Expand Down

0 comments on commit 19b57e0

Please sign in to comment.