-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feature, generator-cli): Add ability to clone repository (#1028)
- Loading branch information
Showing
24 changed files
with
386 additions
and
18 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
packages/generator-cli/configuration/generated/** linguist-generated=true | ||
clis/generator-cli/configuration/generated/** linguist-generated=true | ||
packages/template-resolver/src/generated/** linguist-generated=true | ||
packages/fdr-sdk/src/client/generated/** linguist-generated=true | ||
servers/fdr/src/api/generated/** linguist-generated=true |
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
10 changes: 10 additions & 0 deletions
10
clis/generator-cli/src/configuration/generated/api/resources/readme/types/GithubRemote.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,10 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
|
||
export interface GithubRemote { | ||
/** A full repo url (i.e. https://github.com/fern-api/fern) */ | ||
repoUrl: string; | ||
/** The token used to clone the GitHub repository. */ | ||
installationToken: string; | ||
} |
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
49 changes: 49 additions & 0 deletions
49
clis/generator-cli/src/configuration/generated/api/resources/readme/types/Remote.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,49 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
|
||
import * as FernGeneratorCli from "../../../index"; | ||
|
||
/** | ||
* The remote where the README.md is hosted. | ||
*/ | ||
export type Remote = FernGeneratorCli.Remote.Github; | ||
|
||
export declare namespace Remote { | ||
interface Github extends FernGeneratorCli.GithubRemote, _Utils { | ||
type: "github"; | ||
} | ||
|
||
interface _Utils { | ||
_visit: <_Result>(visitor: FernGeneratorCli.Remote._Visitor<_Result>) => _Result; | ||
} | ||
|
||
interface _Visitor<_Result> { | ||
github: (value: FernGeneratorCli.GithubRemote) => _Result; | ||
_other: (value: { type: string }) => _Result; | ||
} | ||
} | ||
|
||
export const Remote = { | ||
github: (value: FernGeneratorCli.GithubRemote): FernGeneratorCli.Remote.Github => { | ||
return { | ||
...value, | ||
type: "github", | ||
_visit: function <_Result>( | ||
this: FernGeneratorCli.Remote.Github, | ||
visitor: FernGeneratorCli.Remote._Visitor<_Result> | ||
) { | ||
return FernGeneratorCli.Remote._visit(this, visitor); | ||
}, | ||
}; | ||
}, | ||
|
||
_visit: <_Result>(value: FernGeneratorCli.Remote, visitor: FernGeneratorCli.Remote._Visitor<_Result>): _Result => { | ||
switch (value.type) { | ||
case "github": | ||
return visitor.github(value); | ||
default: | ||
return visitor._other(value as any); | ||
} | ||
}, | ||
} as const; |
2 changes: 2 additions & 0 deletions
2
clis/generator-cli/src/configuration/generated/api/resources/readme/types/index.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
22 changes: 22 additions & 0 deletions
22
...ator-cli/src/configuration/generated/serialization/resources/readme/types/GithubRemote.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,22 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
|
||
import * as serializers from "../../../index"; | ||
import * as FernGeneratorCli from "../../../../api/index"; | ||
import * as core from "../../../../core"; | ||
|
||
export const GithubRemote: core.serialization.ObjectSchema< | ||
serializers.GithubRemote.Raw, | ||
FernGeneratorCli.GithubRemote | ||
> = core.serialization.object({ | ||
repoUrl: core.serialization.string(), | ||
installationToken: core.serialization.string(), | ||
}); | ||
|
||
export declare namespace GithubRemote { | ||
interface Raw { | ||
repoUrl: string; | ||
installationToken: string; | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
.../generator-cli/src/configuration/generated/serialization/resources/readme/types/Remote.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,32 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
|
||
import * as serializers from "../../../index"; | ||
import * as FernGeneratorCli from "../../../../api/index"; | ||
import * as core from "../../../../core"; | ||
import { GithubRemote } from "./GithubRemote"; | ||
|
||
export const Remote: core.serialization.Schema<serializers.Remote.Raw, FernGeneratorCli.Remote> = core.serialization | ||
.union("type", { | ||
github: GithubRemote, | ||
}) | ||
.transform<FernGeneratorCli.Remote>({ | ||
transform: (value) => { | ||
switch (value.type) { | ||
case "github": | ||
return FernGeneratorCli.Remote.github(value); | ||
default: | ||
return value as FernGeneratorCli.Remote; | ||
} | ||
}, | ||
untransform: ({ _visit, ...value }) => value as any, | ||
}); | ||
|
||
export declare namespace Remote { | ||
type Raw = Remote.Github; | ||
|
||
interface Github extends GithubRemote.Raw { | ||
type: "github"; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
clis/generator-cli/src/configuration/generated/serialization/resources/readme/types/index.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"extends": "../../shared/tsconfig.shared.json", | ||
"compilerOptions": { "composite": true, "outDir": "dist", "rootDir": "." }, | ||
"include": ["./src/**/*"] | ||
"include": ["./src/**/*"], | ||
"references": [{ "path": "../../packages/commons/github" }] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { lstat, readFile } from "fs/promises"; | ||
import path from "path"; | ||
import { SimpleGit } from "simple-git"; | ||
import { README_FILEPATH } from "./constants"; | ||
|
||
// ClonedRepository is a repository that has been successfully cloned to the local file system | ||
// and is ready to be used. | ||
export class ClonedRepository { | ||
private git: SimpleGit; | ||
private clonePath: string; | ||
|
||
constructor({ git, clonePath }: { git: SimpleGit; clonePath: string }) { | ||
this.git = git; | ||
this.clonePath = clonePath; | ||
} | ||
|
||
public async getReadme(): Promise<string | undefined> { | ||
return await this.readFile({ relativeFilePath: README_FILEPATH }); | ||
} | ||
|
||
private async readFile({ relativeFilePath }: { relativeFilePath: string }): Promise<string | undefined> { | ||
const absoluteFilePath = path.join(this.clonePath, relativeFilePath); | ||
if (!doesPathExist(absoluteFilePath)) { | ||
return undefined; | ||
} | ||
return await readFile(absoluteFilePath, "utf-8"); | ||
} | ||
} | ||
|
||
async function doesPathExist(filepath: string): Promise<boolean> { | ||
try { | ||
await lstat(filepath); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} |
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,11 @@ | ||
// RepositoryReference is a parsed GitHub repository reference, which | ||
// contains a variety of formats. | ||
export interface RepositoryReference { | ||
remote: string; // e.g. github.com | ||
owner: string; // e.g. fern-api | ||
repo: string; // e.g. fern | ||
repoUrl: string; // e.g. https://github.com/fern-api/fern | ||
cloneUrl: string; // e.g. https://github.ccom/fern-api/fern.git | ||
|
||
getAuthedCloneUrl: (installationToken: string) => string; | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/commons/github/src/__test__/cloneRepository.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,28 @@ | ||
import { cloneRepository } from "../cloneRepository"; | ||
|
||
describe("cloneRepository", () => { | ||
it("fern-api/docs-starter-openapi", async () => { | ||
const repository = await cloneRepository({ | ||
githubRepository: "github.com/fern-api/docs-starter-openapi", | ||
installationToken: undefined, | ||
}); | ||
const readme = await repository.getReadme(); | ||
expect(readme).contains("Fern"); | ||
}); | ||
it("invalid installation token", async () => { | ||
await expect(async () => { | ||
await cloneRepository({ | ||
githubRepository: "https://github.com/fern-api/fern-platform", | ||
installationToken: "ghp_xyz", | ||
}); | ||
}).rejects.toThrow(); | ||
}); | ||
it("repository does not exist", async () => { | ||
await expect(async () => { | ||
await cloneRepository({ | ||
githubRepository: "https://github.com/fern-api/does-not-exist", | ||
installationToken: undefined, | ||
}); | ||
}).rejects.toThrow(); | ||
}); | ||
}); |
Oops, something went wrong.