Skip to content

Commit 9875413

Browse files
committed
sync model spinners
1 parent 0f8e276 commit 9875413

File tree

10 files changed

+223
-171
lines changed

10 files changed

+223
-171
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const noSyncTaskEmoji = "\u{2728}";

src/modules/sync/sync/assetFolders.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { AssetFolderModels, ManagementClient } from "@kontent-ai/management-sdk";
2+
import { oraPromise } from "ora";
23
import { match } from "ts-pattern";
34

45
import { logError, logInfo, LogOptions } from "../../../log.js";
56
import { handleKontentErrors, throwError } from "../../../utils/error.js";
67
import { apply, not } from "../../../utils/function.js";
78
import { omit } from "../../../utils/object.js";
89
import { serially } from "../../../utils/requests.js";
10+
import { noSyncTaskEmoji } from "../constants/emojiCodes.js";
911
import { DiffModel } from "../types/diffModel.js";
1012
import { getTargetCodename, PatchOperation } from "../types/patchOperation.js";
1113
import { isOp } from "./utils.js";
@@ -16,29 +18,30 @@ export const syncAssetFolders = async (
1618
logOptions: LogOptions,
1719
) => {
1820
if (!operations.length) {
19-
logInfo(logOptions, "standard", "No asset folders updates");
21+
logInfo(logOptions, "standard", `${noSyncTaskEmoji}No asset folders updates.`);
2022
return;
2123
}
2224

23-
logInfo(logOptions, "standard", "Updating asset folders");
24-
2525
const removeOps = operations.filter(isOp("remove"));
2626

27-
await serially(removeOps.map(operation => () =>
28-
client
29-
.modifyAssetFolders()
30-
.withData([convertOperation(operation)])
31-
.toPromise()
32-
.catch(handleKontentErrors(err => {
33-
logError(
34-
logOptions,
35-
"error",
36-
`Failed to remove asset folder "${operation.path}" error: ${JSON.stringify(err)}. Skipping it.`,
37-
);
27+
await oraPromise(
28+
serially(removeOps.map(operation => () =>
29+
client
30+
.modifyAssetFolders()
31+
.withData([convertOperation(operation)])
32+
.toPromise()
33+
.catch(handleKontentErrors(err => {
34+
logError(
35+
logOptions,
36+
"error",
37+
`Failed to remove asset folder "${operation.path}" error: ${JSON.stringify(err)}. Skipping it.`,
38+
);
3839

39-
return undefined;
40-
}, [5])) // this is a generic code for invalid body, but the error with folder containing assets unfortunately doesn't have a specific code
41-
));
40+
return undefined;
41+
}, [5])) // this is a generic code for invalid body, but the error with folder containing assets unfortunately doesn't have a specific code
42+
)),
43+
{ text: "Updating asset folders" },
44+
);
4245

4346
const restOps = operations.filter(not(isOp("remove")));
4447

src/modules/sync/sync/collections.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { CollectionModels, ManagementClient } from "@kontent-ai/management-sdk";
2+
import { oraPromise } from "ora";
23

34
import { logInfo, LogOptions } from "../../../log.js";
45
import { omit } from "../../../utils/object.js";
6+
import { noSyncTaskEmoji } from "../constants/emojiCodes.js";
57
import { DiffModel } from "../types/diffModel.js";
68
import { getTargetCodename, PatchOperation } from "../types/patchOperation.js";
79

@@ -11,16 +13,17 @@ export const syncAddAndReplaceCollections = (
1113
logOptions: LogOptions,
1214
) => {
1315
if (!collections.length) {
14-
logInfo(logOptions, "standard", "No collections to add or update");
16+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No collections to add or update`);
1517
return Promise.resolve();
1618
}
1719

18-
logInfo(logOptions, "standard", "Adding and updating collections");
19-
20-
return client
21-
.setCollections()
22-
.withData(collections.filter(op => op.op !== "remove").map(transformCollectionsReferences))
23-
.toPromise();
20+
return oraPromise(
21+
client
22+
.setCollections()
23+
.withData(collections.filter(op => op.op !== "remove").map(transformCollectionsReferences))
24+
.toPromise(),
25+
{ text: "Adding and updating collections" },
26+
);
2427
};
2528

2629
export const syncRemoveCollections = (
@@ -31,16 +34,19 @@ export const syncRemoveCollections = (
3134
const collectionsRemoveOps = collections.filter(op => op.op === "remove");
3235

3336
if (!collectionsRemoveOps.length) {
34-
logInfo(logOptions, "standard", "No collections to delete");
37+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No collections to delete`);
3538
return Promise.resolve();
3639
}
3740

38-
logInfo(logOptions, "standard", "Deleting collections");
39-
40-
return client
41-
.setCollections()
42-
.withData(collectionsRemoveOps.map(transformCollectionsReferences))
43-
.toPromise();
41+
return oraPromise(
42+
client
43+
.setCollections()
44+
.withData(collectionsRemoveOps.map(transformCollectionsReferences))
45+
.toPromise(),
46+
{
47+
text: "Deleting collections",
48+
},
49+
);
4450
};
4551

4652
const transformCollectionsReferences = (operation: PatchOperation): CollectionModels.ISetCollectionData => {

src/modules/sync/sync/languages.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { LanguageModels, ManagementClient } from "@kontent-ai/management-sdk";
2+
import { oraPromise } from "ora";
23
import { match } from "ts-pattern";
34
import { v4 as createUuid } from "uuid";
45
import { z } from "zod";
56

67
import { logInfo, LogOptions } from "../../../log.js";
78
import { omit } from "../../../utils/object.js";
89
import { serially } from "../../../utils/requests.js";
10+
import { noSyncTaskEmoji } from "../constants/emojiCodes.js";
911
import { DiffModel } from "../types/diffModel.js";
1012
import { PatchOperation } from "../types/patchOperation.js";
1113

@@ -15,15 +17,14 @@ export const syncLanguages = async (
1517
logOptions: LogOptions,
1618
) => {
1719
if (operations.added.length) {
18-
logInfo(logOptions, "standard", "Adding languages");
19-
await serially(operations.added.filter(op => !op.is_default).map(l => () => addLanguage(client, l)));
20+
await oraPromise(serially(operations.added.filter(op => !op.is_default).map(l => () => addLanguage(client, l))), {
21+
text: "Adding languages",
22+
});
2023
} else {
21-
logInfo(logOptions, "standard", "No languages to add");
24+
logInfo(logOptions, "standard", `${noSyncTaskEmoji}No languages to add`);
2225
}
2326

2427
if ([...operations.updated].flatMap(([, arr]) => arr).length) {
25-
logInfo(logOptions, "standard", "Updating Languages");
26-
2728
const transformedOperations = [...operations.updated].map(
2829
([codename, operations]) => [codename, operations.map(transformLanguagePatchOperation)] as const,
2930
);
@@ -32,21 +33,25 @@ export const syncLanguages = async (
3233
operationsToOrdNumb(operations) - operationsToOrdNumb(operations2)
3334
);
3435

35-
await serially(
36-
sortedOperations.map(([codename, operations]) => () => modifyLanguage(client, codename, operations)),
36+
await oraPromise(
37+
serially(
38+
sortedOperations.map(([codename, operations]) => () => modifyLanguage(client, codename, operations)),
39+
),
40+
{ text: "Updating Languages" },
3741
);
3842
} else {
39-
logInfo(logOptions, "standard", "No languages to update");
43+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No languages to update`);
4044
}
4145

4246
if (operations.deleted.size) {
43-
logInfo(logOptions, "standard", "Deactivating languages");
44-
45-
await serially(
46-
[...operations.deleted].map(codename => () => deleteLanguage(client, codename)),
47+
await oraPromise(
48+
serially(
49+
[...operations.deleted].map(codename => () => deleteLanguage(client, codename)),
50+
),
51+
{ text: "Deactivating languages" },
4752
);
4853
} else {
49-
logInfo(logOptions, "standard", "No languages to deactivate");
54+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No languages to deactivate`);
5055
}
5156
};
5257

src/modules/sync/sync/snippets.ts

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { ContentTypeElements, ContentTypeSnippetModels, ManagementClient } from "@kontent-ai/management-sdk";
2+
import { oraPromise } from "ora";
23

34
import { logInfo, LogOptions } from "../../../log.js";
45
import { omit } from "../../../utils/object.js";
56
import { serially } from "../../../utils/requests.js";
67
import { elementTypes } from "../constants/elements.js";
8+
import { noSyncTaskEmoji } from "../constants/emojiCodes.js";
79
import { DiffModel } from "../types/diffModel.js";
810
import { PatchOperation } from "../types/patchOperation.js";
911
import {
@@ -21,12 +23,16 @@ export const addSnippetsWithoutReferences = async (
2123
logOptions: LogOptions,
2224
) => {
2325
if (!addSnippets.length) {
24-
logInfo(logOptions, "standard", "No content type snippets to add");
26+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No content type snippets to add`);
2527
return;
2628
}
27-
logInfo(logOptions, "standard", "Adding content type snippets");
2829
const addSnippetsWithoutReferences = addSnippets.map(removeReferencesFromAddOp);
29-
await serially(addSnippetsWithoutReferences.map(s => () => addSnippet(client, s)));
30+
await oraPromise(
31+
serially(addSnippetsWithoutReferences.map(s => () => addSnippet(client, s))),
32+
{
33+
text: "Adding content type snippets",
34+
},
35+
);
3036
};
3137

3238
export const addSnippetsReferences = async (
@@ -52,22 +58,24 @@ export const addSnippetsReferences = async (
5258
const snippetsReplaceReferencesOps = addSnippets.map(createUpdateReferencesOps);
5359

5460
if (snippetsReplaceReferencesOps.every(([, arr]) => !arr.length)) {
55-
logInfo(logOptions, "standard", "No content type snippet's references to update");
61+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No content type snippet's references to update`);
5662
return;
5763
}
5864

59-
logInfo(logOptions, "standard", "Updating content type snippet's references");
60-
await serially(
61-
[...snippetReplaceOpsAddIntoReferencingElements, ...snippetsReplaceReferencesOps].map(
62-
([codename, operations]) => () =>
63-
operations.length
64-
? updateSnippet(
65-
client,
66-
codename,
67-
operations.map(o => omit(o, ["oldValue"])),
68-
)
69-
: Promise.resolve(),
65+
await oraPromise(
66+
serially(
67+
[...snippetReplaceOpsAddIntoReferencingElements, ...snippetsReplaceReferencesOps].map(
68+
([codename, operations]) => () =>
69+
operations.length
70+
? updateSnippet(
71+
client,
72+
codename,
73+
operations.map(o => omit(o, ["oldValue"])),
74+
)
75+
: Promise.resolve(),
76+
),
7077
),
78+
{ text: "Updating content type snippet's references" },
7179
);
7280
};
7381

@@ -95,17 +103,19 @@ export const addElementsIntoSnippetsWithoutReferences = async (
95103
);
96104

97105
if (addSnippetsOpsWithoutRefs.every(([, ops]) => !ops.length)) {
98-
logInfo(logOptions, "standard", "No elements to add into content type snippets");
106+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No elements to add into content type snippets`);
99107
return;
100108
}
101109

102-
logInfo(logOptions, "standard", "Adding elements into content type snippets");
103-
await serially(addSnippetsOpsWithoutRefs.map(
104-
([codename, operations]) => () =>
105-
operations.length
106-
? updateSnippet(client, codename, operations)
107-
: Promise.resolve(),
108-
));
110+
await oraPromise(
111+
serially(addSnippetsOpsWithoutRefs.map(
112+
([codename, operations]) => () =>
113+
operations.length
114+
? updateSnippet(client, codename, operations)
115+
: Promise.resolve(),
116+
)),
117+
{ text: "Adding elements into content type snippets" },
118+
);
109119
};
110120

111121
export const updateSnippets = async (
@@ -117,22 +127,24 @@ export const updateSnippets = async (
117127
.map(([c, ops]) => [c, ops.filter(o => !isOp("addInto")(o))] as const);
118128

119129
if (otherSnippetOps.flatMap(([, ops]) => ops).length === 0) {
120-
logInfo(logOptions, "standard", "No content type snippets to update");
130+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No content type snippets to update`);
121131
return;
122132
}
123133

124-
logInfo(logOptions, "standard", "Updating content type snippets");
125-
await serially(
126-
otherSnippetOps.map(
127-
([codename, operations]) => () =>
128-
operations.length
129-
? updateSnippet(
130-
client,
131-
codename,
132-
operations.map(op => "oldValue" in op ? omit(op, ["oldValue"]) : op),
133-
)
134-
: Promise.resolve(),
134+
await oraPromise(
135+
serially(
136+
otherSnippetOps.map(
137+
([codename, operations]) => () =>
138+
operations.length
139+
? updateSnippet(
140+
client,
141+
codename,
142+
operations.map(op => "oldValue" in op ? omit(op, ["oldValue"]) : op),
143+
)
144+
: Promise.resolve(),
145+
),
135146
),
147+
{ text: "Updating content type snippets" },
136148
);
137149
};
138150

@@ -142,10 +154,11 @@ export const deleteContentTypeSnippets = async (
142154
logOptions: LogOptions,
143155
) => {
144156
if (snippetOps.deleted.size) {
145-
logInfo(logOptions, "standard", "Deleting content type snippets");
146-
await serially(Array.from(snippetOps.deleted).map(c => () => deleteSnippet(client, c)));
157+
await oraPromise(serially(Array.from(snippetOps.deleted).map(c => () => deleteSnippet(client, c))), {
158+
text: "Deleting content type snippets",
159+
});
147160
} else {
148-
logInfo(logOptions, "standard", "No content type snippets to delete");
161+
logInfo(logOptions, "standard", `${noSyncTaskEmoji} No content type snippets to delete`);
149162
}
150163
};
151164

0 commit comments

Comments
 (0)