-
Notifications
You must be signed in to change notification settings - Fork 0
/
refactor.ts
157 lines (140 loc) · 4.9 KB
/
refactor.ts
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
import { file } from "@babel/types";
import {
MethodDeclaration,
MethodDeclarationStructure,
OptionalKind,
Project,
ScriptTarget,
SourceFile,
} from "ts-morph";
const typesToRename: Record<string, string> = {
app__routers__compute__models__CommandOutput: "ComputeCommandOutput",
app__routers__utils__models__CommandOutput: "CommandOutput",
Body_create_group_account_groups_post: "CreateGroupBody",
Body_run_command_utilities_command__machine__post: "RunCommandBody",
Body_start_transfer_storage_transfer_post: "TransferStorageBody",
Body_submit_job_compute_jobs__machine__post: "SubmitJobBody",
Body_update_group_membership_account_groups__group__put: "UpdateGroupBody",
Body_upload_file_utilities_upload__machine___path__put: "UploadFileBody",
app__routers__status__models__Status: "Status",
};
const enumsToRename: Record<string, string> = {
app__routers__compute__models__Status: "ComputeStatus",
app__routers__storage__models__Status: "StorageStatus",
app__routers__utils__models__Status: "UtilitiesStatus",
};
const methodsToRename: Record<string, Record<string, string>> = {
AccountService: {
readProjectsAccountProjectsGet: "getProjects",
readUserAccountGet: "getUser",
readRolesAccountRolesGet: "getRoles",
readGroupsAccountGroupsGet: "getGroups",
createGroupAccountGroupsPost: "createGroup",
readGroupAccountGroupsGroupGet: "getGroup",
updateGroupMembershipAccountGroupsGroupPut: "updateGroup",
},
ComputeService: {
readJobsComputeJobsMachineGet: "getJobs",
submitJobComputeJobsMachinePost: "submitJob",
readJobComputeJobsMachineJobidGet: "getJob",
cancelJobComputeJobsMachineJobidDelete: "cancelJob",
},
MetaService: {
readChangelogMetaChangelogGet: "getChangelog",
readConfigMetaConfigGet: "getConfig",
},
StorageService: {
startTransferStorageTransferPost: "startTransfer",
},
TasksService: {
readTaskTasksIdGet: "getTask",
readTasksTasksGet: "getTasks",
},
UtilitiesService: {
uploadFileUtilitiesUploadMachinePathPut: "uploadFile",
downloadFileUtilitiesDownloadMachinePathGet: "downloadFile",
readDirectoryUtilitiesLsMachinePathGet: "listDirectory",
runCommandUtilitiesCommandMachinePost: "runCommand",
},
StatusService: {
readStatusesStatusGet: "getStatus",
readNotesStatusNotesGet: "getNotes",
readOutagesStatusOutagesGet: "getOutages",
readPlannedOutagesStatusOutagesPlannedGet: "getPlannedOutages",
readNoteStatusNotesNameGet: "getNotesBySystem",
readOutageStatusOutagesNameGet: "getOutagesBySystem",
readPlannedOutagesStatusOutagesPlannedNameGet: "getPlannedOutagesBySystem",
readStatusStatusNameGet: "getStatusBySystem",
},
};
const renameTypes = (file: SourceFile) => {
const name = file.getBaseNameWithoutExtension();
if (Object.keys(typesToRename).indexOf(name) !== -1) {
const newTypeName = typesToRename[name];
const newFileName = `${newTypeName}.ts`;
file.move(newFileName);
// Rename the type
const typeToRename = file.getTypeAlias(name);
if (!typeToRename) {
throw new Error(`Type not found: ${name}`);
}
typeToRename.rename(newTypeName);
}
};
const renameEnums = (file: SourceFile) => {
const name = file.getBaseNameWithoutExtension();
if (Object.keys(enumsToRename).indexOf(name) !== -1) {
const newTypeName = enumsToRename[name];
const newFileName = `${newTypeName}.ts`;
file.move(newFileName);
// Rename the enum
const enumToRename = file.getEnum(name);
if (!enumToRename) {
throw new Error(`Enum not found: ${name}`);
}
enumToRename.rename(newTypeName);
}
};
const renameMethods = (file: SourceFile) => {
const name = file.getBaseNameWithoutExtension();
if (Object.keys(methodsToRename).indexOf(name) !== -1) {
const renames = methodsToRename[name];
const cls = file.getClass(name);
if (!cls) {
throw new Error(`Class not found: ${name}`);
}
Object.entries(renames).forEach(([oldName, newName]) => {
const method = cls.getInstanceMethod(oldName);
if (!method) {
throw new Error(`Method not found: ${oldName}`);
}
method.rename(newName);
});
}
file.getProject().save();
};
const refactorModels = async (project: Project) => {
project.getSourceFiles().forEach((f: SourceFile) => {
if (f.getDirectory().getBaseName() !== "models") return;
renameTypes(f);
renameEnums(f);
});
project.save();
};
const refactorServices = async (project: Project) => {
project.getSourceFiles().forEach((f: SourceFile) => {
if (f.getDirectory().getBaseName() !== "services") return;
renameMethods(f);
});
};
const refactor = async () => {
const project = new Project({
tsConfigFilePath: "./tsconfig.json",
});
project.addSourceFilesAtPaths(["src/sfapi/_internal/**/*.ts", "!**/*.d.ts"]);
await refactorServices(project);
await project.save();
await refactorModels(project);
await project.save();
};
refactor();