-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathloadGeneratorWorkspaces.ts
More file actions
104 lines (87 loc) · 3.75 KB
/
loadGeneratorWorkspaces.ts
File metadata and controls
104 lines (87 loc) · 3.75 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils";
import { CONSOLE_LOGGER } from "@fern-api/logger";
import { findUp } from "find-up";
import { access, readdir, readFile } from "fs/promises";
import yaml from "js-yaml";
import { FernSeedConfig } from "./config/index.js";
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 seedConfigPath = join(absolutePathToWorkspace, RelativeFilePath.of(SEED_CONFIG_FILENAME));
try {
await access(seedConfigPath);
} catch {
CONSOLE_LOGGER.warn(`Skipping ${workspace}: no ${SEED_CONFIG_FILENAME} found`);
continue;
}
const seedConfig = await readFile(seedConfigPath);
const workspaceConfig = yaml.load(
seedConfig.toString()
) as unknown as FernSeedConfig.SeedWorkspaceConfiguration;
if (workspaceConfig.disabled === true) {
// Use warn (stderr) so that commands emitting machine-readable output
// on stdout (e.g. `seed affected --json`) remain parseable.
CONSOLE_LOGGER.warn(`Skipping ${workspace}: disabled in ${SEED_CONFIG_FILENAME}`);
continue;
}
workspaces.push({
absolutePathToWorkspace,
workspaceConfig,
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 unknown as FernSeedConfig.CliSeedWorkspaceConfiguration;
return {
workspaceName: CLI_SEED_DIRECTORY,
absolutePathToWorkspace,
workspaceConfig
};
}