-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathcommand.ts
More file actions
301 lines (271 loc) · 12.1 KB
/
command.ts
File metadata and controls
301 lines (271 loc) · 12.1 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { extractErrorMessage, mergeWithOverrides } from "@fern-api/core-utils";
import type { AbsoluteFilePath } from "@fern-api/fs-utils";
import { CliError } from "@fern-api/task-context";
import chalk from "chalk";
import { execFile } from "child_process";
import { readFile, writeFile } from "fs/promises";
import path from "path";
import { promisify } from "util";
import type { Argv } from "yargs";
import { FERN_YML_FILENAME } from "../../../config/fern-yml/constants.js";
import { FernYmlEditor } from "../../../config/fern-yml/FernYmlEditor.js";
import type { Context } from "../../../context/Context.js";
import type { GlobalArgs } from "../../../context/GlobalArgs.js";
import { Icons } from "../../../ui/format.js";
import { command } from "../../_internal/command.js";
import type { SpecEntry } from "../utils/filterSpecs.js";
import { filterSpecs } from "../utils/filterSpecs.js";
import { isEnoentError } from "../utils/isEnoentError.js";
import { loadSpec, parseSpec, serializeSpec } from "../utils/loadSpec.js";
import {
ALL_FORMAT_NAMES,
deriveOutputPath,
normalizeSplitFormat,
OVERLAY_NAME,
OVERRIDES_NAME,
type SplitFormat,
type SplitFormatInput
} from "./deriveOutputPath.js";
import { generateOverlay, generateOverrides, hasChanges, type OverlayOutputAction } from "./diffSpecs.js";
const execFileAsync = promisify(execFile);
/** 50 MB – large enough for oversized OpenAPI specs retrieved via `git show`. */
const GIT_MAX_BUFFER_BYTES = 50 * 1024 * 1024;
export declare namespace SplitCommand {
export interface Args extends GlobalArgs {
api?: string;
output?: string;
format?: SplitFormatInput;
}
}
export class SplitCommand {
public async handle(context: Context, args: SplitCommand.Args): Promise<void> {
const workspace = await context.loadWorkspaceOrThrow();
if (Object.keys(workspace.apis).length === 0) {
throw new CliError({ message: "No APIs found in workspace.", code: CliError.Code.ConfigError });
}
const entries = filterSpecs(workspace, { api: args.api });
if (entries.length === 0) {
context.stderr.info(chalk.dim("No matching OpenAPI/AsyncAPI specs found."));
return;
}
const format: SplitFormat = normalizeSplitFormat(args.format ?? OVERLAY_NAME);
const fernYmlPath = workspace.absoluteFilePath;
if (fernYmlPath == null) {
throw new CliError({
message: `No ${FERN_YML_FILENAME} found. Run 'fern init' to initialize a project.`,
code: CliError.Code.ConfigError
});
}
const editor = await FernYmlEditor.load({ fernYmlPath });
const repoRoot = await this.getRepoRoot(fernYmlPath);
let splitCount = 0;
for (const entry of entries) {
const [currentContent, originalRaw] = await Promise.all([
loadSpec(entry.specFilePath),
this.getFileFromGitHead(repoRoot, entry.specFilePath)
]);
const originalContent = parseSpec(originalRaw, entry.specFilePath);
if (!hasChanges(originalContent, currentContent)) {
context.stderr.info(chalk.dim(`${entry.specFilePath}: no changes from git HEAD, skipping.`));
continue;
}
if (format === OVERLAY_NAME) {
await this.splitAsOverlay(context, editor, entry, originalContent, currentContent, args.output);
} else {
await this.splitAsOverrides(context, editor, entry, originalContent, currentContent, args.output);
}
// Restore spec to git HEAD after overlay/overrides are safely written
await writeFile(entry.specFilePath, originalRaw);
context.stderr.debug(chalk.dim(`Restored ${entry.specFilePath} to git HEAD`));
splitCount++;
}
await editor.save();
if (splitCount === 0) {
context.stderr.info(chalk.dim("No specs had changes to split."));
}
}
private async splitAsOverlay(
context: Context,
editor: FernYmlEditor,
entry: SpecEntry,
// biome-ignore lint/suspicious/noExplicitAny: OpenAPI specs can have any shape
originalContent: Record<string, any>,
// biome-ignore lint/suspicious/noExplicitAny: OpenAPI specs can have any shape
currentContent: Record<string, any>,
outputArg: string | undefined
): Promise<void> {
const overlay = generateOverlay(originalContent, currentContent);
// Check if there's an existing overlay configured in fern.yml
const existingOverlayPath = (await editor.getOverlayPath(entry.specFilePath)) ?? entry.overlays;
let outputPath: AbsoluteFilePath;
if (existingOverlayPath != null) {
if (outputArg != null) {
context.stderr.info(
chalk.yellow(
`Warning: --output ignored; merging into existing overlay file ${chalk.cyan(existingOverlayPath)}`
)
);
}
outputPath = existingOverlayPath;
} else {
outputPath =
outputArg != null
? resolvePathOrThrow(context, outputArg)
: deriveOutputPath(entry.specFilePath, OVERLAY_NAME);
}
if (await this.tryMergeOverlayIntoExistingFile(outputPath, overlay.actions)) {
context.stderr.info(
`${Icons.success} Merged ${chalk.bold(String(overlay.actions.length))} action(s) into existing ${chalk.cyan(outputPath)}`
);
} else {
await writeFile(outputPath, serializeSpec(overlay, outputPath));
context.stderr.info(
`${Icons.success} Overlay written (${chalk.bold(String(overlay.actions.length))} action(s)) ${chalk.dim("→")} ${chalk.cyan(outputPath)}`
);
}
const edit = await editor.addOverlay(entry.specFilePath, outputPath);
if (edit != null) {
const relPath = path.relative(context.cwd, await editor.getApiFilePath());
context.stderr.info(chalk.dim(`${relPath}:${edit.line}: added reference to ${path.basename(outputPath)}`));
}
}
private async splitAsOverrides(
context: Context,
editor: FernYmlEditor,
entry: SpecEntry,
// biome-ignore lint/suspicious/noExplicitAny: OpenAPI specs can have any shape
originalContent: Record<string, any>,
// biome-ignore lint/suspicious/noExplicitAny: OpenAPI specs can have any shape
currentContent: Record<string, any>,
outputArg: string | undefined
): Promise<void> {
const overrides = generateOverrides(originalContent, currentContent);
const outputPath =
outputArg != null
? resolvePathOrThrow(context, outputArg)
: deriveOutputPath(entry.specFilePath, OVERRIDES_NAME);
if (await this.tryMergeIntoExistingFile(outputPath, overrides)) {
context.stderr.info(`${Icons.success} Merged diff into existing ${chalk.cyan(outputPath)}`);
} else {
await writeFile(outputPath, serializeSpec(overrides, outputPath));
context.stderr.info(`${Icons.success} Overrides written ${chalk.dim("→")} ${chalk.cyan(outputPath)}`);
}
const edit = await editor.addOverride(entry.specFilePath, outputPath);
if (edit != null) {
const relPath = path.relative(context.cwd, await editor.getApiFilePath());
context.stderr.info(chalk.dim(`${relPath}:${edit.line}: added reference to ${path.basename(outputPath)}`));
}
}
/**
* Tries to load an existing overlay file and append new actions to it.
* Returns true if the file existed and was merged, false if the file does not exist.
*/
private async tryMergeOverlayIntoExistingFile(
outputPath: AbsoluteFilePath,
newActions: OverlayOutputAction[]
): Promise<boolean> {
let raw: string;
try {
raw = await readFile(outputPath, "utf8");
} catch (error) {
if (isEnoentError(error)) {
return false;
}
throw error;
}
const existing = parseSpec(raw, outputPath);
const existingActions = Array.isArray(existing.actions) ? existing.actions : [];
existing.actions = [...existingActions, ...newActions];
await writeFile(outputPath, serializeSpec(existing, outputPath));
return true;
}
/**
* Tries to load an existing file at `outputPath` and merge overrides into it.
* Returns true if the file existed and was merged, false if the file does not exist.
*/
private async tryMergeIntoExistingFile(
outputPath: AbsoluteFilePath,
// biome-ignore lint/suspicious/noExplicitAny: OpenAPI specs can have any shape
overrides: Record<string, any>
): Promise<boolean> {
let raw: string;
try {
raw = await readFile(outputPath, "utf8");
} catch (error) {
if (isEnoentError(error)) {
return false;
}
throw error;
}
const existing = parseSpec(raw, outputPath);
const merged = mergeWithOverrides({ data: existing, overrides });
await writeFile(outputPath, serializeSpec(merged, outputPath));
return true;
}
private async getFileFromGitHead(repoRoot: string, absolutePath: AbsoluteFilePath): Promise<string> {
try {
const relativePath = path.relative(repoRoot, absolutePath);
const { stdout } = await execFileAsync("git", ["show", `HEAD:${relativePath}`], {
cwd: repoRoot,
encoding: "utf8",
maxBuffer: GIT_MAX_BUFFER_BYTES
});
return stdout;
} catch (error: unknown) {
const detail = extractErrorMessage(error);
throw new CliError({
message: `Failed to get file from git HEAD: ${absolutePath}. Is the file tracked by git and has at least one commit?\n Cause: ${detail}`,
code: CliError.Code.ParseError
});
}
}
private async getRepoRoot(nearPath: AbsoluteFilePath): Promise<string> {
const { stdout } = await execFileAsync("git", ["rev-parse", "--show-toplevel"], {
cwd: path.dirname(nearPath),
encoding: "utf8"
});
return stdout.trim();
}
}
function resolvePathOrThrow(context: Context, outputPath: string): AbsoluteFilePath {
const resolved = context.resolveOutputFilePath(outputPath);
if (resolved == null) {
throw new CliError({
message: `Could not resolve output path: ${outputPath}`,
code: CliError.Code.ConfigError
});
}
return resolved;
}
export function addSplitCommand(cli: Argv<GlobalArgs>): void {
const cmd = new SplitCommand();
command(
cli,
"split",
"Extract changes from a modified spec into an overlay or override file, restoring the spec to its git HEAD state",
(context, args) => cmd.handle(context, args as SplitCommand.Args),
(yargs) =>
yargs
.option("api", {
type: "string",
description: "Filter by API name"
})
.option("output", {
type: "string",
description: "Custom output path for the new overlay/override file"
})
.option("format", {
type: "string",
default: OVERLAY_NAME,
description: `Output format: '${OVERLAY_NAME}' (OpenAPI Overlay 1.0.0) or '${OVERRIDES_NAME}' (Fern deep-merge)`
})
.coerce("format", (value: string): SplitFormatInput => {
if (!(ALL_FORMAT_NAMES as readonly string[]).includes(value)) {
throw new Error(
`Invalid format '${value}'. Expected one of: ${OVERLAY_NAME}, ${OVERRIDES_NAME}`
);
}
return value as SplitFormatInput;
})
);
}