Skip to content

Commit 7dda943

Browse files
ENGG-3170: Create collection folder and sub files using complete collection record (#139)
1 parent 67e5baf commit 7dda943

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/renderer/actions/local-sync/fs-manager.rpc-service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,10 @@ export class FsManagerRPCService extends RPCServiceOverIPC {
125125
"getRawFileData",
126126
this.fsManager.getRawFileData.bind(this.fsManager)
127127
);
128+
129+
this.exposeMethodOverIPC(
130+
"createCollectionFromCompleteRecord",
131+
this.fsManager.createCollectionFromCompleteRecord.bind(this.fsManager)
132+
);
128133
}
129134
}

src/renderer/actions/local-sync/fs-manager.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
API,
5050
APIEntity,
5151
Collection,
52+
CollectionRecord,
5253
Environment,
5354
EnvironmentVariableValue,
5455
ErroredRecords,
@@ -1122,4 +1123,79 @@ export class FsManager {
11221123
}
11231124
return parsedRecordResult;
11241125
}
1126+
1127+
async createCollectionFromCompleteRecord(
1128+
collection: CollectionRecord,
1129+
id: string
1130+
): Promise<FileSystemResult<Collection>> {
1131+
try {
1132+
const collectionFolder = this.createResource({
1133+
id,
1134+
type: "folder",
1135+
});
1136+
const createResult = await createFolder(collectionFolder);
1137+
if (createResult.type === "error") {
1138+
return createResult;
1139+
}
1140+
1141+
if (collection.description?.length) {
1142+
const descriptionFile = this.createResource({
1143+
id: appendPath(collectionFolder.path, DESCRIPTION_FILE),
1144+
type: "file",
1145+
});
1146+
const writeResult = await writeContent(
1147+
descriptionFile,
1148+
{ description: collection.description },
1149+
new ReadmeRecordFileType()
1150+
);
1151+
if (writeResult.type === "error") {
1152+
return writeResult;
1153+
}
1154+
}
1155+
1156+
if (
1157+
collection.data.auth &&
1158+
collection.data.auth.currentAuthType !== AuthType.NO_AUTH
1159+
) {
1160+
const authFile = this.createResource({
1161+
id: appendPath(collectionFolder.path, COLLECTION_AUTH_FILE),
1162+
type: "file",
1163+
});
1164+
const writeResult = await writeContent(
1165+
authFile,
1166+
collection.data.auth,
1167+
new AuthRecordFileType()
1168+
);
1169+
if (writeResult.type === "error") {
1170+
return writeResult;
1171+
}
1172+
}
1173+
1174+
if (collection.data.variables) {
1175+
const variablesFile = this.createResource({
1176+
id: appendPath(collectionFolder.path, COLLECTION_VARIABLES_FILE),
1177+
type: "file",
1178+
});
1179+
const writeResult = await writeContent(
1180+
variablesFile,
1181+
collection.data.variables,
1182+
new CollectionVariablesRecordFileType()
1183+
);
1184+
if (writeResult.type === "error") {
1185+
return writeResult;
1186+
}
1187+
}
1188+
1189+
return parseFolderToCollection(this.rootPath, collectionFolder);
1190+
} catch (e: any) {
1191+
return {
1192+
type: "error",
1193+
error: {
1194+
message: e.message || "An unexpected error has occured!",
1195+
path: e.path || "Unknown path",
1196+
fileType: FileTypeEnum.UNKNOWN,
1197+
},
1198+
};
1199+
}
1200+
}
11251201
}

src/renderer/actions/local-sync/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ export type Environment = {
9595

9696
export type APIEntity = Collection | API | Environment;
9797

98+
export type CollectionRecord = {
99+
type: "collection";
100+
name: string;
101+
description?: string;
102+
collectionId: string;
103+
data: {
104+
auth: Static<typeof Auth>;
105+
variables: Variable;
106+
scripts?: {
107+
preRequest: string;
108+
postResponse: string;
109+
};
110+
};
111+
};
98112
export type ErroredRecords = {
99113
name: string;
100114
path: string;

0 commit comments

Comments
 (0)