forked from fern-api/fern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadGeneratorWorkspaces.ts
More file actions
87 lines (71 loc) · 3.14 KB
/
Copy pathloadGeneratorWorkspaces.ts
File metadata and controls
87 lines (71 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils";
import { findUp } from "find-up";
import { readdir, readFile } from "fs/promises";
import yaml from "js-yaml";
import { FernSeedConfig } from "./config";
export interface GeneratorWorkspace {
workspaceName: string;
absolutePathToWorkspace: AbsoluteFilePath;
workspaceConfig: FernSeedConfig.SeedWorkspaceConfiguration;
}
export interface CliWorkspace {
workspaceName: string;
absolutePathToWorkspace: AbsoluteFilePath;
workspaceConfig: FernSeedConfig.CliSeedWorkspaceConfiguration;
}
export const SEED_DIRECTORY = "seed";
export const SEED_CONFIG_FILENAME = "seed.yml";
export const CLI_SEED_DIRECTORY = "fern-cli";
export async function loadGeneratorWorkspaces(): Promise<GeneratorWorkspace[]> {
const seedDirectory = await getSeedDirectory();
if (seedDirectory == null) {
throw new Error("Failed to find seed folder");
}
const seedDirectoryContents = await readdir(seedDirectory, { withFileTypes: true });
const workspaceDirectoryNames = seedDirectoryContents.reduce<string[]>((all, item) => {
if (item.isDirectory() && item.name !== CLI_SEED_DIRECTORY) {
all.push(item.name);
}
return all;
}, []);
const workspaces: GeneratorWorkspace[] = [];
for (const workspace of workspaceDirectoryNames) {
const absolutePathToWorkspace = join(seedDirectory, RelativeFilePath.of(workspace));
const seedConfig = await readFile(join(absolutePathToWorkspace, RelativeFilePath.of(SEED_CONFIG_FILENAME)));
workspaces.push({
absolutePathToWorkspace,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
workspaceConfig: yaml.load(seedConfig.toString()) as any as FernSeedConfig.SeedWorkspaceConfiguration,
workspaceName: workspace
});
}
return workspaces;
}
async function getSeedDirectory(): Promise<AbsoluteFilePath | undefined> {
const seedDirectoryStr = await findUp(SEED_DIRECTORY, { type: "directory" });
if (seedDirectoryStr == null) {
return undefined;
}
return AbsoluteFilePath.of(seedDirectoryStr);
}
export async function getFernCliSeedDirectory(): Promise<AbsoluteFilePath | undefined> {
const seedDirectoryStr = await findUp(SEED_DIRECTORY, { type: "directory" });
if (seedDirectoryStr == null) {
return undefined;
}
return join(AbsoluteFilePath.of(seedDirectoryStr), RelativeFilePath.of(CLI_SEED_DIRECTORY));
}
export async function loadCliWorkspace(): Promise<CliWorkspace> {
const seedDirectory = await getSeedDirectory();
if (seedDirectory == null) {
throw new Error("Failed to find seed folder");
}
const absolutePathToWorkspace = join(seedDirectory, RelativeFilePath.of(CLI_SEED_DIRECTORY));
const seedConfig = await readFile(join(absolutePathToWorkspace, RelativeFilePath.of(SEED_CONFIG_FILENAME)));
const workspaceConfig = yaml.load(seedConfig.toString()) as any as FernSeedConfig.CliSeedWorkspaceConfiguration;
return {
workspaceName: CLI_SEED_DIRECTORY,
absolutePathToWorkspace,
workspaceConfig
};
}