forked from fern-api/fern
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support
.fernignore
in local generation (fern-api#1905)
* empty * support for .fernignore when generating locally Fixes fern-api#1780 (fern-api#1809) * support for .fernignore when generating locally (fern-api#1780) * ensuring dependecies are up to date * refactor test * refactor into LocalTaskHandler --------- Co-authored-by: dsinghvi <[email protected]> * fix * remove glob package * downgrade glob --------- Co-authored-by: Mendie Uwemedimo <[email protected]>
- Loading branch information
1 parent
1768043
commit b43a25e
Showing
11 changed files
with
199 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
129 changes: 129 additions & 0 deletions
129
packages/cli/generation/local-generation/local-workspace-runner/src/LocalTaskHandler.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,129 @@ | ||
import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils"; | ||
import { loggingExeca } from "@fern-api/logging-execa"; | ||
import { FERNIGNORE_FILENAME } from "@fern-api/project-configuration"; | ||
import { TaskContext } from "@fern-api/task-context"; | ||
import decompress from "decompress"; | ||
import { cp, readdir, readFile } from "fs/promises"; | ||
import tmp from "tmp-promise"; | ||
|
||
export declare namespace LocalTaskHandler { | ||
export interface Init { | ||
context: TaskContext; | ||
absolutePathToTmpOutputDirectory: AbsoluteFilePath; | ||
absolutePathToLocalOutput: AbsoluteFilePath; | ||
} | ||
} | ||
|
||
export class LocalTaskHandler { | ||
private context: TaskContext; | ||
private absolutePathToTmpOutputDirectory: AbsoluteFilePath; | ||
private absolutePathToLocalOutput: AbsoluteFilePath; | ||
|
||
constructor({ context, absolutePathToTmpOutputDirectory, absolutePathToLocalOutput }: LocalTaskHandler.Init) { | ||
this.context = context; | ||
this.absolutePathToLocalOutput = absolutePathToLocalOutput; | ||
this.absolutePathToTmpOutputDirectory = absolutePathToTmpOutputDirectory; | ||
} | ||
|
||
public async copyGeneratedFiles(): Promise<void> { | ||
if (await this.isFernIgnorePresent()) { | ||
await this.copyGeneratedFilesWithFernIgnore(); | ||
} else { | ||
await this.copyGeneratedFilesNoFernIgnore(); | ||
} | ||
} | ||
|
||
private async isFernIgnorePresent(): Promise<boolean> { | ||
const absolutePathToFernignore = AbsoluteFilePath.of( | ||
join(this.absolutePathToLocalOutput, RelativeFilePath.of(FERNIGNORE_FILENAME)) | ||
); | ||
return await doesPathExist(absolutePathToFernignore); | ||
} | ||
|
||
private async copyGeneratedFilesWithFernIgnore(): Promise<void> { | ||
// Step 1: Create temp directory to resolve .fernignore | ||
const tmpOutputResolutionDir = AbsoluteFilePath.of((await tmp.dir({})).path); | ||
|
||
// Step 2: Read all .fernignore paths | ||
const absolutePathToFernignore = AbsoluteFilePath.of( | ||
join(this.absolutePathToLocalOutput, RelativeFilePath.of(FERNIGNORE_FILENAME)) | ||
); | ||
const fernIngnorePaths = await getFernIgnorePaths({ absolutePathToFernignore }); | ||
|
||
// Step 3: Copy files from local output to tmp directory | ||
await cp(this.absolutePathToLocalOutput, tmpOutputResolutionDir, { recursive: true }); | ||
|
||
// Step 3: In tmp directory initialize a `.git` directory | ||
await this.runGitCommand(["init"], tmpOutputResolutionDir); | ||
await this.runGitCommand(["add", "."], tmpOutputResolutionDir); | ||
await this.runGitCommand(["commit", "-m", '"init"'], tmpOutputResolutionDir); | ||
|
||
// Step 4: Stage deletions `git rm -rf .` | ||
await this.runGitCommand(["rm", "-rf", "."], tmpOutputResolutionDir); | ||
|
||
// Step 5: Copy all files from generated temp dir | ||
await this.copyGeneratedFilesToDirectory(tmpOutputResolutionDir); | ||
|
||
// Step 6: Undo changes to fernignore paths | ||
await this.runGitCommand(["reset", "--", ...fernIngnorePaths], tmpOutputResolutionDir); | ||
await this.runGitCommand(["restore", "."], tmpOutputResolutionDir); | ||
|
||
// Step 8: Delete local output directory and copy all files from the generated directory | ||
this.context.logger.debug(`rm -rf ${this.absolutePathToLocalOutput}`); | ||
await cp(tmpOutputResolutionDir, this.absolutePathToLocalOutput, { recursive: true }); | ||
} | ||
|
||
/** | ||
* If no `.fernignore` is present we can delete the local output directory entirely and | ||
* copy the generated output from the tmp directory. | ||
*/ | ||
private async copyGeneratedFilesNoFernIgnore(): Promise<void> { | ||
this.context.logger.debug(`rm -rf ${this.absolutePathToLocalOutput}`); | ||
await this.copyGeneratedFilesToDirectory(this.absolutePathToLocalOutput); | ||
} | ||
|
||
private async copyGeneratedFilesToDirectory(outputPath: AbsoluteFilePath): Promise<void> { | ||
const [firstLocalOutputItem, ...remaininglocalOutputItems] = await readdir( | ||
this.absolutePathToTmpOutputDirectory | ||
); | ||
if (firstLocalOutputItem == null) { | ||
return; | ||
} | ||
|
||
this.context.logger.debug(`Copying generated files to ${outputPath}`); | ||
if (firstLocalOutputItem.endsWith(".zip") && remaininglocalOutputItems.length === 0) { | ||
await decompress( | ||
join(this.absolutePathToTmpOutputDirectory, RelativeFilePath.of(firstLocalOutputItem)), | ||
this.absolutePathToLocalOutput, | ||
{ | ||
strip: 1, | ||
} | ||
); | ||
} else { | ||
await cp(this.absolutePathToTmpOutputDirectory, this.absolutePathToLocalOutput, { recursive: true }); | ||
} | ||
} | ||
|
||
private async runGitCommand(options: string[], cwd: AbsoluteFilePath): Promise<void> { | ||
await loggingExeca(this.context.logger, "git", options, { | ||
cwd, | ||
}); | ||
} | ||
} | ||
|
||
const NEW_LINE_REGEX = /\r?\n/; | ||
|
||
async function getFernIgnorePaths({ | ||
absolutePathToFernignore, | ||
}: { | ||
absolutePathToFernignore: AbsoluteFilePath; | ||
}): Promise<string[]> { | ||
const fernIgnoreFileContents = (await readFile(absolutePathToFernignore)).toString(); | ||
return [ | ||
FERNIGNORE_FILENAME, | ||
...fernIgnoreFileContents | ||
.trim() | ||
.split(NEW_LINE_REGEX) | ||
.filter((line) => !line.startsWith("#")), | ||
]; | ||
} |
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