-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathmigration.ts
More file actions
91 lines (81 loc) · 3.27 KB
/
migration.ts
File metadata and controls
91 lines (81 loc) · 3.27 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
import { AbsoluteFilePath } from "@fern-api/fs-utils";
import { CliError, TaskContext } from "@fern-api/task-context";
import { readFile, writeFile } from "fs/promises";
import YAML from "yaml";
import { Migration } from "../../../types/Migration.js";
import { getAllYamlFiles } from "./getAllYamlFiles.js";
export const migration: Migration = {
name: "remove-inline-error-declarations",
summary: "moves inlined error declarations to be types.",
run: async ({ context }) => {
const yamlFiles = await getAllYamlFiles(context);
for (const filepath of yamlFiles) {
try {
await migrateFile(filepath, context);
} catch (error) {
context.failWithoutThrowing(`Failed to add 'key' property to union in ${filepath}`, error, {
code: CliError.Code.ParseError
});
}
}
}
};
async function migrateFile(filepath: AbsoluteFilePath, context: TaskContext): Promise<void> {
const contents = await readFile(filepath);
const parsedDocument = YAML.parseDocument(contents.toString());
const addType = (typeName: string, typeDeclaration: YAML.Node) => {
const types = parsedDocument.get("types");
if (types == null) {
parsedDocument.set("types", {
[typeName]: typeDeclaration
});
} else if (!YAML.isMap(types)) {
context.failWithoutThrowing(`"types" is not a map in ${filepath}`, undefined, {
code: CliError.Code.ParseError
});
} else {
types.set(typeName, typeDeclaration);
}
};
const errors = parsedDocument.get("errors");
if (errors == null) {
return;
}
if (!YAML.isMap(errors)) {
context.failWithoutThrowing(`"errors" is not a map in ${filepath}`, undefined, {
code: CliError.Code.ParseError
});
return;
}
for (const errorDeclaration of errors.items) {
if (!YAML.isMap(errorDeclaration.value)) {
context.failWithoutThrowing(`Error "${errorDeclaration.key}" is not a map in ${filepath}`, undefined, {
code: CliError.Code.ParseError
});
continue;
}
// move type to be its own declaration
const errorType = errorDeclaration.value.get("type", true);
if (errorType != null && typeof errorType.value !== "string") {
const errorBodyName = `${errorDeclaration.key}Body`;
addType(errorBodyName, errorType);
errorDeclaration.value.set("type", errorBodyName);
}
// move status code
const httpSection = errorDeclaration.value.get("http");
if (httpSection != null) {
if (!YAML.isMap(httpSection)) {
context.failWithoutThrowing(
`http in "${errorDeclaration.key}" is not a map in ${filepath}`,
undefined,
{ code: CliError.Code.ParseError }
);
} else {
const statusCode = httpSection.get("statusCode", true);
errorDeclaration.value.delete("http");
errorDeclaration.value.set("status-code", statusCode);
}
}
}
await writeFile(filepath, parsedDocument.toString());
}